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