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