[Qgis-developer] Printing multi-page PDFs from QgsComposition object

fegyi001 fegyi001 at gmail.com
Tue Apr 2 03:58:01 PDT 2013

Dear Developers!I'm writing a new module, in which I try to implement some
codes to export PDF files. The output looks like a simple Word document:
only text, no maps. For that, I created a QgsComposition object, and added
everything to it (as QgsComposerLabel objects) just like in case of map
compositions. The export to PDF works fine, except that I have no idea how
to tell the module to print multiple pages. I use the default A4 paper size,
and sometimes the text does not fit on a single page.I defined a variable
called "position" which starts from 0 and increases every time a new line is
added to the page. Since it is in millimeters, I can always tell when a new
page should be loaded.Here is an excerpt of my code:def savePDF(self):   
self.mapRenderer = qgis.utils.iface.mapCanvas().mapRenderer()    self.c =
QgsComposition(self.mapRenderer)    self.c.setPaperSize(210.0, 297.0)   
#here I add all the text labels to the page    self.savePDFFileName =
QtGui.QFileDialog.getSaveFileName(self.dlg, u'save as PDF', '.', 'PDF files
(*.pdf)')    self.printer = QPrinter()   
self.printer.setPaperSize(QSizeF(self.c.paperWidth(), self.c.paperHeight()),
QPrinter.Millimeter)    self.printer.setFullPage(True)   
self.printer.setPrintRange(0) #this is for printing all pages   
self.printer.setColorMode(QPrinter.Color)    self.printer.setResolution(150)   
self.pdfPainter = QPainter(self.printer)    self.paperRectMM =
self.printer.pageRect(QPrinter.Millimeter)    self.paperRectPixel =
self.c.render(self.pdfPainter, self.paperRectPixel, self.paperRectMM)   
self.pdfPainter.end()I already spent a lot of time trying to figure it out.
For example, at the QgsComposition class reference page (
http://www.qgis.org/api/classQgsComposition.html ) I found some hints:
::setNumPages() and ::addPaperItem(), but when I try to invoke them they
don't work (error message says: 'QgsComposition' object has no attribute
'addPaperItem') which means that they are not implemented yet. Is it
possible that this can only be solved when a future QGIS release comes out?
For example, the .setNumPages says: "Note: added in version 1.9."I also
tried to go through the QPrinter class reference here:
http://qt-project.org/doc/qt-4.8/qprinter.html According to it, the QPrinter
object has a .newPage() method ("Tells the printer to eject the current page
and to continue printing on a new page. Returns true if this was successful;
otherwise returns false"), but at this point it didn't solve my problem.Any
advices are highly acknowledged! I'm a python newbie...Thanks in advance,             
Gergely Padányi-Gulyás

View this message in context: http://osgeo-org.1560.n6.nabble.com/Printing-multi-page-PDFs-from-QgsComposition-object-tp5044040.html
Sent from the Quantum GIS - Developer mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130402/3fbceb3f/attachment.html>

More information about the Qgis-developer mailing list