Skip to content
Permalink
Browse files

[layouts] Fix incorrect first page size in multipage layouts when

first page is skipped from output

Fixes #18742
  • Loading branch information
nyalldawson committed Oct 23, 2018
1 parent cc543d6 commit 5e6eef3e54d83c0bc983cd8f8f5f32194b0519fd
@@ -1024,7 +1024,7 @@ void QgsLayoutExporter::preparePrintAsPdf( QgsLayout *layout, QPrinter &printer,
// Also an issue with PDF paper size using QPrinter::NativeFormat on Mac (always outputs portrait letter-size)
printer.setOutputFormat( QPrinter::PdfFormat );

updatePrinterPageSize( layout, printer, 0 );
updatePrinterPageSize( layout, printer, firstPageToBeExported( layout ) );

// TODO: add option for this in layout
// May not work on Windows or non-X11 Linux. Works fine on Mac using QPrinter::NativeFormat
@@ -1043,7 +1043,7 @@ void QgsLayoutExporter::preparePrint( QgsLayout *layout, QPrinter &printer, bool

if ( setFirstPageSize )
{
updatePrinterPageSize( layout, printer, 0 );
updatePrinterPageSize( layout, printer, firstPageToBeExported( layout ) );
}
}

@@ -1566,6 +1566,21 @@ QImage QgsLayoutExporter::createImage( const QgsLayoutExporter::ImageExportSetti
}
}

int QgsLayoutExporter::firstPageToBeExported( QgsLayout *layout )
{
const int pageCount = layout->pageCollection()->pageCount();
for ( int i = 0; i < pageCount; ++i )
{
if ( !layout->pageCollection()->shouldExportPage( i ) )
{
continue;
}

return i;
}
return 0; // shouldn't really matter -- we aren't exporting ANY pages!
}

QString QgsLayoutExporter::generateFileName( const PageExportDetails &details ) const
{
if ( details.page == 0 )
@@ -496,6 +496,12 @@ class CORE_EXPORT QgsLayoutExporter

QImage createImage( const ImageExportSettings &settings, int page, QRectF &bounds, bool &skipPage ) const;

/**
* Returns the page number of the first page to be exported from the layout, skipping any pages
* which have been excluded from export.
*/
static int firstPageToBeExported( QgsLayout *layout );

/**
* Saves an image to a file, possibly using format specific options (e.g. LZW compression for tiff)
*/
@@ -463,6 +463,48 @@ def testExportToPdf(self):
self.assertEqual(metadata['SUBJECT'], 'proj abstract')
self.assertEqual(metadata['TITLE'], 'proj title')

def testExportToPdfSkipFirstPage(self):
l = QgsLayout(QgsProject.instance())
l.initializeDefaults()

# page 1 is excluded from export
page1 = l.pageCollection().page(0)
page1.setExcludeFromExports(True)

# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize('A5')
l.pageCollection().addPage(page2)

item2 = QgsLayoutItemShape(l)
item2.attemptSetSceneRect(QRectF(10, 20, 100, 150))
item2.attemptMove(QgsLayoutPoint(10, 20), page=1)
fill = QgsSimpleFillSymbolLayer()
fill_symbol = QgsFillSymbol()
fill_symbol.changeSymbolLayer(0, fill)
fill.setColor(Qt.cyan)
fill.setStrokeStyle(Qt.NoPen)
item2.setSymbol(fill_symbol)
l.addItem(item2)

exporter = QgsLayoutExporter(l)
# setup settings
settings = QgsLayoutExporter.PdfExportSettings()
settings.dpi = 80
settings.rasterizeWholeImage = False
settings.forceVectorOutput = False
settings.exportMetadata = True

pdf_file_path = os.path.join(self.basetestpath, 'test_exporttopdfdpi_skip_first.pdf')
self.assertEqual(exporter.exportToPdf(pdf_file_path, settings), QgsLayoutExporter.Success)
self.assertTrue(os.path.exists(pdf_file_path))

rendered_page_1 = os.path.join(self.basetestpath, 'test_exporttopdfdpi_skip_first.png')
dpi = 80
pdfToPng(pdf_file_path, rendered_page_1, dpi=dpi, page=1)

self.assertTrue(self.checkImage('test_exporttopdfdpi_skip_first', 'exporttopdfdpi_page2', rendered_page_1, size_tolerance=1))

def testExportToSvg(self):
md = QgsProject.instance().metadata()
md.setTitle('proj title')

0 comments on commit 5e6eef3

Please sign in to comment.
You can’t perform that action at this time.