PDA

View Full Version : QWebView prints PDF file OK with QPrintDialog only



sorin
20th November 2009, 20:41
Hi

I have a small application where I create a report in HTML format that must be shown, print to printer or saved to a PDF file.
I could save the HTML report file as a PDF file if I use the QPrintDialog input and select the PDF output but I could not print PDF using only QPrinter and setting QPrinter properties myself in the code. Why is that?


QWebView *web = new QWebView();
web->load(QUrl(fileHTMLReport));
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer);
if ( dialog->exec() == QDialog::Accepted)
web->print(&printer);

So above if I select PDF output into QPrintDialog window I could save fileHTMLReport in PDF format but if I try to do it without using QPrintDialog by setting only the QPrinter parameters result in an empty PDF file?


QWebView *web = new QWebView();
web->load(QUrl(fileHTMLReport));
QPrinter printer;
printer.setPrinterName("Print to File (PDF)");
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPrintRange(QPrinter::AllPages);
printer.setOrientation(QPrinter::Portrait);
printer.setPaperSize(QPrinter::A4);
printer.setResolution(QPrinter::HighResolution);
printer.setFullPage(false);
printer.setNumCopies(1);
printer.setOutputFileName("printYou.pdf");
web->print(&printer);

result in "printYou.pdf" file being empty and not showing "fileHTMLReport" file as when printing is done through QPrintDialog interface call?

What do I miss here?

Sorin

wysota
26th November 2009, 09:56
I would say you forget to set some parameter in your code which is responsible for scaling the painter or something like that. It might be easiest if you looked into source code for QPrintDialog to see what exactly gets set by it.

Guilo
19th April 2010, 21:24
Hi !

I have exactly the same problem here. Have you found a solution ?

Guilo
20th April 2010, 16:49
Hi !

I am here to answer to my problem it could be useful for someone. In fact I just had to connect the loadFinished signal of the QWebView to a method to print. In my code the printing was launched before the end of the page loading which gave me a blank page