QGIS API Documentation 4.3.0-Master (d583d975f4e)
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, Qgis::PdfRenderFlags flags )
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
47 pdf::PDFRenderer::Features features;
48 if ( flags.testFlag( Qgis::PdfRenderFlag::RenderTextAsText ) )
49 {
50 features.setFlag( pdf::PDFRenderer::Feature::RealText, true );
51 }
52 renderer = std::make_unique< pdf::PDFRenderer >( &document, &fontCache, &pdfCms, nullptr, features, meshQualitySettings );
53 }
54 pdf::PDFDocumentReader reader;
55 pdf::PDFDocument document;
56 pdf::PDFModifiedDocument modifiedDocument;
57 pdf::PDFFontCache fontCache;
58 pdf::PDFCMSGeneric pdfCms;
59 pdf::PDFMeshQualitySettings meshQualitySettings;
60 std::unique_ptr< pdf::PDFRenderer > renderer;
61};
62#endif
63
65 : mPath( path )
66 , mFlags( flags )
67{
68#ifdef HAVE_PDF4QT
69 mDocumentContainer = std::make_unique< PdfDocumentContainer >( path, flags );
70#endif
71}
72
74
75#ifdef HAVE_PDF4QT
77{
78 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
79 return static_cast< int >( catalog->getPageCount() );
80}
81#else
83{
84 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
85}
86#endif
87
88#ifdef HAVE_PDF4QT
89QRectF QgsPdfRenderer::pageMediaBox( int pageNumber ) const
90{
91 if ( pageNumber < 0 || pageNumber >= pageCount() )
92 return QRectF();
93
94 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
95 return catalog->getPage( pageNumber )->getMediaBox();
96}
97#else
99{
100 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
101}
102#endif
103
104#ifdef HAVE_PDF4QT
105bool QgsPdfRenderer::render( QPainter *painter, const QRectF &rectangle, int pageIndex )
106{
107 mDocumentContainer->renderer->render( painter, rectangle, pageIndex );
108 return true;
109}
110#else
111bool QgsPdfRenderer::render( QPainter *, const QRectF &, int )
112{
113 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
114}
115
116#endif
QFlags< PdfRenderFlag > PdfRenderFlags
PDF rendering flags.
Definition qgis.h:6960
@ RenderTextAsText
Render text items as text objects, not painter paths.
Definition qgis.h:6951
Custom exception class which is raised when an operation is not supported.
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.
QgsPdfRenderer(const QString &path, Qgis::PdfRenderFlags flags=Qgis::PdfRenderFlags())
Constructs a PDF renderer for the file at the specified path.