Exporting Print Layouts from Processing Scripts

When trying to automate your GIS workflows, one important step is the production of maps. Creating and exporting maps in QGIS is done via the Print Layout. One can automate creation of maps via the a rich Python API via the QgsLayout class.

It is recommended to use the Processing Framework for writing python scripts in QGIS. Here I will show you how to write a processing script that takes a saved map template and renders the current project as a PDF file.

For this example, I am assuming a simple map template with 2 items

  • A label item for the title named ‘Label 1’
  • A map item named ‘Map 1’

To use this layout as a template, we should first use Layout → Save as Template... This will save the layout configuration as a .qpt file.

Next open the Processing Toolbox and go to Scripts → Create New Script…

In the Script Editor, copy/paste the following code.

from PyQt5.QtCore import QCoreApplication
from qgis.core import (QgsProject, QgsLayout, QgsLayoutExporter, 
    QgsReadWriteContext, QgsMapSettings, QgsProcessingAlgorithm, 
    QgsProcessingParameterFileDestination, QgsProcessingParameterFile,
    QgsProcessingParameterString)
from PyQt5.QtXml import QDomDocument
class ExportLayoutAlgorithm(QgsProcessingAlgorithm):
    """Exports the current map view to PDF"""
    TEMPLATE = 'TEMPLATE'
    OUTPUT = 'OUTPUT'
    TITLE = 'TITLE'
    
    def flags(self):
          return super().flags() | QgsProcessingAlgorithm.FlagNoThreading
    def initAlgorithm(self, config=None):
        self.addParameter(
            QgsProcessingParameterFile(
                self.TEMPLATE,
                self.tr('Template File'),
            )
        )
        
        self.addParameter(
            QgsProcessingParameterString(
                self.TITLE,
                self.tr('Map Title'),
                'My Map'
            )
        )
        
        # We add a file output of type CSV.
        self.addParameter(
            QgsProcessingParameterFileDestination(
                self.OUTPUT,
                self.tr('Output File'),
                'PDF File (*.pdf)',
            )
        )
    def processAlgorithm(self, parameters, context, feedback):
        template = self.parameterAsFile(parameters, 'TEMPLATE', context)
        title = self.parameterAsString(parameters, 'TITLE', context)
        pdf = self.parameterAsFileOutput(parameters, 'OUTPUT', context)
        # Load template from file
        project = context.project()
        layout = QgsLayout(project)
        layout.initializeDefaults()
        with open(template) as f:
            template_content = f.read()
        doc = QDomDocument()
        doc.setContent(template_content)
        # adding to existing items
        items, ok = layout.loadFromTemplate(doc, QgsReadWriteContext(), False)
        title_item = layout.itemById('Label 1')
        title_item.setText(title)
        exporter = QgsLayoutExporter(layout)
        exporter.exportToPdf(
             pdf, QgsLayoutExporter.PdfExportSettings())
        return {'OUTPUT': pdf}
    def name(self):
        return 'export_layout_pdf'
    def displayName(self):
        return self.tr('Export Layout As PDF')
    def group(self):
        return self.tr(self.groupId())
    def groupId(self):
        return ''
    def tr(self, string):
        return QCoreApplication.translate('Processing', string)
    def createInstance(self):
        return ExportLayoutAlgorithm()

Save the script and close the Script Editor. You will see the script appear as a new algorithm under Scripts → Export Layout as PDF

Double-click to open it. Choose the saved .qpt file as the Template File. Enter a map title of your choice and choose an output file. Click Run.

The current project will be rendered in the Map element of the template and a PDF file will be created.

Another advantage of writing our export algorithm as a processing script is that it can be used as part of a model and create maps without human input. So if you have a workflow where you need to get source data, process, analyze and produce a map – you can use scripts such as this to create a model and run it without having to click any buttons.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.