It would seem that I am using the wrong properties setting if I had to guess. Anyhow, I have a layout template in a project that I load using python. Within the project canvas, I can select a set of polygon features and zoom the layout map to those features. When I export the layout as a pdf the selected polygons don't show up as selected.

Is there a setting (as in the title above) that can turn layout rendering on for the exported pdf?

Here is the line I've used:

settings = iface.mapCanvas().mapSettings()

This does nothing...am I actually setting the flag for the map canvas which isn't picked up by the layout map item?


You should use flags property of QgsLayoutExporter.PdfExportSettings struct.

layout_manager = QgsProject.instance().layoutManager()
layout = layout_manager.layoutByName('LAYOUT_NAME') ## add your layout name

pdf_settings = QgsLayoutExporter.PdfExportSettings()
pdf_settings.flags = QgsLayoutRenderContext.FlagDrawSelection  # <<--

# specify other pdf_setting attributes
# pdf_settings.??? = ???

path = 'path/to/file.pdf' ## add a proper path
layout_exporter = QgsLayoutExporter(layout)
layout_exporter.exportToPdf(path, pdf_settings)

You may need to add other flags, because (I guess) pdf_settings.flags = .. resets the other flags.

A screenshot taken from pdf (yellow ones are the selected features in QGIS):

enter image description here

  • Thanks for that. It works fine. I'm a bit familiar with programming but am really struggling to understand the API uses in Qgis. How do I set multiple flags at once, or just one flag without messing up the other flags? I can see there is bit shifting, but am not having any luck "or-ing" them together. I found this (void setFlag QgsLayoutRenderContext::Flag flag, bool on=true) in the QgsLayoutRenderContext API reference, but how do I use it? I've tried a thousand ways and keep just getting syntax errors. I'm gonna have nightmares...lol. Any API use totorials that you know of? – Bryan Taylor Nov 28 '20 at 5:30
  • Figured out setting multiple flags...I struggle my friends, but here is one way I found to set multiple flags at once: pdf_settings.flags = QgsLayoutRenderContext.Flag(QgsLayoutRenderContext.FlagDrawSelection| \ QgsLayoutRenderContext.FlagForceVectorOutput| \ QgsLayoutRenderContext.FlagAntialiasing| \ QgsLayoutRenderContext.FlagDebug) – Bryan Taylor Nov 28 '20 at 23:00
  • I didn't have much time to answer your question today. :( – Kadir Şahbaz Nov 28 '20 at 23:03
  • Sahbaz No worries, you're a champ my friend. – Bryan Taylor Nov 28 '20 at 23:06

Unable to find how to set the proper flag under the correct property so that a rendered pdf from a layout will show the selected polygons in a vector layer, I offer this solution NOT using a layout property, but the vector layer properties. This might not be ideal in all situation, but it works well for my needs here:

enter image description here

You can see the symbol and rule that I added, the actual rule "is_selected" is highlighted in yellow. To add a rule you click the "+" symbol at the bottom of the symbology window. You can see that I have already added the "is_selected()" rule to my symbology. It will change any selected feature in the vector layer to that symbology...which will also show up in the layout and be exported in the pdf. It's not pyqgis based, but it could be done in python without much trouble. When you click the "+" button to add a symbology level you are presented with following window:

enter image description here

In the window above, clicking the highlighted "formula" button will bring up the next window where you can design your rule. Fortunately, the Qgis team has a very robust set of pre-built rules/properties, so we just have to double click the one needed (circled in red -you'll have to manually add the closing parenthesis after double clicking):

enter image description here

Hit the "OK" button at the bottom of the window and your symbology rule will be added.

If anyone has a solution that just turns on printing selected features in their selection colour within the layout properties, I'd still be very much interested in that solution also. I actually stumbled across this answer looking at atlas properties here, which gave me the idea: https://blog.samaritakis.gr/en/highlight-features-printing-atlas-print-composer/

  • This is a nice trick. Please also check my answer. – Kadir Şahbaz Nov 28 '20 at 0:43
  • Yeah, I can definitely see this being very handy if there are different reasons (multiple selections) in play for a given layer, or if highlights need address management or policy priorities. Cheers mate. – Bryan Taylor Nov 28 '20 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.