I am using QGIS 2.18.13 and inside my plugin, I call a templet which the should create a new map as a PDF. The code I use for that is as follows:
from PyQt4.QtXml import * templet = "C:\\my_templet\\templet.qpt" template_file = file(templet, 'rt') template_content = template_file.read() template_file.close() maprender = iface.mapCanvas().mapRenderer() composition = QgsComposition(maprender) docu = QDomDocument() docu.setContent(template_content) composition.loadFromTemplate(docu) map_report = composition.getComposerMapById(0) map_report.zoomToExtent(maprender.extent()) #map_report.updateCachedImage() # added for testing did not change anyting composition.refreshItems() composition.exportAsPDF("C:\\my_folder\mypdf.pdf")
The map is created with the correct layout, but all layers, which have a rule-based styling are not shown. Neither are labels which have rule-based labeling. When changing the style to a categorized styling. The layers are shown correctly, which means the coordinatesystem is not to blame.
Reading the documentation of the QgsComposition class didn't yield any insights for me.
How to make the rule-based layers appear in my final PDF without changing them into categorized styling?
When I use the same templet to create a map manually, all layers are shown correctly and as expected.
Or I simply style according to the scale. Both cases dont show in the map.
The error with rule-based styling seems to arise from the setting of the scale. When I dont set a scale, the layer style and labels will be displayed correctly. So I assume that I miss to the composer which zoom it has.