Hi all. I'm a newbie to Qt (but not to Python), and I wanted to ask for "next step" guidance.
I want to generate PDF reports by pointing a HTML-to-PDF renderer at a server that spits out well-formed XHTML with inline SVG. I know that that's a can of worms right there, but I've managed to get a simple server up and running using the combination of CherryPy + Genshi + PyChart, and firefox renders it, so that seemed promising. I know about the "flying saucer" renderer (aka "xhtmlrenderer"), but that's Java and even though I did manage to use it under Jython) I'd prefer something more "natively python" (not that Qt is natively Python, but you get my drift). I have also seen wkhtmltopdf, but it seems that 60% of the source code was ifdef'd in conjunction with a heavily patched QtWebKit, so it wasn't very easy to follow through the flow of source to work out how it worked. After a couple of hours of hacking I came up with the attached (pointing to a public website, not an internal server, of course). This kind of works, but renders the top-left hand corner of the website onto the page, and I can't for the life of me see any hooks for controlling pagination. I'm expecting some kind of "progress callback" interface, a bit like the loadProgress(int) signal, that gets called every time it starts to render a div above which nothing else will be rendered. Or something like that; sorry it's a bit vague, because I don't know the capabilities of QtWebKit. It's possible that it knows all about CSS print media and will just do the right thing :-) Anyway, I've attached my efforts, and I'd greatly appreciate any guidance or prodding in the right direction. Cheers, D
import sys from PyQt4 import QtCore from PyQt4 import QtGui from PyQt4.QtCore import QObject from PyQt4.QtCore import QUrl from PyQt4.QtCore import SIGNAL from PyQt4.QtGui import QPainter from PyQt4.QtGui import QPrinter from PyQt4.QtGui import QImage from PyQt4 import QtWebKit class WebKitPDF ( QObject ): def __init__ ( self, url ): QObject.__init__ ( self ) self.page = QtWebKit.QWebPage ( self ) self.mainFrame = self.page.mainFrame() self.mainFrame.load ( QUrl ( url ) ) self.connect ( self.page, SIGNAL ( "loadProgress(int)" ), self.loadProgress ) self.connect ( self.page, SIGNAL ( "loadFinished(bool)" ), self.renderPDF ) def loadProgress ( self, progress ): print "Progress: ", progress def renderPDF ( self, status ): print "Load finished with status: ", status print "Rendering PDF ..." contentsSize = self.mainFrame.contentsSize(); self.page.setViewportSize ( contentsSize ) self.printer = QPrinter ( QPrinter.PrinterResolution ) self.printer.setOutputFormat ( QPrinter.PdfFormat ) self.printer.setPaperSize ( QPrinter.A4 ) self.printer.setOrientation ( QPrinter.Portrait ) self.printer.setOutputFileName ( "wk.pdf" ) self.painter = QPainter ( self.printer ) self.painter.setRenderHint ( QPainter.Antialiasing ) self.mainFrame.render ( self.painter ) self.painter.end() app = QtGui.QApplication.instance() app.exit ( 0 ) def main(): app = QtGui.QApplication ( sys.argv ) wk = WebKitPDF ( "http://news.bbc.co.uk" ) sys.exit ( app.exec_() ) if __name__ == "__main__": main()
_______________________________________________ PyQt mailing list PyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt