TransWikia.com

Exporting multipages Atlas with predefined layout in one PDF file in QGIS 3.x

Geographic Information Systems Asked by Setare on June 27, 2021

Exporting multipages Atlas with predefined layout in one PDF file in QGIS 3.x
I want to get a PDF export of my atlas in only one PDF File in QGIS 3.16. I’ve written the below code so far with the help of this previously answered question: PyQGIS control creation Atlas Layout

iface.addProject(myAtlasProject)
project = QgsProject.instance()
project.read(myAtlasProject)
manager = project.layoutManager()
layoutName = "Layout1"
layout = manager.layoutByName(layoutName)

for comp in manager.printLayouts():
    comp.atlas().beginRender()
    comp.atlas().first()
    for i in range(0, comp.atlas().count()):
        settingspdf = QgsLayoutExporter.PdfExportSettings()
        exporter =  QgsLayoutExporter(comp.atlas().layout () )
        comp.atlas().next()
    exporter.exportToPdf("c:/temp/"+comp.atlas().currentFilename()+".pdf", settingspdf)
comp.atlas().endRender()

Now the problem is that I get separate pages each time. However I want a single PDF export that contains all the pages. Can somebody help me with that?

2 Answers

i have a partial solution...

I know how to do it in QGIS, in atlas tab and then in output check single file export when possible. If you activate this option in your layout and then save you should be able to use it in pyqgis to export atlas in one page (i haven't tried).

enter image description here

If it doesn't work then I try to find the name of the parameter. I have saved two layouts as template with single file export when possible and without single file export when possible.

When i compare the two files, I get only one difference :

<property key="singleFile" value="true"/>

<property key="singleFile" value="false"/>

I tried to find the property singleFile in an atlas object but there is none. There is maybe a solution to do this in QGIS but if you are happy with your code I think the best will be to do the PDF merging process after even in Python if you want.

To dig deeper : I think you could regroup different QgsLayoutExporter() in a list object and then after your loop is done you do one export with exporter.exportToPdf().

Answered by Corentin Lemaitre on June 27, 2021

I found a partial solution to my question, in that I merge the sperate PDFs in one file. However, this process is very time consuming, and I hope to find a better answer within the Atlas functions.

PdfListe = []
for comp in manager.printLayouts():
    comp.atlas().beginRender()
    comp.atlas().first()
    for i in range(0, comp.atlas().count()):
        settingspdf = QgsLayoutExporter.PdfExportSettings()
        exporter =  QgsLayoutExporter(comp.atlas().layout () )
        exporter.exportToPdf("c:/temp/"+comp.atlas().currentFilename()+".pdf", settingspdf)
        PdfListe.append('C:/temp'+str(comp.atlas().currentFilename())+'.pdf')
        comp.atlas().next()
comp.atlas().endRender()
merger = PdfFileMerger()
for pdf in PdfListe:
    merger.append(PdfFileReader(pdf, 'rb'))
merger.write('c:/temp/atlas.pdf')
merger.close()

Answered by Setare on June 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP