1

In a python Qgis plugin, with this code i create a pdf file, exported from a template generated with "print composer" tool:

    myMapRenderer = self.iface.mapCanvas().mapRenderer()
    # Load template from file
    myComposition = QgsComposition(myMapRenderer)
    myFile = "/...mypath.../pdfTemplate.qpt"
    myTemplateFile = file(myFile, 'rt')
    myTemplateContent = myTemplateFile.read()
    myTemplateFile.close()
    myDocument = QDomDocument()
    myDocument.setContent(myTemplateContent)
    myComposition.loadFromTemplate(myDocument)
    printer = QPrinter()
    printer.setOutputFormat(QPrinter.PdfFormat)
    printer.setOutputFileName("/...myoutputpath.../out.pdf")
    printer.setColorMode(QPrinter.Color)
    printer.setFromTo(0, 0) # convention for whole document
    pdfPainter = QPainter(printer)
    paperRectMM = printer.pageRect(QPrinter.Millimeter)
    paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
    myComposition.setNumPages(3)
    myComposition.render(pdfPainter, paperRectPixel, paperRectMM)
    pdfPainter.end()

I do not understand why the template is composed of 3 pages but the generated pdf file contains only one, the first one.

odd thing: the number of pages of the composition is 3

print myComposition.numPages() --> output 3

Which command is missing to export all pages of the document?

Is it maybe a bug of the library?

3
  • Have you attempted to explicitly specify the 3 pages, such as "printer.setFromTo(1, 3)", rather than "printer.setFromTo(0, 0)"? I understand that "printer.setFromTo(0, 0)" should work to specify the whole document, but it's worth a shot.
    – JohnR
    Commented Apr 21, 2017 at 16:33
  • I have tried different combinations of values for "printer.setFromTo" but always export only the first page...
    – Luca Guida
    Commented Apr 27, 2017 at 9:23
  • @LucaGuida - Click the green faded tick next to your answer to show your question has been solved instead of editing it into the title :)
    – Joseph
    Commented Apr 28, 2017 at 14:01

1 Answer 1

3

Solved by replacing

pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
myComposition.setNumPages(3)
myComposition.render(pdfPainter, paperRectPixel, paperRectMM)
pdfPainter.end()

with:

pdfPainter = QPainter(printer)
myComposition.doPrint(printer, pdfPainter)
pdfPainter.end()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.