Linux.com

Home Learn Linux Linux Documentation Linux How Tos Converting HTML to Pdf Using Python and Qt

Converting HTML to Pdf Using Python and Qt

 

Here is the code:

 

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_())

 

Comments

Subscribe to Comments Feed

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Join / Linux Training / Board