4

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?

3 Answers 3

3

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().

2
  • Thanks. I have already noticed the check box for single file export when possible.Although it was already activated, it doesn't work! So I followed your advice and make a list from my exports and merge them together. Even though it's working, it takes a lot of time!!
    – Setare
    Commented Jun 17, 2021 at 15:58
  • It takes a lot of time because you have to do multiple step or it just take a lot of time to process ? If you have a one step solution may be add an answer to your own question could be really great for those who migth search the solution. Commented Jun 17, 2021 at 16:18
2

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()
1

I managed to have a single PDF from a multipages atlas after trying a lot with exportToPdf method.

here's the code i used:

import os
from qgis.core import QgsProject,QgsLayoutExporter

# load project
project = QgsProject.instance()
project.read('D:\\temp\\test.qgs')

# set path
base_path = ('D:\\temp\\')
pdf_filename = os.path.join(base_path, "test.pdf")

# set layout
layoutName='AtlasLayout'
layout = project.layoutManager().layoutByName(layoutName)

# construct atlas from layout object
myAtlas=layout.atlas()

# construct exporter
exporter = QgsLayoutExporter(myAtlas.layout())

exporter.exportToPdf(myAtlas, pdf_path, QgsLayoutExporter.PdfExportSettings())

My explanation so far: in QGIS API (3.22 in my case), the exportToPdf() method comes in two flavour, one with the QgsLayout/QgsPrintLayout object you passes through your 'exporter' object, second with the QgsLayoutAtlas object used by your QgsLayoutExporter.

The one you used in your loop is the first one, so the method is not set to iterate on your atlas but throws a layout, you've had to set for each feature you iterate on:

exporter.exportToPdf("c:/temp/"+comp.atlas().currentFilename()+".pdf", settingspdf)

The second instead uses your atlas object which provide the required iterator and would look like smg close to:

exporter.exportToPdf(layout.atlas(), your_pdf_filename, settingspdf)

Link to related QGIS API doc: https://api.qgis.org/api/3.22/classQgsLayoutExporter.html#a0d5bdecfc7e15618c1d52b3184b53215

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.