xhtml2pdf
xhtml2pdf copied to clipboard
Add support for `word-wrap` CSS property
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
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.
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 :
.
@perfgao I have the same problem. The generated PDF doesn't break long lines. Does anyone resolve this bug? Thank you 🙇
@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 you are a godsend dude. Thank you !
@p-mazhnik 3 years later, thanks!!! I have wasted like 3 hours of work until I found your reply. Thanks a lot!!
@p-mazhnik , thanks! must be appreciated!
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?
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.
@ShianweiPang
Use in the following way:
pre{
word-wrap: break-all;
-pdf-word-wrap: CJK;
}
word-wrap property is not supported. However, " -pdf-word-wrap: CJK;" only works for few of the HTML data samples for me.