3

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

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 Jun 17 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. – Corentin Lemaitre Jun 17 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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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