How to convert an HTML document containing an SVG to a PDF file in Python

北城以北 提交于 2020-07-22 06:20:16

问题


I need to adapt some Python code that converts HTML to PDF using QPrinter. The HTML includes some PNGs, but these now need to be replaced by SVGs. I do not really know how to do this. I naively replaced the PNGs with equivalent SVGs, but then the SVGs did not show up in the resulting PDF. To be more concrete, something like this

from PyQt4.QtGui import QTextDocument, QPrinter, QApplication
import sys

app = QApplication(sys.argv)
doc = QTextDocument()
doc.setHtml('''
<html>
     <body>
        <h1>Circle</h1>
            <p><img src="circle.svg"/></p>
     </body>
</html>
 ''')
printer = QPrinter()
printer.setOutputFileName("circle.pdf")
printer.setOutputFormat(QPrinter.PdfFormat)
doc.print_(printer)

with circle.svg given by

<svg xmlns="http://www.w3.org/2000/svg">
    <circle cx="50" cy="50" r="40" fill="orange" /> 
</svg>

does not seem to work, whereas replacing the SVG with an equivalent PNG produces a perfect PDF. Does anyone know how to fix this?


回答1:


I ended up using pdfkit (see https://github.com/JazzCore/python-pdfkit), following the instructions given here: wkhtmltopdf failure when embed an SVG. The Python code now looks like this:

import base64
import os
import pdfkit
with open("circle.svg") as f:
    data=f.read()
    encoded_string = base64.b64encode(data.encode('utf-8'))
    b64 = encoded_string.decode('utf-8')
    html = '''
    <html>
        <body>
            <h1>Circle</h1>
                <p><img alt="" src="data:image/svg+xml;base64,''' + b64 + '''" /></p>
        </body>
    </html>
    '''
    pdfkit.from_string(html, "circle.pdf")

with the SVG modified to:

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
    <circle cx="50" cy="50" r="40" fill="orange" /> 
</svg>

I guess that it should still be possible to fix the code without resorting to another library, but this solution is workable for me.



来源:https://stackoverflow.com/questions/24846171/how-to-convert-an-html-document-containing-an-svg-to-a-pdf-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!