xhtml2pdf icon indicating copy to clipboard operation
xhtml2pdf copied to clipboard

Add support for `word-wrap` CSS property

Open perfgao opened this issue 8 years ago • 11 comments

Hi,

When the contents of the <p> paragraphs in the html code are too long, How can I make it display the entire contents of the normal? No artificial add tags,eg: <br>. I tried to use css to limit the maximum length, and automatic word wrap, but it still can not.

test case:
# -*- coding: utf-8 -*-

import xhtml2pdf.pisa as pisa

pisa.showLogging()

def dumpErrors(pdf, showLog=True):
    if pdf.warn:
        print "*** %d WARNINGS OCCURED" % pdf.warn
    if pdf.err:
        print "*** %d ERRORS OCCURED" % pdf.err

def testlong(src="example.html", dest="example.pdf"):
    pdf = pisa.CreatePDF(file(src, "r"), file(dest, "wb"))

    dumpErrors(pdf)
    if not pdf.err:
        pisa.startViewer(dest)

if __name__=="__main__":
    testlong()

the example html code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>test</title>
        <style>
            body{
                font-family: 'Gilles';
            }
            .testdiv {
                width:100%;
                max-width:500px;
                word-break:break-all;
            }
        </style>
    </head>
    <body>
        <div class="testdiv">
            <p>
 "sdfsdfsdfffffffffffffffffffffffffffwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwggggggggggggggggggggggggpppppppppppaaaaaaazzzzzzzzzzxxxxxxxccccccvvvvvvvbbbbnnnnnnnnmmmmmkkkkkkkkllllll"
            </p>
        </div>
    </body>
</html>

the Pdf display is like this:

"sdfsdfsdfffffffffffffffffffffffffffwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwggggggggggggggggggggggggpppppppppppaaaaaaazzzzzzzzzzxxxxxxxccccc

perfgao avatar Jan 19 '17 08:01 perfgao

Thats a tough one, it may be that a word is too long and not breaking correctly. Can you attach an actual PDF representation of the HTML snippet so I can see whats happening, as this hasn't happened to me before.

LegoStormtroopr avatar Jan 19 '17 21:01 LegoStormtroopr

sorry, I do not quite understand what you mean. You mean is the need for post-production documents? As I posted the same code, the screenshot of the pdf : pdf.

perfgao avatar Jan 20 '17 09:01 perfgao

@perfgao I have the same problem. The generated PDF doesn't break long lines. Does anyone resolve this bug? Thank you 🙇

vuonghv avatar May 06 '19 06:05 vuonghv

@vuonghv try to use property

    -pdf-word-wrap: CJK;

It helped me. I could find this only in the source code, there is no documentation for this for some reason.
Notice that word may be broken at any character, as in word-break: break-all;

p-mazhnik avatar Oct 09 '19 21:10 p-mazhnik

@p-mazhnik you are a godsend dude. Thank you !

us24man avatar Mar 10 '21 09:03 us24man

@p-mazhnik 3 years later, thanks!!! I have wasted like 3 hours of work until I found your reply. Thanks a lot!!

SoundWaveX81 avatar Sep 01 '22 09:09 SoundWaveX81

@p-mazhnik , thanks! must be appreciated!

ksspam avatar Nov 29 '22 07:11 ksspam

Any ideas why it doesn't work for 'a' selector? a { -pdf-word-wrap: CJK; }

The workaround works perfectly on the 'p' and 'div' elements. But I need to apply it to links only. Has anyone managed to solve this?

seluianova avatar Jan 30 '23 12:01 seluianova

Any updates on this regarding word-wrap for other tagging like <a> or <pre>? I am struggling to have my text fully displayed. Do need some advice.

image

ShianweiPang avatar Feb 23 '23 10:02 ShianweiPang

image

@ShianweiPang
Use in the following way: pre{ word-wrap: break-all; -pdf-word-wrap: CJK; }

nisha325 avatar Jun 05 '23 13:06 nisha325

word-wrap property is not supported. However, " -pdf-word-wrap: CJK;" only works for few of the HTML data samples for me.

sneharane588 avatar May 15 '24 11:05 sneharane588