There are various approaches to generate an atlas. Now (don't know since when...), there is a new processing script for this intend e.g scripting Atlas creation without complex boilerplate. The one for PDF export is native:atlaslayouttopdf and native:atlaslayouttoimage. You can see below an illustrated sample using native:atlaslayouttopdf
from qgis import processing
parameters = {
"COVERAGE_LAYER" : 'my_layer_name', # Could be also QgsVectorLayer
"DISABLE_TILED" : False,
"DPI" : None,
"FILTER_EXPRESSION" : "",
"FORCE_VECTOR" : False,
"GEOREFERENCE" : True,
"INCLUDE_METADATA" : True,
"LAYERS" : None,
"LAYOUT" : "your_layout_name", # Need your own layout
"OUTPUT" : "/tmp/atlas.pdf", # Need to change path
"SIMPLIFY" : True,
"SORTBY_EXPRESSION" : "",
"SORTBY_REVERSE" : False,
"TEXT_FORMAT" : 0
}
# Use the following (uncomment if interested) to learn about argument you can provide for each key above
# processing.algorithmHelp("native:atlaslayouttopdf")
out = processing.run("native:atlaslayouttopdf", parameters)
print(out['OUTPUT'])
To ease the pain to discover the parameters above, I recommend to run manually the processing script. Then, go to "Processing" menu and "History..." and select the algorithm to see parameters like in below screenshot. They are quite similar to the one I provided (blurry part for customer anonymity)
Alternate solution to export to image using native:atlaslayouttoimage
from qgis import processing
parameters_img = {
"ANTIALIAS" : True,
"COVERAGE_LAYER" : "my_layer_name",
"DPI" : None,
"EXTENSION" : 8, # 10 for tif
"FILENAME_EXPRESSION" : "\"output_\"||@atlas_featurenumber",
"FILTER_EXPRESSION" : "",
"FOLDER" : "/tmp/tryout", # Change directory
"GEOREFERENCE" : True,
"INCLUDE_METADATA" : True,
"LAYERS" : None,
"LAYOUT" : "your_layout_name",
"SORTBY_EXPRESSION" : "",
"SORTBY_REVERSE" : False
}
out_img = processing.run("native:atlaslayouttoimage", parameters_img)
print(out_img['OUTPUT'])
from qgis import processing
coverage_layer = iface.activeLayer() # Change the ref for your own
# Use the following (uncomment if interested) to learn about argument you can provide for each key in parameters below
# processing.algorithmHelp("native:atlaslayouttopdf")
for feature in coverage_layer.getFeatures():
parameters = {
"COVERAGE_LAYER" : coverage_layer, # Could be also QgsVectorLayer
"DISABLE_TILED" : False,
"DPI" : None,
"FILTER_EXPRESSION" : "id='{}'".format(feature['id']), #id is the key property in your feature
"FORCE_VECTOR" : False,
"GEOREFERENCE" : True,
"INCLUDE_METADATA" : True,
"LAYERS" : None,
"LAYOUT" : "rapport-basias-positif", # Need your own layout
"OUTPUT" : "/tmp/atlas_{}.pdf".format(feature['id']), # Need to change path
"SIMPLIFY" : True,
"SORTBY_EXPRESSION" : "",
"SORTBY_REVERSE" : False,
"TEXT_FORMAT" : 0
}
out = processing.run("native:atlaslayouttopdf", parameters)
print(out['OUTPUT'])
In fact, for PDF export, if you untick "Single file export when possible", QGIS will magically create for each feature a separate PDF for each feature when exporting. Work in the GUI but not tested in the processing script as OUTPUT
expect a file and not a directory