Rendering UI files to PDF
As a followup to my previous blog post about rendering widgets to SVG, lets take a look at rendering them to PDF. I won't go into as much detail as the previous blog post since the code is mostly the same. This time, instead of a renderToSvg() method, we have a renderToPdf() method. All the rest of the code is basically the same as the previous example.
The renderToPdf() method makes use of the ability of QPrinter to generate PDF files (incidentally, it can also generate postscript). The important part of the code is as follows:
QPrinter pdf; pdf.setOutputFormat( QPrinter::PdfFormat ); pdf.setOutputFileName( pdfFile ); QPainter p; p.begin(&pdf); target->render(&p); p.end();We create a printer (using the default printing mode etc.), then tell it to use PDF output. Next we set the output filename. Finally, we render the widget as before. Like QSvgGenerator, QPrinter is a QPaintDevice so the API is just the same. The output file can be seen at http://xmelegance.org/devel/networkrequests.pdf, and the code is in gitorious qt-examples repository as before.
I mentioned in my previous post that QImage is also a QPaintDevice, so an exercise I'm leaving to you is to make 3rd version of the code that will render widgets to an image.