QGIS API Documentation 3.99.0-Master (26c88405ac0)
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( nullptr, []( bool * )->QString {return QString(); }, true, false )
39 , document( reader.readFromFile( path ) )
40 , modifiedDocument( &document, nullptr )
41 , fontCache( 1000, 1000 )
42 {
43 fontCache.setDocument( modifiedDocument );
44 renderer = std::make_unique< pdf::PDFRenderer >( &document,
45 &fontCache,
46 &pdfCms,
47 nullptr,
48 pdf::PDFRenderer::Features(),
49 meshQualitySettings );
50 }
51 pdf::PDFDocumentReader reader;
52 pdf::PDFDocument document;
53 pdf::PDFModifiedDocument modifiedDocument;
54 pdf::PDFFontCache fontCache;
55 pdf::PDFCMSGeneric pdfCms;
56 pdf::PDFMeshQualitySettings meshQualitySettings;
57 std::unique_ptr< pdf::PDFRenderer > renderer;
58};
59#endif
60
62 : mPath( path )
63{
64#ifdef HAVE_PDF4QT
65 mDocumentContainer = std::make_unique< PdfDocumentContainer >( path );
66#endif
67}
68
70
71#ifdef HAVE_PDF4QT
73{
74 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
75 return catalog->getPageCount();
76}
77#else
79{
80 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
81}
82#endif
83
84#ifdef HAVE_PDF4QT
85QRectF QgsPdfRenderer::pageMediaBox( int pageNumber ) const
86{
87 if ( pageNumber < 0 || pageNumber >= pageCount() )
88 return QRectF();
89
90 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
91 return catalog->getPage( pageNumber )->getMediaBox();
92}
93#else
95{
96 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
97}
98#endif
99
100#ifdef HAVE_PDF4QT
101bool QgsPdfRenderer::render( QPainter *painter, const QRectF &rectangle, int pageIndex )
102{
103 mDocumentContainer->renderer->render( painter, rectangle, pageIndex );
104 return true;
105}
106#else
107bool QgsPdfRenderer::render( QPainter *, const QRectF &, int )
108{
109 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
110}
111
112#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.