Class: QgsLayoutExporter¶
Handles rendering and exports of layouts to various formats.
- class qgis.core.QgsLayoutExporter[source]¶
Bases:
object- __init__(layout: QgsLayout | None)
Constructor for QgsLayoutExporter, for the specified
layout.- Parameters:
layout (Optional[QgsLayout])
- __init__(a0: QgsLayoutExporter)
- Parameters:
a0 (QgsLayoutExporter)
- Canceled = 1¶
- class ExportResult¶
Bases:
int
- FileError = 3¶
- class ImageExportSettings¶
Bases:
objectContains settings relating to exporting layouts to raster images
- cropMargins: QgsMargins¶
- cropToContents: bool¶
- dpi: float¶
- exportMetadata: bool¶
- flags: Qgis.LayoutRenderFlags¶
- generateWorldFile: bool¶
- imageSize: QSize¶
- quality: int¶
- IteratorError = 6¶
- MemoryError = 2¶
- class PageExportDetails¶
Bases:
objectContains details of a page being exported by the class
- baseName: str¶
- directory: str¶
- extension: str¶
- page: int¶
- class PdfExportSettings¶
Bases:
objectContains settings relating to exporting layouts to PDF
- appendGeoreference: bool¶
- dpi: float¶
- exportLayersAsSeperateFiles: bool¶
- exportMetadata: bool¶
- flags: Qgis.LayoutRenderFlags¶
- forceVectorOutput: bool¶
- includeGeoPdfFeatures: bool¶
- rasterizeWholeImage: bool¶
- simplifyGeometries: bool¶
- textRenderFormat: Qgis.TextRenderFormat¶
- useIso32000ExtensionFormatGeoreferencing: bool¶
- useOgcBestPracticeFormatGeoreferencing: bool¶
- writeGeoPdf: bool¶
- PrintError = 4¶
- class PrintExportSettings¶
Bases:
objectContains settings relating to printing layouts
- dpi: float¶
- flags: Qgis.LayoutRenderFlags¶
- rasterizeWholeImage: bool¶
- Success = 0¶
- class SvgExportSettings¶
Bases:
objectContains settings relating to exporting layouts to SVG
- cropMargins: QgsMargins¶
- cropToContents: bool¶
- dpi: float¶
- exportAsLayers: bool¶
- exportLabelsToSeparateLayers: bool¶
- exportMetadata: bool¶
- flags: Qgis.LayoutRenderFlags¶
- forceVectorOutput: bool¶
- simplifyGeometries: bool¶
- textRenderFormat: Qgis.TextRenderFormat¶
- SvgLayerError = 5¶
- computeWorldFileParameters(self, dpi: float = -1)[source]¶
Compute world file parameters. Assumes the whole page containing the reference map item will be exported.
The
dpiargument can be set to the actual DPI of exported file, or left as -1 to use the layout’s default DPI.computeWorldFileParameters(self, region: QRectF, dpi: float = -1) -> (float, float, float, float, float, float) Computes the world file parameters for a specified
regionof the layout.The
dpiargument can be set to the actual DPI of exported file, or left as -1 to use the layout’s default DPI.- Parameters:
dpi (float = -1) -> (float)
- static containsAdvancedEffects(layout: QgsLayout | None) bool[source]¶
Returns
Trueif the specifiedlayoutcontains visible items which have settings such as opacity which will prevent these individual items from being exported as vector artwork.See also
Added in version 3.20.
- Parameters:
layout (Optional[QgsLayout])
- Return type:
bool
- errorFile(self) str[source]¶
Returns the file name corresponding to the last error encountered during an export.
- Return type:
str
- errorMessage(self) str[source]¶
Returns a string describing the last error encountered during an export.
Added in version 3.38.
- Return type:
str
- exportToImage(self, filePath: str | None, settings: QgsLayoutExporter.ImageExportSettings) QgsLayoutExporter.ExportResult[source]¶
Exports the layout to the
filePath, using the specified exportsettings.If the layout is a multi-page layout, then filenames for each page will automatically be generated by appending “_1”, “_2”, etc to the image file’s base name.
Returns a result code indicating whether the export was successful or an error was encountered. If an error code is returned,
errorFile()can be called to determine the filename for the export which encountered the error.exportToImage(iterator: Optional[QgsAbstractLayoutIterator], baseFilePath: Optional[str], extension: Optional[str], settings: QgsLayoutExporter.ImageExportSettings, feedback: Optional[QgsFeedback] = None) -> (QgsLayoutExporter.ExportResult, str) Exports a layout
iteratorto raster images, with the specified exportsettings.The
baseFilePathargument gives a base file path, which is modified by the iterator to obtain file paths for each iterator feature.Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then
errorwill be set to the error description.- Parameters:
filePath (Optional[str])
settings (QgsLayoutExporter.ImageExportSettings)
- Return type:
- exportToPdf(self, filePath: str | None, settings: QgsLayoutExporter.PdfExportSettings) QgsLayoutExporter.ExportResult[source]¶
Exports the layout as a PDF to the
filePath, using the specified exportsettings.Returns a result code indicating whether the export was successful or an error was encountered.
exportToPdf(iterator: Optional[QgsAbstractLayoutIterator], fileName: Optional[str], settings: QgsLayoutExporter.PdfExportSettings, feedback: Optional[QgsFeedback] = None) -> (QgsLayoutExporter.ExportResult, str) Exports a layout
iteratorto a single PDF file, with the specified exportsettings.The
fileNameargument gives the destination file name for the output PDF.Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then
errorwill be set to the error description.See also
- Parameters:
filePath (Optional[str])
settings (QgsLayoutExporter.PdfExportSettings)
- Return type:
- static exportToPdfs(iterator: QgsAbstractLayoutIterator | None, baseFilePath: str | None, settings: QgsLayoutExporter.PdfExportSettings, feedback: QgsFeedback | None = None)[source]¶
Exports a layout
iteratorto multiple PDF files, with the specified exportsettings.The
baseFilePathargument gives a base file path, which is modified by the iterator to obtain file paths for each iterator feature.Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then
errorwill be set to the error description.See also
- Parameters:
iterator (Optional[QgsAbstractLayoutIterator])
baseFilePath (Optional[str])
settings (QgsLayoutExporter.PdfExportSettings)
feedback (Optional[QgsFeedback] = None) -> (QgsLayoutExporter.ExportResult)
- exportToSvg(self, filePath: str | None, settings: QgsLayoutExporter.SvgExportSettings) QgsLayoutExporter.ExportResult[source]¶
Exports the layout as an SVG to the
filePath, using the specified exportsettings.Returns a result code indicating whether the export was successful or an error was encountered.
exportToSvg(iterator: Optional[QgsAbstractLayoutIterator], baseFilePath: Optional[str], settings: QgsLayoutExporter.SvgExportSettings, feedback: Optional[QgsFeedback] = None) -> (QgsLayoutExporter.ExportResult, str) Exports a layout
iteratorto SVG files, with the specified exportsettings.The
baseFilePathargument gives a base file path, which is modified by the iterator to obtain file paths for each iterator feature.Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then
errorwill be set to the error description.- Parameters:
filePath (Optional[str])
settings (QgsLayoutExporter.SvgExportSettings)
- Return type:
- virtual generateFileName(self, details: QgsLayoutExporter.PageExportDetails) str[source]¶
Generates the file name for a page during export.
Subclasses can override this method to customize page file naming.
- Parameters:
details (QgsLayoutExporter.PageExportDetails)
- Return type:
str
- georeferenceOutput(self, file: str | None, referenceMap: QgsLayoutItemMap | None = None, exportRegion: QRectF = QRectF(), dpi: float = -1) bool[source]¶
Georeferences a
file(image of PDF) exported from the layout.The
referenceMapargument specifies a map item to use for georeferencing. If left asNone, the default layoutQgsLayout.referenceMap()will be used.The
exportRegionargument can be set to a valid rectangle to indicate that only part of the layout was exported.Similarly, the
dpican be set to the actual DPI of exported file, or left as -1 to use the layout’s default DPI.The function will return
Trueif the output was successfully georeferenced.See also
- Parameters:
file (Optional[str])
referenceMap (Optional[QgsLayoutItemMap] = None)
exportRegion (QRectF = QRectF())
dpi (float = -1)
- Return type:
bool
- labelingResults(self) Any¶
Returns the labeling results for all map items included in the export. Map keys are the item UUIDs (see
QgsLayoutItem.uuid()).Ownership of the results remains with the layout exporter.
Added in version 3.20.
- Return type:
- layout(self) QgsLayout | None[source]¶
Returns the layout linked to this exporter.
- Return type:
Optional[QgsLayout]
- print(self, printer: QPrinter, settings: QgsLayoutExporter.PrintExportSettings) QgsLayoutExporter.ExportResult[source]¶
Prints the layout to a
printer, using the specified exportsettings.Returns a result code indicating whether the export was successful or an error was encountered.
print(iterator: Optional[QgsAbstractLayoutIterator], printer: QPrinter, settings: QgsLayoutExporter.PrintExportSettings, feedback: Optional[QgsFeedback] = None) -> (QgsLayoutExporter.ExportResult, str) Exports a layout
iteratorto aprinter, with the specified exportsettings.Returns a result code indicating whether the export was successful or an error was encountered. If an error was obtained then
errorwill be set to the error description.- Parameters:
printer (QPrinter)
settings (QgsLayoutExporter.PrintExportSettings)
- Return type:
- renderPage(self, painter: QPainter | None, page: int)[source]¶
Renders a full page to a destination
painter.The
pageargument specifies the page number to render. Page numbers are 0 based, such that the first page in a layout is page 0.See also
- Parameters:
painter (Optional[QPainter])
page (int)
- renderPageToImage(self, page: int, imageSize: QSize = QSize(), dpi: float = -1) QImage[source]¶
Renders a full page to an image.
The
pageargument specifies the page number to render. Page numbers are 0 based, such that the first page in a layout is page 0.The optional
imageSizeparameter can specify the target image size, in pixels. It is the caller’s responsibility to ensure that the ratio of the target image size matches the ratio of the corresponding layout page size.The
dpiparameter is an optional dpi override. Set to -1 to use the default layout print resolution. This parameter has no effect ifimageSizeis specified.Returns the rendered image, or a null QImage if the image does not fit into available memory.
See also
See also
- Parameters:
page (int)
imageSize (QSize = QSize())
dpi (float = -1)
- Return type:
QImage
- renderRegion(self, painter: QPainter | None, region: QRectF)[source]¶
Renders a
regionfrom the layout to apainter. This method can be used to render sections of pages rather than full pages.See also
See also
- Parameters:
painter (Optional[QPainter])
region (QRectF)
- renderRegionToImage(self, region: QRectF, imageSize: QSize = QSize(), dpi: float = -1) QImage[source]¶
Renders a
regionof the layout to an image. This method can be used to render sections of pages rather than full pages.The optional
imageSizeparameter can specify the target image size, in pixels. It is the caller’s responsibility to ensure that the ratio of the target image size matches the ratio of the specified region of the layout.The
dpiparameter is an optional dpi override. Set to -1 to use the default layout print resolution. This parameter has no effect ifimageSizeis specified.Returns the rendered image, or a null QImage if the image does not fit into available memory.
See also
See also
- Parameters:
region (QRectF)
imageSize (QSize = QSize())
dpi (float = -1)
- Return type:
QImage
- static requiresRasterization(layout: QgsLayout | None) bool[source]¶
Returns
Trueif the specifiedlayoutcontains visible items which have settings that require rasterization of the entire export layout in order to reproduce the desired appearance.See also
Added in version 3.20.
- Parameters:
layout (Optional[QgsLayout])
- Return type:
bool