1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qpdfwriter.h>
5
6#ifndef QT_NO_PDF
7
8#include "qpagedpaintdevice_p.h"
9#include <QtCore/private/qobject_p.h>
10#include "private/qpdf_p.h"
11#include <QtCore/qfile.h>
12
13QT_BEGIN_NAMESPACE
14
15class QPdfWriterPrivate : public QObjectPrivate
16{
17public:
18 QPdfWriterPrivate()
19 : QObjectPrivate()
20 {
21 engine = new QPdfEngine();
22 output = nullptr;
23 pdfVersion = QPdfWriter::PdfVersion_1_4;
24 }
25 ~QPdfWriterPrivate()
26 {
27 delete engine;
28 delete output;
29 }
30
31 QPdfEngine *engine;
32 QFile *output;
33 QPdfWriter::PdfVersion pdfVersion;
34};
35
36class QPdfPagedPaintDevicePrivate : public QPagedPaintDevicePrivate
37{
38public:
39 QPdfPagedPaintDevicePrivate(QPdfWriterPrivate *d)
40 : QPagedPaintDevicePrivate(), pd(d)
41 {}
42
43 ~QPdfPagedPaintDevicePrivate()
44 {}
45
46 bool setPageLayout(const QPageLayout &newPageLayout) override
47 {
48 // Try to set the paint engine page layout
49 pd->engine->setPageLayout(newPageLayout);
50 return pageLayout().isEquivalentTo(other: newPageLayout);
51 }
52
53 bool setPageSize(const QPageSize &pageSize) override
54 {
55 // Try to set the paint engine page size
56 pd->engine->setPageSize(pageSize);
57 return pageLayout().pageSize().isEquivalentTo(other: pageSize);
58 }
59
60 bool setPageOrientation(QPageLayout::Orientation orientation) override
61 {
62 // Set the print engine value
63 pd->engine->setPageOrientation(orientation);
64 return pageLayout().orientation() == orientation;
65 }
66
67 bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) override
68 {
69 // Try to set engine margins
70 pd->engine->setPageMargins(margins, units);
71 return pageLayout().margins() == margins && pageLayout().units() == units;
72 }
73
74 QPageLayout pageLayout() const override
75 {
76 return pd->engine->pageLayout();
77 }
78
79 QPdfWriterPrivate *pd;
80};
81
82/*! \class QPdfWriter
83 \inmodule QtGui
84
85 \brief The QPdfWriter class is a class to generate PDFs
86 that can be used as a paint device.
87
88 \ingroup painting
89
90 QPdfWriter generates PDF out of a series of drawing commands using QPainter.
91 The newPage() method can be used to create several pages.
92 */
93
94/*!
95 Constructs a PDF writer that will write the pdf to \a filename.
96 */
97QPdfWriter::QPdfWriter(const QString &filename)
98 : QObject(*new QPdfWriterPrivate),
99 QPagedPaintDevice(new QPdfPagedPaintDevicePrivate(d_func()))
100{
101 Q_D(QPdfWriter);
102
103 d->engine->setOutputFilename(filename);
104}
105
106/*!
107 Constructs a PDF writer that will write the pdf to \a device.
108 */
109QPdfWriter::QPdfWriter(QIODevice *device)
110 : QObject(*new QPdfWriterPrivate),
111 QPagedPaintDevice(new QPdfPagedPaintDevicePrivate(d_func()))
112{
113 Q_D(QPdfWriter);
114
115 d->engine->d_func()->outDevice = device;
116}
117
118/*!
119 Destroys the pdf writer.
120 */
121QPdfWriter::~QPdfWriter()
122{
123
124}
125
126/*!
127 \since 5.10
128
129 Sets the PDF version for this writer to \a version.
130
131 If \a version is the same value as currently set then no change will be made.
132*/
133void QPdfWriter::setPdfVersion(PdfVersion version)
134{
135 Q_D(QPdfWriter);
136
137 if (d->pdfVersion == version)
138 return;
139
140 d->pdfVersion = version;
141 d->engine->setPdfVersion(static_cast<QPdfEngine::PdfVersion>(static_cast<int>(version)));
142}
143
144/*!
145 \since 5.10
146
147 Returns the PDF version for this writer. The default is \c PdfVersion_1_4.
148*/
149QPdfWriter::PdfVersion QPdfWriter::pdfVersion() const
150{
151 Q_D(const QPdfWriter);
152 return d->pdfVersion;
153}
154
155/*!
156 Returns the title of the document.
157 */
158QString QPdfWriter::title() const
159{
160 Q_D(const QPdfWriter);
161 return d->engine->d_func()->title;
162}
163
164/*!
165 Sets the title of the document being created to \a title.
166 */
167void QPdfWriter::setTitle(const QString &title)
168{
169 Q_D(QPdfWriter);
170 d->engine->d_func()->title = title;
171}
172
173/*!
174 Returns the creator of the document.
175 */
176QString QPdfWriter::creator() const
177{
178 Q_D(const QPdfWriter);
179 return d->engine->d_func()->creator;
180}
181
182/*!
183 Sets the creator of the document to \a creator.
184 */
185void QPdfWriter::setCreator(const QString &creator)
186{
187 Q_D(QPdfWriter);
188 d->engine->d_func()->creator = creator;
189}
190
191/*!
192 \reimp
193 */
194QPaintEngine *QPdfWriter::paintEngine() const
195{
196 Q_D(const QPdfWriter);
197
198 return d->engine;
199}
200
201/*!
202 \since 5.3
203
204 Sets the PDF \a resolution in DPI.
205
206 This setting affects the coordinate system as returned by, for
207 example QPainter::viewport().
208
209 \sa resolution()
210*/
211
212void QPdfWriter::setResolution(int resolution)
213{
214 Q_D(const QPdfWriter);
215 if (resolution > 0)
216 d->engine->setResolution(resolution);
217}
218
219/*!
220 \since 5.3
221
222 Returns the resolution of the PDF in DPI.
223
224 \sa setResolution()
225*/
226
227int QPdfWriter::resolution() const
228{
229 Q_D(const QPdfWriter);
230 return d->engine->resolution();
231}
232
233/*!
234 \since 5.15
235
236 Sets the document metadata. This metadata is not influenced by the setTitle / setCreator methods,
237 so is up to the user to keep it consistent.
238 \a xmpMetadata contains XML formatted metadata to embed into the PDF file.
239
240 \sa documentXmpMetadata()
241*/
242
243void QPdfWriter::setDocumentXmpMetadata(const QByteArray &xmpMetadata)
244{
245 Q_D(const QPdfWriter);
246 d->engine->setDocumentXmpMetadata(xmpMetadata);
247}
248
249/*!
250 \since 5.15
251
252 Gets the document metadata, as it was provided with a call to setDocumentXmpMetadata. It will not
253 return the default metadata.
254
255 \sa setDocumentXmpMetadata()
256*/
257
258QByteArray QPdfWriter::documentXmpMetadata() const
259{
260 Q_D(const QPdfWriter);
261 return d->engine->documentXmpMetadata();
262}
263
264/*!
265 \since 5.15
266
267 Adds \a fileName attachment to the PDF with (optional) \a mimeType.
268 \a data contains the raw file data to embed into the PDF file.
269*/
270
271void QPdfWriter::addFileAttachment(const QString &fileName, const QByteArray &data, const QString &mimeType)
272{
273 Q_D(QPdfWriter);
274 d->engine->addFileAttachment(fileName, data, mimeType);
275}
276
277/*!
278 \internal
279
280 Returns the metric for the given \a id.
281*/
282int QPdfWriter::metric(PaintDeviceMetric id) const
283{
284 Q_D(const QPdfWriter);
285 return d->engine->metric(metricType: id);
286}
287
288/*!
289 \reimp
290*/
291bool QPdfWriter::newPage()
292{
293 Q_D(QPdfWriter);
294
295 return d->engine->newPage();
296}
297
298QT_END_NAMESPACE
299
300#include "moc_qpdfwriter.cpp"
301
302#endif // QT_NO_PDF
303

source code of qtbase/src/gui/painting/qpdfwriter.cpp