|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmlayouttopdf.cpp |
| 3 | + --------------------- |
| 4 | + begin : June 2020 |
| 5 | + copyright : (C) 2020 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsalgorithmlayouttopdf.h" |
| 19 | +#include "qgslayout.h" |
| 20 | +#include "qgsprintlayout.h" |
| 21 | +#include "qgsprocessingoutputs.h" |
| 22 | +#include "qgslayoutexporter.h" |
| 23 | + |
| 24 | +///@cond PRIVATE |
| 25 | + |
| 26 | +QString QgsLayoutToPdfAlgorithm::name() const |
| 27 | +{ |
| 28 | + return QStringLiteral( "printlayouttopdf" ); |
| 29 | +} |
| 30 | + |
| 31 | +QString QgsLayoutToPdfAlgorithm::displayName() const |
| 32 | +{ |
| 33 | + return QObject::tr( "Export print layout as PDF" ); |
| 34 | +} |
| 35 | + |
| 36 | +QStringList QgsLayoutToPdfAlgorithm::tags() const |
| 37 | +{ |
| 38 | + return QObject::tr( "layout,composer,composition,save" ).split( ',' ); |
| 39 | +} |
| 40 | + |
| 41 | +QString QgsLayoutToPdfAlgorithm::group() const |
| 42 | +{ |
| 43 | + return QObject::tr( "Cartography" ); |
| 44 | +} |
| 45 | + |
| 46 | +QString QgsLayoutToPdfAlgorithm::groupId() const |
| 47 | +{ |
| 48 | + return QStringLiteral( "cartography" ); |
| 49 | +} |
| 50 | + |
| 51 | +QString QgsLayoutToPdfAlgorithm::shortDescription() const |
| 52 | +{ |
| 53 | + return QObject::tr( "Exports a print layout as a PDF." ); |
| 54 | +} |
| 55 | + |
| 56 | +QString QgsLayoutToPdfAlgorithm::shortHelpString() const |
| 57 | +{ |
| 58 | + return QObject::tr( "This algorithm outputs a print layout as a PDF file." ); |
| 59 | +} |
| 60 | + |
| 61 | +void QgsLayoutToPdfAlgorithm::initAlgorithm( const QVariantMap & ) |
| 62 | +{ |
| 63 | + addParameter( new QgsProcessingParameterLayout( QStringLiteral( "LAYOUT" ), QObject::tr( "Print layout" ) ) ); |
| 64 | + |
| 65 | + std::unique_ptr< QgsProcessingParameterNumber > dpiParam = qgis::make_unique< QgsProcessingParameterNumber >( QStringLiteral( "DPI" ), QObject::tr( "DPI (leave blank for default layout DPI)" ), QgsProcessingParameterNumber::Double, QVariant(), true, 0 ); |
| 66 | + dpiParam->setFlags( dpiParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 67 | + addParameter( dpiParam.release() ); |
| 68 | + |
| 69 | + std::unique_ptr< QgsProcessingParameterBoolean > forceVectorParam = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "FORCE_VECTOR" ), QObject::tr( "Always export as vectors" ), false ); |
| 70 | + forceVectorParam->setFlags( forceVectorParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 71 | + addParameter( forceVectorParam.release() ); |
| 72 | + |
| 73 | + std::unique_ptr< QgsProcessingParameterBoolean > appendGeorefParam = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "GEOREFERENCE" ), QObject::tr( "Append georeference information" ), true ); |
| 74 | + appendGeorefParam->setFlags( appendGeorefParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 75 | + addParameter( appendGeorefParam.release() ); |
| 76 | + |
| 77 | + std::unique_ptr< QgsProcessingParameterBoolean > exportRDFParam = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "INCLUDE_METADATA" ), QObject::tr( "Export RDF metadata (title, author, etc.)" ), true ); |
| 78 | + exportRDFParam->setFlags( exportRDFParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 79 | + addParameter( exportRDFParam.release() ); |
| 80 | + |
| 81 | + std::unique_ptr< QgsProcessingParameterBoolean > disableTiled = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "DISABLE_TILED" ), QObject::tr( "Disable tiled raster layer exports" ), false ); |
| 82 | + disableTiled->setFlags( disableTiled->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 83 | + addParameter( disableTiled.release() ); |
| 84 | + |
| 85 | + std::unique_ptr< QgsProcessingParameterBoolean > simplify = qgis::make_unique< QgsProcessingParameterBoolean >( QStringLiteral( "SIMPLIFY" ), QObject::tr( "Simplify geometries to reduce output file size" ), true ); |
| 86 | + simplify->setFlags( simplify->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 87 | + addParameter( simplify.release() ); |
| 88 | + |
| 89 | + QStringList textExportOptions |
| 90 | + { |
| 91 | + QObject::tr( "Always Export Text as Paths (Recommended" ), |
| 92 | + QObject::tr( "Always Export Text as Text Objects" ) |
| 93 | + }; |
| 94 | + |
| 95 | + std::unique_ptr< QgsProcessingParameterEnum > textFormat = qgis::make_unique< QgsProcessingParameterEnum >( QStringLiteral( "TEXT_FORMAT" ), QObject::tr( "Text export" ), textExportOptions, false, 0 ); |
| 96 | + textFormat->setFlags( textFormat->flags() | QgsProcessingParameterDefinition::FlagAdvanced ); |
| 97 | + addParameter( textFormat.release() ); |
| 98 | + |
| 99 | + addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "PDF file" ), QObject::tr( "PDF Format" ) + " (*.pdf *.PDF)" ) ); |
| 100 | +} |
| 101 | + |
| 102 | +QgsProcessingAlgorithm::Flags QgsLayoutToPdfAlgorithm::flags() const |
| 103 | +{ |
| 104 | + return QgsProcessingAlgorithm::flags() | FlagNoThreading; |
| 105 | +} |
| 106 | + |
| 107 | +QgsLayoutToPdfAlgorithm *QgsLayoutToPdfAlgorithm::createInstance() const |
| 108 | +{ |
| 109 | + return new QgsLayoutToPdfAlgorithm(); |
| 110 | +} |
| 111 | + |
| 112 | +QVariantMap QgsLayoutToPdfAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) |
| 113 | +{ |
| 114 | + // this needs to be done in main thread, layouts are not thread safe |
| 115 | + QgsPrintLayout *layout = parameterAsLayout( parameters, QStringLiteral( "LAYOUT" ), context ); |
| 116 | + if ( !layout ) |
| 117 | + throw QgsProcessingException( QObject::tr( "Cannot find layout with name \"%1\"" ).arg( parameters.value( QStringLiteral( "LAYOUT" ) ).toString() ) ); |
| 118 | + |
| 119 | + const QString dest = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context ); |
| 120 | + |
| 121 | + QgsLayoutExporter exporter( layout ); |
| 122 | + QgsLayoutExporter::PdfExportSettings settings; |
| 123 | + |
| 124 | + if ( parameters.value( QStringLiteral( "DPI" ) ).isValid() ) |
| 125 | + { |
| 126 | + settings.dpi = parameterAsDouble( parameters, QStringLiteral( "DPI" ), context ); |
| 127 | + } |
| 128 | + |
| 129 | + settings.forceVectorOutput = parameterAsBool( parameters, QStringLiteral( "FORCE_VECTOR" ), context ); |
| 130 | + settings.appendGeoreference = parameterAsBool( parameters, QStringLiteral( "GEOREFERENCE" ), context ); |
| 131 | + settings.exportMetadata = parameterAsBool( parameters, QStringLiteral( "INCLUDE_METADATA" ), context ); |
| 132 | + settings.exportMetadata = parameterAsBool( parameters, QStringLiteral( "INCLUDE_METADATA" ), context ); |
| 133 | + settings.simplifyGeometries = parameterAsBool( parameters, QStringLiteral( "SIMPLIFY" ), context ); |
| 134 | + settings.textRenderFormat = parameterAsEnum( parameters, QStringLiteral( "TEXT_FORMAT" ), context ) == 0 ? QgsRenderContext::TextFormatAlwaysOutlines : QgsRenderContext::TextFormatAlwaysText; |
| 135 | + |
| 136 | + if ( parameterAsBool( parameters, QStringLiteral( "DISABLE_TILED" ), context ) ) |
| 137 | + settings.flags = settings.flags | QgsLayoutRenderContext::FlagDisableTiledRasterLayerRenders; |
| 138 | + else |
| 139 | + settings.flags = settings.flags & ~QgsLayoutRenderContext::FlagDisableTiledRasterLayerRenders; |
| 140 | + |
| 141 | + switch ( exporter.exportToPdf( dest, settings ) ) |
| 142 | + { |
| 143 | + case QgsLayoutExporter::Success: |
| 144 | + { |
| 145 | + feedback->pushInfo( QObject::tr( "Successfully exported layout to %1" ).arg( QDir::toNativeSeparators( dest ) ) ); |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + case QgsLayoutExporter::FileError: |
| 150 | + throw QgsProcessingException( QObject::tr( "Cannot write to %1.\n\nThis file may be open in another application." ).arg( QDir::toNativeSeparators( dest ) ) ); |
| 151 | + |
| 152 | + case QgsLayoutExporter::PrintError: |
| 153 | + throw QgsProcessingException( QObject::tr( "Could not create print device." ) ); |
| 154 | + |
| 155 | + case QgsLayoutExporter::MemoryError: |
| 156 | + throw QgsProcessingException( QObject::tr( "Exporting the PDF " |
| 157 | + "resulted in a memory overflow.\n\n" |
| 158 | + "Please try a lower resolution or a smaller paper size." ) ); |
| 159 | + |
| 160 | + case QgsLayoutExporter::SvgLayerError: |
| 161 | + case QgsLayoutExporter::IteratorError: |
| 162 | + case QgsLayoutExporter::Canceled: |
| 163 | + // no meaning for PDF exports, will not be encountered |
| 164 | + break; |
| 165 | + } |
| 166 | + |
| 167 | + feedback->setProgress( 100 ); |
| 168 | + |
| 169 | + QVariantMap outputs; |
| 170 | + outputs.insert( QStringLiteral( "OUTPUT" ), dest ); |
| 171 | + return outputs; |
| 172 | +} |
| 173 | + |
| 174 | +///@endcond |
| 175 | + |
0 commit comments