In our recent project we needed to convert HTML to Pdf. We first tried PHP based library called tcpdf, but it has lots of limitations.
Finally solution was to use Qt. Here is the code written in PyQT:
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * app = QApplication(sys.argv) web = QWebView() 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) print "Pdf generated" QApplication.exit() QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt) sys.exit(app.exec_())
To convert HTML text we can also use setHtml() instead of load() which takes url of the page you want to convert. But on Windows when we use setHtml() the loadFinished() signal was not emitted( bug?? ) whereas it works on Linux.