Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate a QGIS map PDF using python
# coding=utf-8
# A simple demonstration of to generate a PDF using a QGIS project
# and a QGIS layout template.
#
# This code is public domain, use if for any purpose you see fit.
# Tim Sutton 2015
import sys
from qgis.core import (
QgsProject, QgsComposition, QgsApplication, QgsProviderRegistry)
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from PyQt4.QtCore import QFileInfo
from PyQt4.QtXml import QDomDocument
gui_flag = True
app = QgsApplication(sys.argv, gui_flag)
# Make sure QGIS_PREFIX_PATH is set in your env if needed!
app.initQgis()
# Probably you want to tweak this
project_path = 'project.qgs'
# and this
template_path = 'template.qpt'
def make_pdf():
canvas = QgsMapCanvas()
# Load our project
QgsProject.instance().read(QFileInfo(project_path))
bridge = QgsLayerTreeMapCanvasBridge(
QgsProject.instance().layerTreeRoot(), canvas)
bridge.setCanvasLayers()
template_file = file(template_path)
template_content = template_file.read()
template_file.close()
document = QDomDocument()
document.setContent(template_content)
composition = QgsComposition(canvas.mapSettings())
# You can use this to replace any string like this [key]
# in the template with a new value. e.g. to replace
# [date] pass a map like this {'date': '1 Jan 2012'}
substitution_map = {
'DATE_TIME_START': 'foo',
'DATE_TIME_END': 'bar'}
composition.loadFromTemplate(document, substitution_map)
# You must set the id in the template
map_item = composition.getComposerItemById('map')
map_item.setMapCanvas(canvas)
map_item.zoomToExtent(canvas.extent())
# You must set the id in the template
legend_item = composition.getComposerItemById('legend')
legend_item.updateLegend()
composition.refreshItems()
composition.exportAsPDF('report.pdf')
QgsProject.instance().clear()
make_pdf()
@snorfalorpagus

This comment has been minimized.

Copy link

snorfalorpagus commented Jun 15, 2015

Is it possible to use a composer that is saved in the project file itself, rather than load it from a template?

@chriserik

This comment has been minimized.

Copy link

chriserik commented Aug 21, 2016

Unfortunately, this does not seem to work on QGIS 2.8.3 anymore. The map canvas stays blank. Any Ideas on what API change may have caused this? Thanks!

@qnmai

This comment has been minimized.

Copy link

qnmai commented Nov 3, 2016

I'm having the same blank map issue using QGIS 2.8.3. Maybe this is related--but a table item I have added is staying blank as well.

@taguaysa

This comment has been minimized.

Copy link

taguaysa commented Jun 21, 2017

Blank map in Qgis 2.18.9 ... it seems that composition.getComposerItemById returns nothing. Any idea?

@cm0001

This comment has been minimized.

Copy link

cm0001 commented Jun 5, 2018

Would love to see an update/example of this for QGIS3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.