Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. Problem converting html to pdf on Windows
Forum Updated to NodeBB v4.3 + New Features

Problem converting html to pdf on Windows

Scheduled Pinned Locked Moved Qt WebKit
2 Posts 1 Posters 971 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jimpaul
    wrote on last edited by
    #1

    This blogger says the code below works on linux but not Windows though I have not tried it on Linux myself: https://bharatikunal.wordpress.com/2010/01/

    I realize this may be a windows problem but that is what I am working with. Other than serving my html via http, can anyone think of a work around?

    Note: If I use web.load() instead of web.setHtml() it works fine. web.show() properly displays the page in either case.

    @import sys
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from PyQt4.QtWebKit import *

    app = QApplication(sys.argv)

    web = QWebView()

    html = open('//myhtml//notes.html').read()
    web.setHtml(html)
    #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)
    QApplication.exit()

    QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)

    sys.exit(app.exec_())@

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jimpaul
      wrote on last edited by
      #2

      I found this simple solution on StackOverflow

      @from PyQt4.QtGui import QTextDocument, QPrinter, QApplication

      import sys
      app = QApplication(sys.argv)

      doc = QTextDocument()
      doc.setHtml('''
      <html>
      <body>
      <h1>Page 1</h1>
      </body>
      </html>
      ''')

      printer = QPrinter()
      printer.setOutputFileName("foo.pdf")
      printer.setOutputFormat(QPrinter.PdfFormat)
      doc.print_(printer)@

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved