How to create pdf file from Qt application?

Surjya Narayana Padhi picture Surjya Narayana Padhi · Feb 27, 2011 · Viewed 12.3k times · Source

In my Qt application I am conducting some network tests. I have to create a report according to the test output. So I need to create the report in pdf format.

Can anybody please let me know how I can put my test results in a pdf file? My result contains graphs using the Qwt library.

Answer

Max Lambertini picture Max Lambertini · Nov 30, 2011

this code outputs pdf from html:

QTextDocument doc;
doc.setHtml("<h1>hello, I'm an head</h1>");
QPrinter printer;
printer.setOutputFileName("c:\\temp\\file.pdf");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();

I guess you can generate an html wrapper for your img and quickly print your image. Otherwise you might copy the image directly on the printer, since it is a paint device in a similar fashion

QPrinter printer;
QPainter painter(&printer);

printer.setOutputFileName("c:\\temp\\file.pdf");
printer.setOutputFormat(QPrinter::PdfFormat);

painter.drawImage(QRect(0,0,100,100), <QImage loaded from your file>);
printer.newPage();