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 "qpdf_p.h" |
10 | #include "qpdfoutputintent.h" |
11 | |
12 | #include <QtCore/qfile.h> |
13 | #include <QtCore/private/qobject_p.h> |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | class QPdfWriterPrivate : public QObjectPrivate |
18 | { |
19 | public: |
20 | QPdfWriterPrivate() |
21 | : QObjectPrivate() |
22 | { |
23 | engine = new QPdfEngine(); |
24 | output = nullptr; |
25 | pdfVersion = QPdfWriter::PdfVersion_1_4; |
26 | } |
27 | ~QPdfWriterPrivate() |
28 | { |
29 | delete engine; |
30 | delete output; |
31 | } |
32 | |
33 | QPdfEngine *engine; |
34 | QFile *output; |
35 | QPdfWriter::PdfVersion pdfVersion; |
36 | }; |
37 | |
38 | class QPdfPagedPaintDevicePrivate : public QPagedPaintDevicePrivate |
39 | { |
40 | public: |
41 | QPdfPagedPaintDevicePrivate(QPdfWriterPrivate *d) |
42 | : QPagedPaintDevicePrivate(), pd(d) |
43 | {} |
44 | |
45 | ~QPdfPagedPaintDevicePrivate() |
46 | {} |
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(other: 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(other: 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 | |
81 | QPdfWriterPrivate *pd; |
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 | */ |
99 | QPdfWriter::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 | */ |
111 | QPdfWriter::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 | */ |
123 | QPdfWriter::~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 | */ |
135 | void 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 | */ |
151 | QPdfWriter::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 | */ |
160 | QString 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 | */ |
169 | void 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 | */ |
178 | QString 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 | */ |
187 | void 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 | */ |
198 | QUuid 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 | */ |
208 | void QPdfWriter::setDocumentId(QUuid documentId) |
209 | { |
210 | Q_D(QPdfWriter); |
211 | d->engine->d_func()->documentId = documentId; |
212 | } |
213 | |
214 | /*! |
215 | \reimp |
216 | */ |
217 | QPaintEngine *QPdfWriter::paintEngine() const |
218 | { |
219 | Q_D(const QPdfWriter); |
220 | |
221 | return d->engine; |
222 | } |
223 | |
224 | /*! |
225 | \since 5.3 |
226 | |
227 | Sets the PDF \a resolution in DPI. |
228 | |
229 | This setting affects the coordinate system as returned by, for |
230 | example QPainter::viewport(). |
231 | |
232 | \sa resolution() |
233 | */ |
234 | |
235 | void QPdfWriter::setResolution(int resolution) |
236 | { |
237 | Q_D(const QPdfWriter); |
238 | if (resolution > 0) |
239 | d->engine->setResolution(resolution); |
240 | } |
241 | |
242 | /*! |
243 | \since 5.3 |
244 | |
245 | Returns the resolution of the PDF in DPI. |
246 | |
247 | \sa setResolution() |
248 | */ |
249 | |
250 | int QPdfWriter::resolution() const |
251 | { |
252 | Q_D(const QPdfWriter); |
253 | return d->engine->resolution(); |
254 | } |
255 | |
256 | /*! |
257 | \since 5.15 |
258 | |
259 | Sets the document metadata. This metadata is not influenced by the setTitle / setCreator methods, |
260 | so is up to the user to keep it consistent. |
261 | \a xmpMetadata contains XML formatted metadata to embed into the PDF file. |
262 | |
263 | \sa documentXmpMetadata() |
264 | */ |
265 | |
266 | void QPdfWriter::setDocumentXmpMetadata(const QByteArray &xmpMetadata) |
267 | { |
268 | Q_D(const QPdfWriter); |
269 | d->engine->setDocumentXmpMetadata(xmpMetadata); |
270 | } |
271 | |
272 | /*! |
273 | \since 5.15 |
274 | |
275 | Gets the document metadata, as it was provided with a call to setDocumentXmpMetadata. It will not |
276 | return the default metadata. |
277 | |
278 | \sa setDocumentXmpMetadata() |
279 | */ |
280 | |
281 | QByteArray QPdfWriter::documentXmpMetadata() const |
282 | { |
283 | Q_D(const QPdfWriter); |
284 | return d->engine->documentXmpMetadata(); |
285 | } |
286 | |
287 | /*! |
288 | \since 5.15 |
289 | |
290 | Adds \a fileName attachment to the PDF with (optional) \a mimeType. |
291 | \a data contains the raw file data to embed into the PDF file. |
292 | */ |
293 | |
294 | void QPdfWriter::addFileAttachment(const QString &fileName, const QByteArray &data, const QString &mimeType) |
295 | { |
296 | Q_D(QPdfWriter); |
297 | d->engine->addFileAttachment(fileName, data, mimeType); |
298 | } |
299 | |
300 | /*! |
301 | \internal |
302 | |
303 | Returns the metric for the given \a id. |
304 | */ |
305 | int QPdfWriter::metric(PaintDeviceMetric id) const |
306 | { |
307 | Q_D(const QPdfWriter); |
308 | return d->engine->metric(metricType: id); |
309 | } |
310 | |
311 | /*! |
312 | \reimp |
313 | */ |
314 | bool QPdfWriter::newPage() |
315 | { |
316 | Q_D(QPdfWriter); |
317 | |
318 | return d->engine->newPage(); |
319 | } |
320 | |
321 | /*! |
322 | \enum QPdfWriter::ColorModel |
323 | \since 6.8 |
324 | |
325 | This enumeration describes the way in which the PDF engine interprets |
326 | stroking and filling colors, set as a QPainter's pen or brush (via |
327 | QPen and QBrush). |
328 | |
329 | \value RGB All colors are converted to RGB and saved as such in the |
330 | PDF. |
331 | |
332 | \value Grayscale All colors are converted to grayscale. For backwards |
333 | compatibility, they are emitted in the PDF output as RGB colors, with |
334 | identical quantities of red, green and blue. |
335 | |
336 | \value CMYK All colors are converted to CMYK and saved as such. |
337 | |
338 | \value Auto RGB colors are emitted as RGB; CMYK colors are emitted as |
339 | CMYK. Colors of any other color spec are converted to RGB. |
340 | This is the default since Qt 6.8. |
341 | |
342 | \sa QColor, QGradient |
343 | */ |
344 | |
345 | /*! |
346 | \since 6.8 |
347 | |
348 | Returns the color model used by this PDF writer. |
349 | The default is QPdfWriter::ColorModel::Auto. |
350 | */ |
351 | QPdfWriter::ColorModel QPdfWriter::colorModel() const |
352 | { |
353 | Q_D(const QPdfWriter); |
354 | return static_cast<ColorModel>(d->engine->d_func()->colorModel); |
355 | } |
356 | |
357 | /*! |
358 | \since 6.8 |
359 | |
360 | Sets the color model used by this PDF writer to \a model. |
361 | */ |
362 | void QPdfWriter::setColorModel(ColorModel model) |
363 | { |
364 | Q_D(QPdfWriter); |
365 | d->engine->d_func()->colorModel = static_cast<QPdfEngine::ColorModel>(model); |
366 | } |
367 | |
368 | /*! |
369 | \since 6.8 |
370 | |
371 | Returns the output intent used by this PDF writer. |
372 | */ |
373 | QPdfOutputIntent QPdfWriter::outputIntent() const |
374 | { |
375 | Q_D(const QPdfWriter); |
376 | return d->engine->d_func()->outputIntent; |
377 | } |
378 | |
379 | /*! |
380 | \since 6.8 |
381 | |
382 | Sets the output intent used by this PDF writer to \a intent. |
383 | */ |
384 | void QPdfWriter::setOutputIntent(const QPdfOutputIntent &intent) |
385 | { |
386 | Q_D(QPdfWriter); |
387 | d->engine->d_func()->outputIntent = intent; |
388 | } |
389 | |
390 | QT_END_NAMESPACE |
391 | |
392 | #include "moc_qpdfwriter.cpp" |
393 | |
394 | #endif // QT_NO_PDF |
395 |
Definitions
- QPdfWriterPrivate
- QPdfWriterPrivate
- ~QPdfWriterPrivate
- QPdfPagedPaintDevicePrivate
- QPdfPagedPaintDevicePrivate
- ~QPdfPagedPaintDevicePrivate
- setPageLayout
- setPageSize
- setPageOrientation
- setPageMargins
- pageLayout
- QPdfWriter
- QPdfWriter
- ~QPdfWriter
- setPdfVersion
- pdfVersion
- title
- setTitle
- creator
- setCreator
- documentId
- setDocumentId
- paintEngine
- setResolution
- resolution
- setDocumentXmpMetadata
- documentXmpMetadata
- addFileAttachment
- metric
- newPage
- colorModel
- setColorModel
- outputIntent
Learn Advanced QML with KDAB
Find out more