我需要调整一些使用 QPrinter 将 HTML 转换为 PDF 的 Python 代码。HTML 包含一些 PNG,但现在需要用 SVG 替换。我真的不知道该怎么做。我天真地用等效的 SVG 替换了 PNG,但是 SVG 没有出现在生成的 PDF 中。更具体地说,像这样
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)
用 circle.svg 给出
<svg xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" fill="orange" />
</svg>
似乎不起作用,而用等效的 PNG 替换 SVG 会产生完美的 PDF。有谁知道如何解决这一问题?