简体   繁体   中英

TypeError: unsupported operand type(s) for -: 'str' and 'int' while using pisa.Createpdf

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)

And this is the error I get:

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'

But if I replace the html file with something else or just store plain HTML in a variable and do the conversion it works. Looks like the sourceHtml variable that holds the entire HTML from the emltohtml.html file is a string, which is what it should be, but doesn't like a certain character in the HTML and hence it's breaking?

Here is the html file: https://pastebin.com/4CtMDLYJ

This is probably because you've used height: x%; in your stylesheet. reportlab.platypus.tables.Table._calc_height(self, availHeight, availWidth, H=None, W=None) does not convert x% to a number corresponding to a portion of the available height and attempts the following:

for i in xrange(hmax-1,-1,-1):
    j.append(height)
    y = H[i] - c        # c is '100%', for example, while H[i] is an int, thus the exception.
    t = height + y
    c = (t - height) - y
    height = t

Removing height: x%; from your style fixes it, which is not great.

Another option would be to use position: absolute; instead, setting top: 0; bottom: 0; top: 0; bottom: 0; , for example, to fill 100% of the height, but that doesn't seem to be supported either (though it doesn't throw any exceptions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM