TransWikia.com

exportToPdf doesn't export the layout in QGIS 3

Geographic Information Systems Asked by Radka on May 31, 2021

I have a trouble exporting a print layout in QGIS 3.10 to PDF when doing it programatically. I’ve created a project with print layout manually and now just want to modify the content of the map with python and then save it as PDF. This is the code I tried for export, everything works just fine except the last line, which does nothing and ends with code 4.

manager = QgsProject.instance().layoutManager()
layout = manager.layoutByName("my_layout")
exporter = QgsLayoutExporter(layout)
filename = "C://layout.pdf"
#so far so good, but this doesnt create the file:
exporter.exportToPdf(filename, QgsLayoutExporter.PdfExportSettings()) 

Any idea what could be wrong? When I run the export of the same layout manually, it works fine.

One Answer

Code 4 means Could not start printing to destination device (Reference). Probably, Windows does not allow you to write files to the root directory (C:).

Make a new directory (Test) in C:, then export the layout into this directory.

filename = "C:/test/layout.pdf" # use / or  instead of //    
exporter.exportToPdf(filename, QgsLayoutExporter.PdfExportSettings()) 

Correct answer by Kadir Şahbaz on May 31, 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