30using namespace Qt::StringLiterals;
36QStringList QgsLayoutAtlasToPdfAlgorithmBase::tags()
const
38 return QObject::tr(
"layout,atlas,composer,composition,save" ).split(
',' );
41QString QgsLayoutAtlasToPdfAlgorithmBase::group()
const
43 return QObject::tr(
"Cartography" );
46QString QgsLayoutAtlasToPdfAlgorithmBase::groupId()
const
48 return u
"cartography"_s;
56void QgsLayoutAtlasToPdfAlgorithmBase::initAlgorithm(
const QVariantMap & )
63 addParameter(
new QgsProcessingParameterBoolean( u
"SORTBY_REVERSE"_s, QObject::tr(
"Reverse sort order (used when a sort expression is provided)" ),
false ) );
65 auto layersParam = std::make_unique<QgsProcessingParameterMultipleLayers>( u
"LAYERS"_s, QObject::tr(
"Map layers to assign to unlocked map item(s)" ),
Qgis::ProcessingSourceType::MapLayer, QVariant(),
true );
67 addParameter( layersParam.release() );
71 addParameter( dpiParam.release() );
73 auto forceVectorParam = std::make_unique<QgsProcessingParameterBoolean>( u
"FORCE_VECTOR"_s, QObject::tr(
"Always export as vectors" ),
false );
75 addParameter( forceVectorParam.release() );
77 auto forceRasterParam = std::make_unique<QgsProcessingParameterBoolean>( u
"FORCE_RASTER"_s, QObject::tr(
"Always export as raster" ),
false );
79 addParameter( forceRasterParam.release() );
81 auto appendGeorefParam = std::make_unique<QgsProcessingParameterBoolean>( u
"GEOREFERENCE"_s, QObject::tr(
"Append georeference information" ),
true );
83 addParameter( appendGeorefParam.release() );
85 auto exportRDFParam = std::make_unique<QgsProcessingParameterBoolean>( u
"INCLUDE_METADATA"_s, QObject::tr(
"Export RDF metadata (title, author, etc.)" ),
true );
87 addParameter( exportRDFParam.release() );
89 auto disableTiled = std::make_unique<QgsProcessingParameterBoolean>( u
"DISABLE_TILED"_s, QObject::tr(
"Disable tiled raster layer exports" ),
false );
91 addParameter( disableTiled.release() );
93 auto simplify = std::make_unique<QgsProcessingParameterBoolean>( u
"SIMPLIFY"_s, QObject::tr(
"Simplify geometries to reduce output file size" ),
true );
95 addParameter( simplify.release() );
97 const QStringList textExportOptions {
98 QObject::tr(
"Always Export Text as Paths (Recommended)" ),
99 QObject::tr(
"Always Export Text as Text Objects" ),
100 QObject::tr(
"Prefer Exporting Text as Text Objects" ),
103 auto textFormat = std::make_unique<QgsProcessingParameterEnum>( u
"TEXT_FORMAT"_s, QObject::tr(
"Text export" ), textExportOptions,
false, 0 );
105 addParameter( textFormat.release() );
107 const QStringList imageCompressionOptions {
108 QObject::tr(
"Lossy (JPEG)" ),
109 QObject::tr(
"Lossless" )
112 auto imageCompression = std::make_unique<QgsProcessingParameterEnum>( u
"IMAGE_COMPRESSION"_s, QObject::tr(
"Image compression" ), imageCompressionOptions,
false, 0 );
114 addParameter( imageCompression.release() );
120 QgsPrintLayout *l = parameterAsLayout( parameters, u
"LAYOUT"_s, context );
122 throw QgsProcessingException( QObject::tr(
"Cannot find layout with name \"%1\"" ).arg( parameters.value( u
"LAYOUT"_s ).toString() ) );
124 std::unique_ptr<QgsPrintLayout> layout( l->
clone() );
127 QString expression, error;
128 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, u
"COVERAGE_LAYER"_s, context );
134 expression = parameterAsString( parameters, u
"FILTER_EXPRESSION"_s, context );
137 if ( !expression.isEmpty() && !error.isEmpty() )
143 expression = parameterAsString( parameters, u
"SORTBY_EXPRESSION"_s, context );
144 if ( !expression.isEmpty() )
146 const bool sortByReverse = parameterAsBool( parameters, u
"SORTBY_REVERSE"_s, context );
158 throw QgsProcessingException( QObject::tr(
"Selected layout does not have atlas functionality enabled" ) );
164 if ( parameters.value( u
"DPI"_s ).isValid() )
166 settings.
dpi = parameterAsDouble( parameters, u
"DPI"_s, context );
168 settings.
forceVectorOutput = parameterAsBool( parameters, u
"FORCE_VECTOR"_s, context );
170 settings.
appendGeoreference = parameterAsBool( parameters, u
"GEOREFERENCE"_s, context );
171 settings.
exportMetadata = parameterAsBool( parameters, u
"INCLUDE_METADATA"_s, context );
173 const int textFormat = parameterAsEnum( parameters, u
"TEXT_FORMAT"_s, context );
174 switch ( textFormat )
189 if ( parameterAsBool( parameters, u
"DISABLE_TILED"_s, context ) )
194 if ( parameterAsEnum( parameters, u
"IMAGE_COMPRESSION"_s, context ) == 1 )
201 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
202 if ( layers.size() > 0 )
204 const QList<QGraphicsItem *> items = layout->items();
205 for ( QGraphicsItem *graphicsItem : items )
217 return exportAtlas( atlas, exporter, settings, parameters, context, feedback );
224QString QgsLayoutAtlasToPdfAlgorithm::name()
const
226 return u
"atlaslayouttopdf"_s;
229QString QgsLayoutAtlasToPdfAlgorithm::displayName()
const
231 return QObject::tr(
"Export atlas layout as PDF (single file)" );
234QString QgsLayoutAtlasToPdfAlgorithm::shortDescription()
const
236 return QObject::tr(
"Exports an atlas layout as a single PDF file." );
239QString QgsLayoutAtlasToPdfAlgorithm::shortHelpString()
const
241 return QObject::tr(
"This algorithm outputs an atlas layout as a single PDF file.\n\n"
242 "If a coverage layer is set, the selected layout's atlas settings exposed in this algorithm "
243 "will be overwritten. In this case, an empty filter or sort by expression will turn those "
247void QgsLayoutAtlasToPdfAlgorithm::initAlgorithm(
const QVariantMap & )
249 QgsLayoutAtlasToPdfAlgorithmBase::initAlgorithm();
253QgsLayoutAtlasToPdfAlgorithm *QgsLayoutAtlasToPdfAlgorithm::createInstance()
const
255 return new QgsLayoutAtlasToPdfAlgorithm();
262 const QString dest = parameterAsFileOutput( parameters, u
"OUTPUT"_s, context );
267 feedback->
pushInfo( QObject::tr(
"Exporting %n atlas feature(s)",
"", atlas->
count() ) );
272 feedback->
pushInfo( QObject::tr(
"Successfully exported atlas to %1" ).arg( QDir::toNativeSeparators( dest ) ) );
277 throw QgsProcessingException( !error.isEmpty() ? error : QObject::tr(
"Cannot write to %1.\n\nThis file may be open in another application." ).arg( QDir::toNativeSeparators( dest ) ) );
280 throw QgsProcessingException( !error.isEmpty() ? error : QObject::tr(
"Could not create print device." ) );
284 "resulted in a memory overflow.\n\n"
285 "Please try a lower resolution or a smaller paper size." ) );
288 throw QgsProcessingException( !error.isEmpty() ? error : QObject::tr(
"Error encountered while exporting atlas." ) );
298 feedback->
reportError( QObject::tr(
"No atlas features found" ) );
304 outputs.insert( u
"OUTPUT"_s, dest );
312QString QgsLayoutAtlasToMultiplePdfAlgorithm::name()
const
314 return u
"atlaslayouttomultiplepdf"_s;
317QString QgsLayoutAtlasToMultiplePdfAlgorithm::displayName()
const
319 return QObject::tr(
"Export atlas layout as PDF (multiple files)" );
322QString QgsLayoutAtlasToMultiplePdfAlgorithm::shortDescription()
const
324 return QObject::tr(
"Exports an atlas layout to multiple PDF files." );
327QString QgsLayoutAtlasToMultiplePdfAlgorithm::shortHelpString()
const
329 return QObject::tr(
"This algorithm outputs an atlas layout to multiple PDF files.\n\n"
330 "If a coverage layer is set, the selected layout's atlas settings exposed in this algorithm "
331 "will be overwritten. In this case, an empty filter or sort by expression will turn those "
336void QgsLayoutAtlasToMultiplePdfAlgorithm::initAlgorithm(
const QVariantMap & )
338 QgsLayoutAtlasToPdfAlgorithmBase::initAlgorithm();
343QgsLayoutAtlasToMultiplePdfAlgorithm *QgsLayoutAtlasToMultiplePdfAlgorithm::createInstance()
const
345 return new QgsLayoutAtlasToMultiplePdfAlgorithm();
354 const QString filename = parameterAsString( parameters, u
"OUTPUT_FILENAME"_s, context );
355 const QString destFolder = parameterAsFile( parameters, u
"OUTPUT_FOLDER"_s, context );
358 const QString dest = u
"%1/atlas.pdf"_s.arg( destFolder );
362 feedback->
pushInfo( QObject::tr(
"Exporting %n atlas feature(s)",
"", atlas->
count() ) );
369 else if ( !filename.isEmpty() )
373 throw QgsProcessingException( QObject::tr(
"Output file name expression is not valid: %1" ).arg( error ) );
383 feedback->
pushInfo( QObject::tr(
"Successfully exported atlas to %1" ).arg( QDir::toNativeSeparators( destFolder ) ) );
388 throw QgsProcessingException( !error.isEmpty() ? error : QObject::tr(
"Cannot write to %1.\n\nThis file may be open in another application." ).arg( QDir::toNativeSeparators( dest ) ) );
391 throw QgsProcessingException( !error.isEmpty() ? error : QObject::tr(
"Could not create print device." ) );
395 "resulted in a memory overflow.\n\n"
396 "Please try a lower resolution or a smaller paper size." ) );
399 throw QgsProcessingException( !error.isEmpty() ? error : QObject::tr(
"Error encountered while exporting atlas." ) );
409 feedback->
reportError( QObject::tr(
"No atlas features found" ) );
415 outputs.insert( u
"OUTPUT_FOLDER"_s, destFolder );
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer).
@ Folder
Parameter is a folder.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ PreferText
Render text as text objects, unless doing so results in rendering artifacts or poor quality rendering...
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
@ AlwaysText
Always render text as text objects. While this mode preserves text objects as text for post-processin...
@ NoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
@ DisableTiledRasterLayerRenders
If set, then raster layers will not be drawn as separate tiles. This may improve the appearance in ex...
@ LosslessImageRendering
Render images losslessly whenever possible, instead of the default lossy jpeg rendering used for some...
void setProgress(double progress)
Sets the current progress for the feedback object.
Used to render QgsLayout as an atlas, by iterating over the features from an associated vector layer.
QString filenameExpression() const
Returns the filename expression used for generating output filenames for each atlas page.
void setCoverageLayer(QgsVectorLayer *layer)
Sets the coverage layer to use for the atlas features.
bool setFilterExpression(const QString &expression, QString &errorString)
Sets the expression used for filtering features in the coverage layer.
void setSortAscending(bool ascending)
Sets whether features should be sorted in an ascending order.
void setEnabled(bool enabled)
Sets whether the atlas is enabled.
bool enabled() const
Returns whether the atlas generation is enabled.
bool setFilenameExpression(const QString &expression, QString &errorString)
Sets the filename expression used for generating output filenames for each atlas page.
void setSortFeatures(bool enabled)
Sets whether features should be sorted in the atlas.
int count() const override
Returns the number of features to iterate over.
void setSortExpression(const QString &expression)
Sets the expression (or field name) to use for sorting features.
void setFilterFeatures(bool filtered)
Sets whether features should be filtered in the coverage layer.
int updateFeatures()
Requeries the current atlas coverage layer and applies filtering and sorting.
Handles rendering and exports of layouts to various formats.
ExportResult exportToPdf(const QString &filePath, const QgsLayoutExporter::PdfExportSettings &settings)
Exports the layout as a PDF to the filePath, using the specified export settings.
ExportResult
Result codes for exporting layouts.
@ Canceled
Export was canceled.
@ MemoryError
Unable to allocate memory required to export.
@ PrintError
Could not start printing to destination device.
@ IteratorError
Error iterating over layout.
@ FileError
Could not write to destination file, likely due to a lock held by another application.
@ Success
Export was successful.
@ SvgLayerError
Could not create layered SVG file.
static ExportResult exportToPdfs(QgsAbstractLayoutIterator *iterator, const QString &baseFilePath, const QgsLayoutExporter::PdfExportSettings &settings, QString &error, QgsFeedback *feedback=nullptr)
Exports a layout iterator to multiple PDF files, with the specified export settings.
Layout graphical items for displaying a map.
bool keepLayerSet() const
Returns whether a stored layer set should be used or the current layer set from the project associate...
void setKeepLayerSet(bool enabled)
Sets whether the stored layer set should be used or the current layer set of the associated project.
bool followVisibilityPreset() const
Returns whether the map should follow a map theme.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the stored layers set.
Base class for graphical items within a QgsLayout.
static QVector< double > predefinedScales(const QgsLayout *layout)
Returns a list of predefined scales associated with a layout.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
QgsPrintLayout * clone() const override
Creates a clone of the layout.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean parameter for processing algorithms.
An expression parameter for processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
An input file or folder parameter for processing algorithms.
A print layout parameter, allowing users to select a print layout.
A vector layer (with or without geometry) parameter for processing algorithms.
Represents a vector layer which manages a vector based dataset.
Contains settings relating to exporting layouts to PDF.
bool forceVectorOutput
Set to true to force vector object exports, even when the resultant appearance will differ from the l...
bool rasterizeWholeImage
Set to true to force whole layout to be rasterized while exporting.
bool exportMetadata
Indicates whether PDF export should include metadata generated from the layout's project's metadata.
bool appendGeoreference
Indicates whether PDF export should append georeference data.
Qgis::LayoutRenderFlags flags
Layout context flags, which control how the export will be created.
double dpi
Resolution to export layout at. If dpi <= 0 the default layout dpi will be used.
QVector< qreal > predefinedMapScales
A list of predefined scales to use with the layout.
bool simplifyGeometries
Indicates whether vector geometries should be simplified to avoid redundant extraneous detail,...
Qgis::TextRenderFormat textRenderFormat
Text rendering format, which controls how text should be rendered in the export (e....