QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#ifdef HAVE_PDF4QT
20#include "pdfdocumentreader.h"
21#include "pdfrenderer.h"
22#include "pdffont.h"
23#include "pdfcms.h"
24#else
25#include "qgsexception.h"
26#include <QObject>
27#endif
28
29#include <QRectF>
30#include <QPainter>
31
32#ifdef HAVE_PDF4QT
33class PdfDocumentContainer
34{
35 public:
36 PdfDocumentContainer( const QString &path )
37 : reader( nullptr, []( bool * )->QString {return QString(); }, true, false )
38 , document( reader.readFromFile( path ) )
39 , modifiedDocument( &document, nullptr )
40 , fontCache( 1000, 1000 )
41 {
42 fontCache.setDocument( modifiedDocument );
43 renderer = std::make_unique< pdf::PDFRenderer >( &document,
44 &fontCache,
45 &pdfCms,
46 nullptr,
47 pdf::PDFRenderer::Features(),
48 meshQualitySettings );
49 }
50 pdf::PDFDocumentReader reader;
51 pdf::PDFDocument document;
52 pdf::PDFModifiedDocument modifiedDocument;
53 pdf::PDFFontCache fontCache;
54 pdf::PDFCMSGeneric pdfCms;
55 pdf::PDFMeshQualitySettings meshQualitySettings;
56 std::unique_ptr< pdf::PDFRenderer > renderer;
57};
58#endif
59
60QgsPdfRenderer::QgsPdfRenderer( const QString &path )
61 : mPath( path )
62{
63#ifdef HAVE_PDF4QT
64 mDocumentContainer = std::make_unique< PdfDocumentContainer >( path );
65#endif
66}
67
69
70#ifdef HAVE_PDF4QT
72{
73 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
74 return catalog->getPageCount();
75}
76#else
78{
79 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
80}
81#endif
82
83#ifdef HAVE_PDF4QT
84QRectF QgsPdfRenderer::pageMediaBox( int pageNumber ) const
85{
86 if ( pageNumber < 0 || pageNumber >= pageCount() )
87 return QRectF();
88
89 const pdf::PDFCatalog *catalog = mDocumentContainer->document.getCatalog();
90 return catalog->getPage( pageNumber )->getMediaBox();
91}
92#else
94{
95 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
96}
97#endif
98
99#ifdef HAVE_PDF4QT
100bool QgsPdfRenderer::render( QPainter *painter, const QRectF &rectangle, int pageIndex )
101{
102 mDocumentContainer->renderer->render( painter, rectangle, pageIndex );
103 return true;
104}
105#else
106bool QgsPdfRenderer::render( QPainter *, const QRectF &, int )
107{
108 throw QgsNotSupportedException( QObject::tr( "Rendering PDF requires a QGIS build with PDF4Qt library support" ) );
109}
110
111#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.