Jun 15
XHTML5 + CSS3 to PDF using PyQT4, WebKit and headless x11-server solution on ubuntu linux
In our app, we have the "burning" need to convert our good looking, xhtml-5, css-3, loads of google chart pictures page into pdf. Lucky enough, I found a quite easy way to accomplish this. All the credits to the awesome QT4 wrapper for webkit.
sudo apt-get install libqt4-gui libqt4-network libqt4-webkit sudo apt-get install python-qt4
Here comes the actually code to generate the pdf file:
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_())
finally, we need to wrap the above script in a "fake" headless x11 StackOverflow article provides inspiration for avoiding a x11 server:
sudo apt-get install xvfb xvfb-run python html2pdf.py