Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpdfwriter.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#include <qpdfwriter.h>
6
7#ifndef QT_NO_PDF
8
10#include "qpdf_p.h"
12
13#include <QtCore/qfile.h>
14#include <QtCore/private/qobject_p.h>
15
17
19{
20public:
23 {
24 engine = new QPdfEngine();
25 output = nullptr;
26 pdfVersion = QPdfWriter::PdfVersion_1_4;
27 }
29 {
30 delete engine;
31 delete output;
32 }
33
37};
38
40{
41public:
45
48
49 bool setPageLayout(const QPageLayout &newPageLayout) override
50 {
51 // Try to set the paint engine page layout
52 pd->engine->setPageLayout(newPageLayout);
53 return pageLayout().isEquivalentTo(newPageLayout);
54 }
55
56 bool setPageSize(const QPageSize &pageSize) override
57 {
58 // Try to set the paint engine page size
59 pd->engine->setPageSize(pageSize);
60 return pageLayout().pageSize().isEquivalentTo(pageSize);
61 }
62
63 bool setPageOrientation(QPageLayout::Orientation orientation) override
64 {
65 // Set the print engine value
66 pd->engine->setPageOrientation(orientation);
67 return pageLayout().orientation() == orientation;
68 }
69
70 bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) override
71 {
72 // Try to set engine margins
73 pd->engine->setPageMargins(margins, units);
74 return pageLayout().margins() == margins && pageLayout().units() == units;
75 }
76
77 QPageLayout pageLayout() const override
78 {
79 return pd->engine->pageLayout();
80 }
81
83};
84
85/*! \class QPdfWriter
86 \inmodule QtGui
87
88 \brief The QPdfWriter class is a class to generate PDFs
89 that can be used as a paint device.
90
91 \ingroup painting
92
93 QPdfWriter generates PDF out of a series of drawing commands using QPainter.
94 The newPage() method can be used to create several pages.
95 */
96
97/*!
98 Constructs a PDF writer that will write the pdf to \a filename.
99 */
100QPdfWriter::QPdfWriter(const QString &filename)
101 : QObject(*new QPdfWriterPrivate),
102 QPagedPaintDevice(new QPdfPagedPaintDevicePrivate(d_func()))
103{
104 Q_D(QPdfWriter);
105
106 d->engine->setOutputFilename(filename);
107}
108
109/*!
110 Constructs a PDF writer that will write the pdf to \a device.
111 */
112QPdfWriter::QPdfWriter(QIODevice *device)
113 : QObject(*new QPdfWriterPrivate),
114 QPagedPaintDevice(new QPdfPagedPaintDevicePrivate(d_func()))
115{
116 Q_D(QPdfWriter);
117
118 d->engine->d_func()->outDevice = device;
119}
120
121/*!
122 Destroys the pdf writer.
123 */
124QPdfWriter::~QPdfWriter()
125{
126
127}
128
129/*!
130 \since 5.10
131
132 Sets the PDF version for this writer to \a version.
133
134 If \a version is the same value as currently set then no change will be made.
135*/
136void QPdfWriter::setPdfVersion(PdfVersion version)
137{
138 Q_D(QPdfWriter);
139
140 if (d->pdfVersion == version)
141 return;
142
143 d->pdfVersion = version;
144 d->engine->setPdfVersion(static_cast<QPdfEngine::PdfVersion>(static_cast<int>(version)));
145}
146
147/*!
148 \since 5.10
149
150 Returns the PDF version for this writer. The default is \c PdfVersion_1_4.
151*/
152QPdfWriter::PdfVersion QPdfWriter::pdfVersion() const
153{
154 Q_D(const QPdfWriter);
155 return d->pdfVersion;
156}
157
158/*!
159 Returns the title of the document.
160 */
161QString QPdfWriter::title() const
162{
163 Q_D(const QPdfWriter);
164 return d->engine->d_func()->title;
165}
166
167/*!
168 Sets the title of the document being created to \a title.
169 */
170void QPdfWriter::setTitle(const QString &title)
171{
172 Q_D(QPdfWriter);
173 d->engine->d_func()->title = title;
174}
175
176/*!
177 Returns the creator of the document.
178 */
179QString QPdfWriter::creator() const
180{
181 Q_D(const QPdfWriter);
182 return d->engine->d_func()->creator;
183}
184
185/*!
186 Sets the creator of the document to \a creator.
187 */
188void QPdfWriter::setCreator(const QString &creator)
189{
190 Q_D(QPdfWriter);
191 d->engine->d_func()->creator = creator;
192}
193
194/*!
195 \since 6.8
196 Returns the ID of the document. By default, the ID is a
197 randomly generated UUID.
198 */
199QUuid QPdfWriter::documentId() const
200{
201 Q_D(const QPdfWriter);
202 return d->engine->d_func()->documentId;
203}
204
205/*!
206 \since 6.8
207 Sets the ID of the document to \a documentId.
208 */
209void QPdfWriter::setDocumentId(QUuid documentId)
210{
211 Q_D(QPdfWriter);
212 d->engine->d_func()->documentId = documentId;
213}
214
215/*!
216 \since 6.9
217 Returns the author of the document.
218 */
219QString QPdfWriter::author() const
220{
221 Q_D(const QPdfWriter);
222 return d->engine->d_func()->author;
223}
224
225/*!
226 \since 6.9
227 Sets the author of the document to \a author.
228 */
229void QPdfWriter::setAuthor(const QString &author)
230{
231 Q_D(QPdfWriter);
232 d->engine->d_func()->author = author;
233}
234
235/*!
236 \reimp
237 */
238QPaintEngine *QPdfWriter::paintEngine() const
239{
240 Q_D(const QPdfWriter);
241
242 return d->engine;
243}
244
245/*!
246 \since 5.3
247
248 Sets the PDF \a resolution in DPI.
249
250 This setting affects the coordinate system as returned by, for
251 example QPainter::viewport().
252
253 \sa resolution()
254*/
255
256void QPdfWriter::setResolution(int resolution)
257{
258 Q_D(const QPdfWriter);
259 if (resolution > 0)
260 d->engine->setResolution(resolution);
261}
262
263/*!
264 \since 5.3
265
266 Returns the resolution of the PDF in DPI.
267
268 \sa setResolution()
269*/
270
271int QPdfWriter::resolution() const
272{
273 Q_D(const QPdfWriter);
274 return d->engine->resolution();
275}
276
277/*!
278 \since 5.15
279
280 Sets the document metadata. This metadata is not influenced by the setTitle / setCreator methods,
281 so is up to the user to keep it consistent.
282 \a xmpMetadata contains XML formatted metadata to embed into the PDF file.
283
284 \sa documentXmpMetadata()
285*/
286
287void QPdfWriter::setDocumentXmpMetadata(const QByteArray &xmpMetadata)
288{
289 Q_D(const QPdfWriter);
290 d->engine->setDocumentXmpMetadata(xmpMetadata);
291}
292
293/*!
294 \since 5.15
295
296 Gets the document metadata, as it was provided with a call to setDocumentXmpMetadata. It will not
297 return the default metadata.
298
299 \sa setDocumentXmpMetadata()
300*/
301
302QByteArray QPdfWriter::documentXmpMetadata() const
303{
304 Q_D(const QPdfWriter);
305 return d->engine->documentXmpMetadata();
306}
307
308/*!
309 \since 5.15
310
311 Adds \a fileName attachment to the PDF with (optional) \a mimeType.
312 \a data contains the raw file data to embed into the PDF file.
313*/
314
315void QPdfWriter::addFileAttachment(const QString &fileName, const QByteArray &data, const QString &mimeType)
316{
317 Q_D(QPdfWriter);
318 d->engine->addFileAttachment(fileName, data, mimeType);
319}
320
321/*!
322 \internal
323
324 Returns the metric for the given \a id.
325*/
326int QPdfWriter::metric(PaintDeviceMetric id) const
327{
328 Q_D(const QPdfWriter);
329 return d->engine->metric(id);
330}
331
332/*!
333 \reimp
334*/
335bool QPdfWriter::newPage()
336{
337 Q_D(QPdfWriter);
338
339 return d->engine->newPage();
340}
341
342/*!
343 \enum QPdfWriter::ColorModel
344 \since 6.8
345
346 This enumeration describes the way in which the PDF engine interprets
347 stroking and filling colors, set as a QPainter's pen or brush (via
348 QPen and QBrush).
349
350 \value RGB All colors are converted to RGB and saved as such in the
351 PDF.
352
353 \value Grayscale All colors are converted to grayscale. For backwards
354 compatibility, they are emitted in the PDF output as RGB colors, with
355 identical quantities of red, green and blue.
356
357 \value CMYK All colors are converted to CMYK and saved as such.
358
359 \value Auto RGB colors are emitted as RGB; CMYK colors are emitted as
360 CMYK. Colors of any other color spec are converted to RGB.
361 This is the default since Qt 6.8.
362
363 \sa QColor, QGradient
364*/
365
366/*!
367 \since 6.8
368
369 Returns the color model used by this PDF writer.
370 The default is QPdfWriter::ColorModel::Auto.
371*/
372QPdfWriter::ColorModel QPdfWriter::colorModel() const
373{
374 Q_D(const QPdfWriter);
375 return static_cast<ColorModel>(d->engine->d_func()->colorModel);
376}
377
378/*!
379 \since 6.8
380
381 Sets the color model used by this PDF writer to \a model.
382*/
383void QPdfWriter::setColorModel(ColorModel model)
384{
385 Q_D(QPdfWriter);
386 d->engine->d_func()->colorModel = static_cast<QPdfEngine::ColorModel>(model);
387}
388
389/*!
390 \since 6.8
391
392 Returns the output intent used by this PDF writer.
393*/
394QPdfOutputIntent QPdfWriter::outputIntent() const
395{
396 Q_D(const QPdfWriter);
397 return d->engine->d_func()->outputIntent;
398}
399
400/*!
401 \since 6.8
402
403 Sets the output intent used by this PDF writer to \a intent.
404*/
405void QPdfWriter::setOutputIntent(const QPdfOutputIntent &intent)
406{
407 Q_D(QPdfWriter);
408 d->engine->d_func()->outputIntent = intent;
409}
410
411QT_END_NAMESPACE
412
413#include "moc_qpdfwriter.cpp"
414
415#endif // QT_NO_PDF
\inmodule QtCore
Definition qmargins.h:304
bool setPageSize(const QPageSize &pageSize) override
bool setPageLayout(const QPageLayout &newPageLayout) override
QPdfPagedPaintDevicePrivate(QPdfWriterPrivate *d)
QPdfWriterPrivate * pd
QPageLayout pageLayout() const override
bool setPageMargins(const QMarginsF &margins, QPageLayout::Unit units) override
bool setPageOrientation(QPageLayout::Orientation orientation) override
QPdfEngine * engine
QPdfWriter::PdfVersion pdfVersion
Combined button and popup list for selecting options.