import email, sys, os
from xhtml2pdf import pisa
def convertHtmlToPdf(sourceHtml, outputFilename):
resultFile = open(outputFilename, "wb")
pisaStatus = pisa.CreatePDF(sourceHtml,resultFile)
resultFile.close()
return
#a = <html grabbed from the ingested email>
msg = email.message_from_string(a)
print(msg.get_payload())
for part in msg.walk():
if part.get_content_type():
body = str(part.get_payload())
with open('/tmp/emltohtml.html', 'wb') as f:
f.write(body)
#read
f = open("/tmp/emltohtml.html", "rb")
sourceHtml = f.read()
pisa.showLogging()
outputFilename = "/tmp/test555.pdf"
pisa.showLogging()
phantom.debug(type(sourceHtml))
convertHtmlToPdf(sourceHtml, outputFilename)
这就是我得到的错误:
WARNING [xhtml2pdf] /home/user/.local/lib/python2.7/site-packages/xhtml2pdf/parser.py line 97: Attribute 'align' of wrong value, allowed is one of: ['top', 'middle', 'bottom', 'left', 'right', 'texttop', 'absmiddle', 'absbottom', 'baseline']
u'<a href="https://urldefense.com/v3/__http://etrack.freeconferencecall.com/t/gcH1AAhbaBYSdQAD1mBEMg2Hha3XvJ2KBhaaaa32mBPIHFDxaa?m=8_w8xE0*amp;k=KvVrr.Jahols*25BtvglZhl.jht*amp;e=j*amp;q=__;fn5-fg!!CDV5USbF!dFnF_cam37jt_IHL0zIYmhY5tpZXYU64EfP_nbPsYeojc2eWzyniAoQumbF7P18jnKk$"><img align="center" alt="FreeConferenceCall logo" class="float-center" data-unique-identifier="" src="https://www.freeconferencecall.com/images/email_template/fcc_logo_noshadow.png" style="clear: both; display: block; margin: 0px auto; max-width: 100%; outline: 0px; text-align: center; text-decoration: none;"/></a>'
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/home/user/.local/lib/python2.7/site-packages/xhtml2pdf/document.py", line 144, in pisaDocument
doc.build(context.story)
File "/home/user/.local/lib/python2.7/site-packages/reportlab/platypus/doctemplate.py", line 1056, in build
self.handle_flowable(flowables)
File "/home/user/.local/lib/python2.7/site-packages/reportlab/platypus/doctemplate.py", line 912, in handle_flowable
if frame.add(f, canv, trySplit=self.allowSplitting):
File "/home/user/.local/lib/python2.7/site-packages/reportlab/platypus/frames.py", line 174, in _add
w, h = flowable.wrap(aW, h)
File "/home/user/.local/lib/python2.7/site-packages/xhtml2pdf/xhtml2pdf_reportlab.py", line 775, in wrap
return Table.wrap(self, availWidth, availHeight)
File "/home/user/.local/lib/python2.7/site-packages/reportlab/platypus/tables.py", line 1206, in wrap
self._calc(availWidth, availHeight)
File "/home/user/.local/lib/python2.7/site-packages/reportlab/platypus/tables.py", line 657, in _calc
self._calc_height(availHeight,availWidth,W=W)
File "/home/user/.local/lib/python2.7/site-packages/reportlab/platypus/tables.py", line 624, in _calc_height
y = H[i] - c
TypeError: unsupported operand type(s) for -: 'str' and 'int'
但是如果我用其他东西替换html文件,或者只是将普通html存储在一个变量中并进行转换,它就会工作。看起来,保存emltohtml.HTML文件中整个HTML的sourceHtml变量是一个字符串,它应该是字符串,但不喜欢HTML中的某个字符,因此它正在中断
以下是html文件: https://pastebin.com/4CtMDLYJ
这可能是因为您在样式表中使用了
height: x%;
reportlab.platypus.tables.Table._calc_height(self, availHeight, availWidth, H=None, W=None)
不将x%转换为与可用高度的一部分相对应的数字,并尝试以下操作:从样式中删除
height: x%;
可以修复它,这并不好另一个选项是使用
position: absolute;
,例如,设置top: 0; bottom: 0;
填充100%的高度,但这似乎也不受支持(尽管它不会引发任何异常)相关问题 更多 >
编程相关推荐