def testIteratorToPdfs(self): project, layout = self.prepareIteratorLayout() atlas = layout.atlas() atlas.setFilenameExpression("'test_exportiteratortopdf_' || \"NAME_1\"") # setup settings settings = QgsLayoutExporter.PdfExportSettings() settings.dpi = 80 settings.rasterizeWholeImage = False settings.forceVectorOutput = False result, error = QgsLayoutExporter.exportToPdfs(atlas, self.basetestpath + '/', settings) self.assertEqual(result, QgsLayoutExporter.Success, error) page1_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Basse-Normandie.pdf') rendered_page_1 = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Basse-Normandie.png') pdfToPng(page1_path, rendered_page_1, dpi=80, page=1) self.assertTrue(self.checkImage('iteratortopdf1', 'iteratortoimage1', rendered_page_1, size_tolerance=2)) page2_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Bretagne.pdf') rendered_page_2 = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Bretagne.png') pdfToPng(page2_path, rendered_page_2, dpi=80, page=1) self.assertTrue(self.checkImage('iteratortopdf2', 'iteratortoimage2', rendered_page_2, size_tolerance=2)) page3_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Centre.pdf') self.assertTrue(os.path.exists(page3_path)) page4_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Pays de la Loire.pdf') self.assertTrue(os.path.exists(page4_path))
def testIteratorToPdfs(self): project, layout = self.prepareIteratorLayout() atlas = layout.atlas() atlas.setFilenameExpression("'test_exportiteratortopdf_' || \"NAME_1\"") # setup settings settings = QgsLayoutExporter.PdfExportSettings() settings.dpi = 80 settings.rasterizeWholeImage = False settings.forceVectorOutput = False result, error = QgsLayoutExporter.exportToPdfs(atlas, self.basetestpath + '/', settings) self.assertEqual(result, QgsLayoutExporter.Success, error) page1_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Basse-Normandie.pdf') rendered_page_1 = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Basse-Normandie.png') pdfToPng(page1_path, rendered_page_1, dpi=80, page=1) self.assertTrue(self.checkImage('iteratortopdf1', 'iteratortoimage1', rendered_page_1, size_tolerance=2)) page2_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Bretagne.pdf') rendered_page_2 = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Bretagne.png') pdfToPng(page2_path, rendered_page_2, dpi=80, page=1) self.assertTrue(self.checkImage('iteratortopdf2', 'iteratortoimage2', rendered_page_2, size_tolerance=2)) page3_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Centre.pdf') self.assertTrue(os.path.exists(page3_path)) page4_path = os.path.join(self.basetestpath, 'test_exportiteratortopdf_Pays de la Loire.pdf') self.assertTrue(os.path.exists(page4_path))
def exportPdf(self, outputPath, feedback=None): exporter = QgsLayoutExporter(self.layout) result, error = exporter.exportToPdfs( self.layout.atlas(), outputPath, settings=QgsLayoutExporter.PdfExportSettings(), feedback=feedback) return result, error
def exportCompo(self, cView, folder, title, extension): """Function that sets how to export files. Returns a file :param cView: The print layout to export :param folder: The folder in which to store the output file :param title: The print layout name :param extension: The file extension to use for the output :return: A file representing the layout in the selected format """ #self.msgWMSWarning(cView) myAtlas = cView.atlas() #Let's use custom export properties if there are exportSettings = self.overrideExportSettings(cView, extension) # Do the export process exporter = QgsLayoutExporter(cView) if myAtlas.enabled(): feedback = QgsFeedback() # if single file export is required (only compatible with pdf, yet) # singleFile can be true and None in that case if cView.customProperty( 'singleFile') is not False and extension == '.pdf': result, error = exporter.exportToPdf( myAtlas, os.path.join(folder, title + '.pdf'), exportSettings, feedback) else: #If instead multiple files will be output # Check if there's a valid expression for filenames, # and otherwise inform that a default one will be used and set it using the layout name. # replacement in the GUI is failing at the moment # if len(myAtlas.filenameExpression()) == 0: # self.iface.messageBar().pushMessage( # self.tr(u'Empty filename expression'), # self.tr(u'The print layout "{}" has an empty output filename expression. {}_@atlas_pagename is used as default.').format(title, title), # level = Qgis.Warning # ) # myAtlas.setFilenameExpression(u"'{}_'||@atlas_pagename".format(title)) current_fileName = myAtlas.filenameExpression() #export atlas to multiple pdfs if extension == '.pdf': result, error = exporter.exportToPdfs( myAtlas, os.path.join(folder, current_fileName), exportSettings, feedback) # export atlas to svg format elif extension == '.svg': result, error = exporter.exportToSvg( myAtlas, os.path.join(folder, current_fileName), exportSettings, feedback) # export atlas to image format else: result, error = exporter.exportToImage( myAtlas, os.path.join(folder, current_fileName), extension, exportSettings, feedback) myAtlas.endRender() # if the composition has no atlas else: if extension == '.pdf': result = exporter.exportToPdf( os.path.join(folder, title + '.pdf'), exportSettings) elif extension == '.svg': result = exporter.exportToSvg( os.path.join(folder, title + '.svg'), exportSettings) else: result = exporter.exportToImage( os.path.join(folder, title + extension), exportSettings) return result == QgsLayoutExporter.Success
def atlas_renderer(layout, coverage_layer, output_path, file_format): """Extract composition using atlas generation. :param layout: QGIS Print Layout object used for producing the report. :type layout: qgis.core.QgsPrintLayout :param coverage_layer: Coverage Layer used for atlas map. :type coverage_layer: QgsMapLayer :param output_path: The output path of the product. :type output_path: str :param file_format: File format of map output, 'pdf' or 'png'. :type file_format: str :return: Generated output path(s). :rtype: str, list """ # set the composer map to be atlas driven composer_map = layout_item( layout, 'impact-map', QgsLayoutItemMap) composer_map.setAtlasDriven(True) composer_map.setAtlasScalingMode(QgsLayoutItemMap.Auto) # setup the atlas composition and composition atlas mode atlas_composition = layout.atlas() atlas_composition.setCoverageLayer(coverage_layer) atlas_on_single_file = layout.customProperty('singleFile', True) if file_format == QgisComposerComponentsMetadata.OutputFormat.PDF: if not atlas_composition.filenameExpression(): atlas_composition.setFilenameExpression( "'output_'||@atlas_featurenumber") output_directory = os.path.dirname(output_path) # we need to set the predefined scales for atlas project_scales = [] scales = QgsProject.instance().readListEntry( "Scales", "/ScalesList")[0] has_project_scales = QgsProject.instance().readBoolEntry( "Scales", "/useProjectScales")[0] if not has_project_scales or not scales: scales_string = str(general_setting("Map/scales", PROJECT_SCALES)) scales = scales_string.split(',') for scale in scales: parts = scale.split(':') if len(parts) == 2: project_scales.append(float(parts[1])) layout.reportContext().setPredefinedScales(project_scales) settings = QgsLayoutExporter.PdfExportSettings() LOGGER.info('Exporting Atlas') atlas_output = [] if atlas_on_single_file: res, error = QgsLayoutExporter.exportToPdf( atlas_composition, output_path, settings) atlas_output.append(output_path) else: res, error = QgsLayoutExporter.exportToPdfs( atlas_composition, output_directory, settings) if res != QgsLayoutExporter.Success: LOGGER.error(error) return atlas_output
def atlas_renderer(layout, coverage_layer, output_path, file_format): """Extract composition using atlas generation. :param layout: QGIS Print Layout object used for producing the report. :type layout: qgis.core.QgsPrintLayout :param coverage_layer: Coverage Layer used for atlas map. :type coverage_layer: QgsMapLayer :param output_path: The output path of the product. :type output_path: str :param file_format: File format of map output, 'pdf' or 'png'. :type file_format: str :return: Generated output path(s). :rtype: str, list """ # set the composer map to be atlas driven composer_map = layout_item( layout, 'impact-map', QgsLayoutItemMap) composer_map.setAtlasDriven(True) composer_map.setAtlasScalingMode(QgsLayoutItemMap.Auto) # setup the atlas composition and composition atlas mode atlas_composition = layout.atlas() atlas_composition.setCoverageLayer(coverage_layer) atlas_on_single_file = layout.customProperty('singleFile', True) if file_format == QgisComposerComponentsMetadata.OutputFormat.PDF: if not atlas_composition.filenameExpression(): atlas_composition.setFilenameExpression( "'output_'||@atlas_featurenumber") output_directory = os.path.dirname(output_path) # we need to set the predefined scales for atlas project_scales = [] scales = QgsProject.instance().readListEntry( "Scales", "/ScalesList")[0] has_project_scales = QgsProject.instance().readBoolEntry( "Scales", "/useProjectScales")[0] if not has_project_scales or not scales: scales_string = str(general_setting("Map/scales", PROJECT_SCALES)) scales = scales_string.split(',') for scale in scales: parts = scale.split(':') if len(parts) == 2: project_scales.append(float(parts[1])) layout.reportContext().setPredefinedScales(project_scales) settings = QgsLayoutExporter.PdfExportSettings() LOGGER.info('Exporting Atlas') atlas_output = [] if atlas_on_single_file: res, error = QgsLayoutExporter.exportToPdf( atlas_composition, output_path, settings) atlas_output.append(output_path) else: res, error = QgsLayoutExporter.exportToPdfs( atlas_composition, output_directory, settings) if res != QgsLayoutExporter.Success: LOGGER.error(error) return atlas_output