Layout export from stand-alone PyQGIS script shows empty map

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)

qgs.initQgis()

# 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)
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:

This is the output prepared executing the code in the CMD:

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

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 Jan 10 at 17:55
• I would try the answer posted here gis.stackexchange.com/questions/155745/… – adegolyer Jan 14 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 Jan 14 at 14:10