4

I have a problem with exporting of a QGIS layout by the pyQGIS. I have prepared the qgz project and also the qpt layout.

When I try to export the layout to PDF by QgsLayoutExporter.exportToPdf by calling python via the CMD, the resulting PDF is not prepared correctly (it has no map and scale bar is corrupted).This error is printed to the CMD window:

ERROR 4: `path\to\output\directory\test.pdf' not recognized as a supported file format.

When I copy and paste the same code into the QGIS python console, resulting PDF is prepared correctly.

I tried the solution on Windows 10, QGIS 3.4 / 3.6 and on Ubuntu 18, QGIS 3.6. The behaviour is the same.

What am I doing wrong or is there a bug in the pyQGIS?

Here is my source code:

import sys, os
from qgis.core import *
from qgis.PyQt.QtXml import QDomDocument
from PyQt5.QtCore import *
from PyQt5.QtXml import *

os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = r'path\to\Qt5\plugins'
os.environ['PATH'] += r';path\to\apps\qgis\bin;path\to\apps\Qt5\bin'

# supply path to qgis install location
QgsApplication.setPrefixPath(r"path\to\apps\qgis", True)

# create a reference to the QgsApplication
# setting the second argument to True enables the GUI, which we need
# to do since this is a custom application

qgs = QgsApplication([], False)

# load providers
qgs.initQgis()

# Write your code here to load some layers, use processing
# algorithms, etc.

projectInstance = QgsProject.instance()
projectInstance.read(r'path\to\QGIS\project.qgz')

layout = QgsProject.instance().layoutManager().layoutByName("overview")
pdfPath = os.path.join('path\to\output\directory', 'test.pdf')
exporter = QgsLayoutExporter(layout)

imageSettings = QgsLayoutExporter.PdfExportSettings()
imageSettings.dpi = 300
result = exporter.exportToPdf(pdfPath, imageSettings)
print(result)
print(exporter.errorFile())

# When your script is complete, call exitQgis() to remove the
# provider and layer registries from memory
qgs.exitQgis()

This is the output prepared executing the code in the QGIS Python console: enter image description here

This is the output prepared executing the code in the CMD: enter image description here

  • 1
    your code works without problems.Tested on Windows 10 – Fran Raga May 11 at 17:21
  • 1
    I just ran into the same problem. Any word on a solution? @Fran Raga: any thoughts on why it would not be working? – cbrannin May 30 at 21:46
  • Nope, I can't reproduce it. It works for me. – Fran Raga May 31 at 7:28
  • Is the use of "path\to" intentional in your script? I would expect it to be something like 'C:/OSGeo4W64/' and other Windows folder locations. – Michel Stuyts Jun 6 at 11:43
  • I'm having the same problem with qgis 3.4.7 on Ubuntu 18.4 – adegolyer Jun 6 at 18:22
0

My problem was caused by using the wrong value in QgsApplication.setPrefixPath. Having the wrong value caused qgis to not be able to render any map layers, hence the blank layouts.

On my ubuntu install the correct setting is below:

QgsApplication.setPrefixPath("/usr", True)

having it set to '/usr/bin/qgis' caused the export to fail.

The answer to this post was very helpful Failed to create memory layers in QGIS application on Linux

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.