28using namespace Qt::StringLiterals;
32QString QgsLayoutToPdfAlgorithm::name()
const
34 return u
"printlayouttopdf"_s;
37QString QgsLayoutToPdfAlgorithm::displayName()
const
39 return QObject::tr(
"Export print layout as PDF" );
42QStringList QgsLayoutToPdfAlgorithm::tags()
const
44 return QObject::tr(
"layout,composer,composition,save" ).split(
',' );
47QString QgsLayoutToPdfAlgorithm::group()
const
49 return QObject::tr(
"Cartography" );
52QString QgsLayoutToPdfAlgorithm::groupId()
const
54 return u
"cartography"_s;
57QString QgsLayoutToPdfAlgorithm::shortDescription()
const
59 return QObject::tr(
"Exports a print layout as a PDF." );
62QString QgsLayoutToPdfAlgorithm::shortHelpString()
const
64 return QObject::tr(
"This algorithm outputs a print layout as a PDF file." );
67void QgsLayoutToPdfAlgorithm::initAlgorithm(
const QVariantMap & )
71 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 );
73 addParameter( layersParam.release() );
77 addParameter( dpiParam.release() );
79 auto forceVectorParam = std::make_unique<QgsProcessingParameterBoolean>( u
"FORCE_VECTOR"_s, QObject::tr(
"Always export as vectors" ),
false );
81 addParameter( forceVectorParam.release() );
83 auto forceRasterParam = std::make_unique<QgsProcessingParameterBoolean>( u
"FORCE_RASTER"_s, QObject::tr(
"Always export as raster" ),
false );
85 addParameter( forceRasterParam.release() );
87 auto appendGeorefParam = std::make_unique<QgsProcessingParameterBoolean>( u
"GEOREFERENCE"_s, QObject::tr(
"Append georeference information" ),
true );
89 addParameter( appendGeorefParam.release() );
91 auto exportRDFParam = std::make_unique<QgsProcessingParameterBoolean>( u
"INCLUDE_METADATA"_s, QObject::tr(
"Export RDF metadata (title, author, etc.)" ),
true );
93 addParameter( exportRDFParam.release() );
95 auto disableTiled = std::make_unique<QgsProcessingParameterBoolean>( u
"DISABLE_TILED"_s, QObject::tr(
"Disable tiled raster layer exports" ),
false );
97 addParameter( disableTiled.release() );
99 auto simplify = std::make_unique<QgsProcessingParameterBoolean>( u
"SIMPLIFY"_s, QObject::tr(
"Simplify geometries to reduce output file size" ),
true );
101 addParameter( simplify.release() );
103 const QStringList textExportOptions {
104 QObject::tr(
"Always Export Text as Paths (Recommended)" ),
105 QObject::tr(
"Always Export Text as Text Objects" ),
106 QObject::tr(
"Prefer Exporting Text as Text Objects" )
109 auto textFormat = std::make_unique<QgsProcessingParameterEnum>( u
"TEXT_FORMAT"_s, QObject::tr(
"Text export" ), textExportOptions,
false, 0 );
111 addParameter( textFormat.release() );
113 const QStringList imageCompressionOptions {
114 QObject::tr(
"Lossy (JPEG)" ),
115 QObject::tr(
"Lossless" )
118 auto imageCompression = std::make_unique<QgsProcessingParameterEnum>( u
"IMAGE_COMPRESSION"_s, QObject::tr(
"Image compression" ), imageCompressionOptions,
false, 0 );
120 addParameter( imageCompression.release() );
122 auto layeredExport = std::make_unique<QgsProcessingParameterBoolean>( u
"SEPARATE_LAYERS"_s, QObject::tr(
"Export layers as separate PDF files" ),
false );
124 addParameter( layeredExport.release() );
134QgsLayoutToPdfAlgorithm *QgsLayoutToPdfAlgorithm::createInstance()
const
136 return new QgsLayoutToPdfAlgorithm();
142 QgsPrintLayout *l = parameterAsLayout( parameters, u
"LAYOUT"_s, context );
144 throw QgsProcessingException( QObject::tr(
"Cannot find layout with name \"%1\"" ).arg( parameters.value( u
"LAYOUT"_s ).toString() ) );
145 std::unique_ptr<QgsPrintLayout> layout( l->
clone() );
147 const QList<QgsMapLayer *> layers = parameterAsLayerList( parameters, u
"LAYERS"_s, context );
148 if ( layers.size() > 0 )
150 const QList<QGraphicsItem *> items = layout->items();
151 for ( QGraphicsItem *graphicsItem : items )
163 const QString dest = parameterAsFileOutput( parameters, u
"OUTPUT"_s, context );
168 if ( parameters.value( u
"DPI"_s ).isValid() )
170 settings.
dpi = parameterAsDouble( parameters, u
"DPI"_s, context );
173 settings.
forceVectorOutput = parameterAsBool( parameters, u
"FORCE_VECTOR"_s, context );
175 settings.
appendGeoreference = parameterAsBool( parameters, u
"GEOREFERENCE"_s, context );
176 settings.
exportMetadata = parameterAsBool( parameters, u
"INCLUDE_METADATA"_s, context );
178 const int textFormat = parameterAsEnum( parameters, u
"TEXT_FORMAT"_s, context );
179 switch ( textFormat )
195 if ( parameterAsBool( parameters, u
"DISABLE_TILED"_s, context ) )
200 if ( parameterAsEnum( parameters, u
"IMAGE_COMPRESSION"_s, context ) == 1 )
205 switch ( exporter.exportToPdf( dest, settings ) )
209 feedback->
pushInfo( QObject::tr(
"Successfully exported layout to %1" ).arg( QDir::toNativeSeparators( dest ) ) );
214 throw QgsProcessingException( !exporter.errorMessage().isEmpty() ? exporter.errorMessage() : QObject::tr(
"Cannot write to %1.\n\nThis file may be open in another application." ).arg( QDir::toNativeSeparators( dest ) ) );
217 throw QgsProcessingException( !exporter.errorMessage().isEmpty() ? exporter.errorMessage() : QObject::tr(
"Could not create print device." ) );
220 throw QgsProcessingException( !exporter.errorMessage().isEmpty() ? exporter.errorMessage() : QObject::tr(
"Exporting the PDF "
221 "resulted in a memory overflow.\n\n"
222 "Please try a lower resolution or a smaller paper size." ) );
234 outputs.insert( u
"OUTPUT"_s, dest );
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer).
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.
Handles rendering and exports of layouts to various formats.
@ 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.
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.
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.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A print layout parameter, allowing users to select a print layout.
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.
bool exportLayersAsSeperateFiles
true if individual layers from the layout should be rendered to separate PDF files.
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....