Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create pdf file from Qt application?

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.

like image 673
Surjya Narayana Padhi Avatar asked Feb 27 '11 17:02

Surjya Narayana Padhi


1 Answers

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();
like image 197
Max Lambertini Avatar answered Sep 19 '22 18:09

Max Lambertini