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

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

projectInstance = QgsProject.instance()

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)

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

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
    Commented May 11, 2019 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
    Commented May 30, 2019 at 21:46
  • Nope, I can't reproduce it. It works for me.
    – Fran Raga
    Commented May 31, 2019 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. Commented Jun 6, 2019 at 11:43
  • I'm having the same problem with qgis 3.4.7 on Ubuntu 18.4
    – adegolyer
    Commented Jun 6, 2019 at 18:22

1 Answer 1


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

  • so what's the solution for Windows?
    – Theo F
    Commented Jan 10, 2020 at 17:55
  • I would try the answer posted here gis.stackexchange.com/questions/155745/…
    – adegolyer
    Commented Jan 14, 2020 at 13:30
  • these solutions work for vector layers. But I can't seem to get any WMS layer to show as 'valid' in a standalone script. In QGIS console, I can. hmm...
    – Theo F
    Commented Jan 14, 2020 at 14:10

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.