View Full Version : QWebView print() generates huge PDF files on Windows

25th September 2012, 17:59
Hi all,

I tried both on Ubuntu 11.04 and Windows7 to print a html document loaded in QWebView to a PDF file.

On Ubuntu the output PDF file size is about 35KB.
On Windows using PdfFormat as output format the generated PDF file it's 10MB! but using instead PDFCreator as printer the file size is 35KB like on Ubuntu.

The HTML doc contains no images, just some texts and tables.
Looking at the huge PDF files (Win version) I've noticed the background of each page is converted to image (even if it's white/transparent) and I suppose this cause the bigger file size.

I'm using QT4.7.2 on Ubuntu 11.04 vs. QT4.7.1 on Windows7.

Is there a way to get on Windows the same behaviour I've on Ubuntu?

The HTML doc is attached.
Here's the python code I used to reproduce the problem:

from PyQt4.QtGui import QApplication, QPrinter
from PyQt4.QtCore import QObject, SIGNAL
from PyQt4.QtWebKit import QWebView

webView = None

def onLoadFinished( ok ):
global webView
printer = QPrinter( QPrinter.HighResolution )
printer.setOutputFormat( QPrinter.PdfFormat )
printer.setOutputFileName( "out.pdf" )
webView.print_( printer )

if __name__ == "__main__":
import sys
app = QApplication(sys.argv)

webView = QWebView()
QObject.connect(webView, SIGNAL("loadFinished(bool)"), onLoadFinished)

html = ""
with open("html.txt", 'r') as fin:
html = fin.read()
webView.setHtml( html )


26th September 2012, 02:09
You should look at QWebSettings::PrintElementBackgrounds in QWebSettings::setAttribute()

26th September 2012, 15:01
You should look at QWebSettings::PrintElementBackgrounds in QWebSettings::setAttribute()

I've already looked at QWebSettings attributes but it doesn't help.

If I unset that attribute I don't get any background. Instead I want to get a PDF containing the table elements with their background color.
What I don't like is to get a 10MB file without any logical reason. Every PDF reader I've tried takes too much time to load each page of a such PDF file, and the HTML doc I attached is a simplified version of the one I have.