Skip to content

Instantly share code, notes, and snippets.

@creotiv
Created January 9, 2012 22:36
Show Gist options
  • Save creotiv/1585360 to your computer and use it in GitHub Desktop.
Save creotiv/1585360 to your computer and use it in GitHub Desktop.
WebKit(PyQt4) PDF Printer example
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
class htmlViewer(QWebView):
def __init__(self,url, parent=None):
QWebView.__init__(self,parent)
self.setZoomFactor(1)
self.setUrl(QUrl(url))
self.printer = QPrinter(QPrinterInfo.defaultPrinter(),QPrinter.HighResolution)
self.printer.setOutputFormat(QPrinter.PdfFormat)
self.printer.setOrientation(QPrinter.Portrait)
self.printer.setPaperSize(QPrinter.A4)
self.printer.setFullPage(True)
#self.printer.setResolution(72)
self.printer.setOutputFileName("printYou.pdf")
self.loadFinished.connect(self.execpreview)
def execpreview(self,arg):
self.print_(self.printer)
a = htmlViewer("http://habrahabr.ru/")
a.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment