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 \internal
99*/
100QPdfWriter::QPdfWriter()
101 : QPdfWriter(*new QPdfWriterPrivate)
102{
103
104}
105
106/*!
107 \internal
108
109 This constructor isn't optional; it must always be the target constructor
110 of this class, and all other ctors must delegate to it.
111
112 This is because we need to pass \a dd to both QObject(QObjectPrivate&) as
113 well as the QPdfPagedPaintDevicePrivate(QPdfWriterPrivate*) ctor, in the
114 mem-initializer-list, where we cannot create variables to hold \a dd. In
115 this constructor, the function argument \a dd acts as that variable.
116*/
117QPdfWriter::QPdfWriter(QPdfWriterPrivate &dd)
118 : QObject(dd),
119 QPagedPaintDevice(new QPdfPagedPaintDevicePrivate(&dd))
120{
121
122}
123
124/*!
125 Constructs a PDF writer that will write the pdf to \a filename.
126 */
127QPdfWriter::QPdfWriter(const QString &filename)
128 : QPdfWriter()
129{
130 Q_D(QPdfWriter);
131
132 d->engine->setOutputFilename(filename);
133}
134
135/*!
136 Constructs a PDF writer that will write the pdf to \a device.
137 */
138QPdfWriter::QPdfWriter(QIODevice *device)
139 : QPdfWriter()
140{
141 Q_D(QPdfWriter);
142
143 d->engine->d_func()->outDevice = device;
144}
145
146/*!
147 Destroys the pdf writer.
148 */
149QPdfWriter::~QPdfWriter()
150{
151
152}
153
154/*!
155 \since 5.10
156
157 Sets the PDF version for this writer to \a version.
158
159 If \a version is the same value as currently set then no change will be made.
160*/
161void QPdfWriter::setPdfVersion(PdfVersion version)
162{
163 Q_D(QPdfWriter);
164
165 if (d->pdfVersion == version)
166 return;
167
168 d->pdfVersion = version;
169 d->engine->setPdfVersion(static_cast<QPdfEngine::PdfVersion>(static_cast<int>(version)));
170}
171
172/*!
173 \since 5.10
174
175 Returns the PDF version for this writer. The default is \c PdfVersion_1_4.
176*/
177QPdfWriter::PdfVersion QPdfWriter::pdfVersion() const
178{
179 Q_D(const QPdfWriter);
180 return d->pdfVersion;
181}
182
183/*!
184 Returns the title of the document.
185 */
186QString QPdfWriter::title() const
187{
188 Q_D(const QPdfWriter);
189 return d->engine->d_func()->title;
190}
191
192/*!
193 Sets the title of the document being created to \a title.
194 */
195void QPdfWriter::setTitle(const QString &title)
196{
197 Q_D(QPdfWriter);
198 d->engine->d_func()->title = title;
199}
200
201/*!
202 Returns the creator of the document.
203 */
204QString QPdfWriter::creator() const
205{
206 Q_D(const QPdfWriter);
207 return d->engine->d_func()->creator;
208}
209
210/*!
211 Sets the creator of the document to \a creator.
212 */
213void QPdfWriter::setCreator(const QString &creator)
214{
215 Q_D(QPdfWriter);
216 d->engine->d_func()->creator = creator;
217}
218
219/*!
220 \since 6.8
221 Returns the ID of the document. By default, the ID is a
222 randomly generated UUID.
223 */
224QUuid QPdfWriter::documentId() const
225{
226 Q_D(const QPdfWriter);
227 return d->engine->d_func()->documentId;
228}
229
230/*!
231 \since 6.8
232 Sets the ID of the document to \a documentId.
233 */
234void QPdfWriter::setDocumentId(QUuid documentId)
235{
236 Q_D(QPdfWriter);
237 d->engine->d_func()->documentId = documentId;
238}
239
240/*!
241 \since 6.9
242 Returns the author of the document.
243 */
244QString QPdfWriter::author() const
245{
246 Q_D(const QPdfWriter);
247 return d->engine->d_func()->author;
248}
249
250/*!
251 \since 6.9
252 Sets the author of the document to \a author.
253 */
254void QPdfWriter::setAuthor(const QString &author)
255{
256 Q_D(QPdfWriter);
257 d->engine->d_func()->author = author;
258}
259
260/*!
261 \reimp
262 */
263QPaintEngine *QPdfWriter::paintEngine() const
264{
265 Q_D(const QPdfWriter);
266
267 return d->engine;
268}
269
270/*!
271 \since 5.3
272
273 Sets the PDF \a resolution in DPI.
274
275 This setting affects the coordinate system as returned by, for
276 example QPainter::viewport().
277
278 \sa resolution()
279*/
280
281void QPdfWriter::setResolution(int resolution)
282{
283 Q_D(const QPdfWriter);
284 if (resolution > 0)
285 d->engine->setResolution(resolution);
286}
287
288/*!
289 \since 5.3
290
291 Returns the resolution of the PDF in DPI.
292
293 \sa setResolution()
294*/
295
296int QPdfWriter::resolution() const
297{
298 Q_D(const QPdfWriter);
299 return d->engine->resolution();
300}
301
302/*!
303 \since 5.15
304
305 Sets the document metadata. This metadata is not influenced by the setTitle / setCreator methods,
306 so is up to the user to keep it consistent.
307 \a xmpMetadata contains XML formatted metadata to embed into the PDF file.
308
309 \sa documentXmpMetadata()
310*/
311
312void QPdfWriter::setDocumentXmpMetadata(const QByteArray &xmpMetadata)
313{
314 Q_D(const QPdfWriter);
315 d->engine->setDocumentXmpMetadata(xmpMetadata);
316}
317
318/*!
319 \since 5.15
320
321 Gets the document metadata, as it was provided with a call to setDocumentXmpMetadata. It will not
322 return the default metadata.
323
324 \sa setDocumentXmpMetadata()
325*/
326
327QByteArray QPdfWriter::documentXmpMetadata() const
328{
329 Q_D(const QPdfWriter);
330 return d->engine->documentXmpMetadata();
331}
332
333/*!
334 \since 5.15
335
336 Adds \a fileName attachment to the PDF with (optional) \a mimeType.
337 \a data contains the raw file data to embed into the PDF file.
338*/
339
340void QPdfWriter::addFileAttachment(const QString &fileName, const QByteArray &data, const QString &mimeType)
341{
342 Q_D(QPdfWriter);
343 d->engine->addFileAttachment(fileName, data, mimeType);
344}
345
346/*!
347 \internal
348
349 Returns the metric for the given \a id.
350*/
351int QPdfWriter::metric(PaintDeviceMetric id) const
352{
353 Q_D(const QPdfWriter);
354 return d->engine->metric(id);
355}
356
357/*!
358 \reimp
359*/
360bool QPdfWriter::newPage()
361{
362 Q_D(QPdfWriter);
363
364 return d->engine->newPage();
365}
366
367/*!
368 \enum QPdfWriter::ColorModel
369 \since 6.8
370
371 This enumeration describes the way in which the PDF engine interprets
372 stroking and filling colors, set as a QPainter's pen or brush (via
373 QPen and QBrush).
374
375 \value RGB All colors are converted to RGB and saved as such in the
376 PDF.
377
378 \value Grayscale All colors are converted to grayscale. For backwards
379 compatibility, they are emitted in the PDF output as RGB colors, with
380 identical quantities of red, green and blue.
381
382 \value CMYK All colors are converted to CMYK and saved as such.
383
384 \value Auto RGB colors are emitted as RGB; CMYK colors are emitted as
385 CMYK. Colors of any other color spec are converted to RGB.
386 This is the default since Qt 6.8.
387
388 \sa QColor, QGradient
389*/
390
391/*!
392 \since 6.8
393
394 Returns the color model used by this PDF writer.
395 The default is QPdfWriter::ColorModel::Auto.
396*/
397QPdfWriter::ColorModel QPdfWriter::colorModel() const
398{
399 Q_D(const QPdfWriter);
400 return static_cast<ColorModel>(d->engine->d_func()->colorModel);
401}
402
403/*!
404 \since 6.8
405
406 Sets the color model used by this PDF writer to \a model.
407*/
408void QPdfWriter::setColorModel(ColorModel model)
409{
410 Q_D(QPdfWriter);
411 d->engine->d_func()->colorModel = static_cast<QPdfEngine::ColorModel>(model);
412}
413
414/*!
415 \since 6.8
416
417 Returns the output intent used by this PDF writer.
418*/
419QPdfOutputIntent QPdfWriter::outputIntent() const
420{
421 Q_D(const QPdfWriter);
422 return d->engine->d_func()->outputIntent;
423}
424
425/*!
426 \since 6.8
427
428 Sets the output intent used by this PDF writer to \a intent.
429*/
430void QPdfWriter::setOutputIntent(const QPdfOutputIntent &intent)
431{
432 Q_D(QPdfWriter);
433 d->engine->d_func()->outputIntent = intent;
434}
435
436QT_END_NAMESPACE
437
438#include "moc_qpdfwriter.cpp"
439
440#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.