Problem converting html to pdf on Windows
-
This blogger says the code below works on linux but not Windows though I have not tried it on Linux myself: https://bharatikunal.wordpress.com/2010/01/
I realize this may be a windows problem but that is what I am working with. Other than serving my html via http, can anyone think of a work around?
Note: If I use web.load() instead of web.setHtml() it works fine. web.show() properly displays the page in either case.
@import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *app = QApplication(sys.argv)
web = QWebView()
html = open('//myhtml//notes.html').read()
web.setHtml(html)
#web.load(QUrl("http://www.google.com"))#web.show()
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("file.pdf")def convertIt():
web.print_(printer)
QApplication.exit()QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)
sys.exit(app.exec_())@
-
I found this simple solution on StackOverflow
@from PyQt4.QtGui import QTextDocument, QPrinter, QApplication
import sys
app = QApplication(sys.argv)doc = QTextDocument()
doc.setHtml('''
<html>
<body>
<h1>Page 1</h1>
</body>
</html>
''')printer = QPrinter()
printer.setOutputFileName("foo.pdf")
printer.setOutputFormat(QPrinter.PdfFormat)
doc.print_(printer)@