QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgspdfrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspdfrenderer.cpp
3 -------------------
4 begin : December 2023
5 copyright : (C) 2023 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 "qgspdfrenderer.h"
19
20#ifdef HAVE_PDF4QT
21#include "pdfdocumentreader.h"
22#include "pdfrenderer.h"
23#include "pdffont.h"
24#include "pdfcms.h"
25#else
26#include "qgsexception.h"
27#include <QObject>
28#endif
29
30#include <QRectF>
31#include <QPainter>
32
33#ifdef HAVE_PDF4QT
34class PdfDocumentContainer
35{
36 public:
37 PdfDocumentContainer( const QString &path )
38 : reader(
39 nullptr, []( bool * ) -> QString { return QString(); }, true, false
40 )
41 , document( reader.readFromFile( path ) )
42 , modifiedDocument( &document, nullptr )
43 , fontCache( 1000, 1000 )
44 {
45 fontCache.setDocument( modifiedDocument );
46 renderer = std::make_unique< pdf::PDFRenderer >( &document, &fontCache, &pdfCms, nullptr, pdf::PDFRenderer::Features(), meshQualitySettings );
47 }
48 pdf::PDFDocumentReader reader;
49 pdf::PDFDocument document;
50 pdf::PDFModifiedDocument modifiedDocument;
51 pdf::PDFFontCache fontCache;
52 pdf::PDFCMSGeneric pdfCms;
53 pdf::PDFMeshQualitySettings meshQualitySettings;
54 std::unique_ptr< pdf::PDFRenderer > renderer;
55};
56#endif
57
59 : mPath( path )
60{
61#ifdef HAVE_PDF4QT
62 mDocumentContainer = std::make_unique< PdfDocumentContainer >( path );
63#endif
64}
65
67
68#ifdef HAVE_PDF4QT
70{
71 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
72 return catalog->getPageCount();
73}
74#else
76{
77 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
78}
79#endif
80
81#ifdef HAVE_PDF4QT
82QRectF QgsPdfRenderer::pageMediaBox( int pageNumber ) const
83{
84 if ( pageNumber < 0 || pageNumber >= pageCount() )
85 return QRectF();
86
87 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
88 return catalog->getPage( pageNumber )->getMediaBox();
89}
90#else
92{
93 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
94}
95#endif
96
97#ifdef HAVE_PDF4QT
98bool QgsPdfRenderer::render( QPainter *painter, const QRectF &rectangle, int pageIndex )
99{
100 mDocumentContainer->renderer->render( painter, rectangle, pageIndex );
101 return true;
102}
103#else
104bool QgsPdfRenderer::render( QPainter *, const QRectF &, int )
105{
106 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
107}
108
109#endif
Custom exception class which is raised when an operation is not supported.
QgsPdfRenderer(const QString &path)
Constructs a PDF renderer for the file at the specified path.
QRectF pageMediaBox(int pageNumber) const
Returns the media box for the specified page.
int pageCount() const
Returns the number of pages in the PDF.
bool render(QPainter *painter, const QRectF &painterRect, int pageIndex)
Renders the PDF from the specified path to a painter.
QString path() const
Returns the file path of the associated PDF file.