Dominic Jacobssen wrote:

> Anyway, I've attached my efforts, and I'd greatly appreciate any
> guidance or prodding in the right direction.

Hi Dominic, hi all,

Attached herein is a version that works by sizing the PDF page 
correctly. (Also the URL and destination file are command line 
arguments, and no longer hardcoded, for simplicity.)

So it's still a one page PDF, but it doesn't cut off the contents 
anymore.

Otherwise I couldn't find hooks for automated pagination. It may be you 
will have to write those yourself, which shouldn't be horribly 
complicated. Basically, I think: create a QRegion the size of your PDF 
page; render the main Web frame *clipped to that region* onto the 
QPrinter; issue a QPrinter.newPage(); move the region around; loop until 
you've covered the whole content area.

... Also, might be a stupid question, but what happens if you use 
QWebFrame.print() instead of .render()?

HTH,

-- S.
import sys

from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtCore import QObject
from PyQt4.QtCore import QUrl
from PyQt4.QtCore import QSizeF
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, dest ):
    QObject.__init__ ( self )
    self.dest = dest
    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 = QPrinter ( QPrinter.ScreenResolution )
    self.printer.setOutputFormat ( QPrinter.PdfFormat )
    #self.printer.setPaperSize ( QPrinter.A4 )
    self.printer.setFullPage( True )
    self.printer.setPaperSize ( QSizeF( contentsSize ), QPrinter.DevicePixel )
    self.printer.setOrientation ( QPrinter.Portrait )
    self.printer.setOutputFileName ( self.dest )

    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( url, dest ):
  app = QtGui.QApplication ( sys.argv )
  wk = WebKitPDF ( url, dest )
  sys.exit ( app.exec_() )


if __name__ == "__main__":

  import sys

  try:
    url  = sys.argv[1]
    dest = sys.argv[2]

  except IndexError:
    sys.exit()

  main( url, dest )
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to