AusweisApp2/src/export/PdfCreator.cpp

128 lines
3.5 KiB
C++
Raw Normal View History

2017-07-03 09:33:28 +02:00
/*
2019-05-22 10:08:38 +02:00
* \copyright Copyright (c) 2016-2019 Governikus GmbH & Co. KG, Germany
2017-07-03 09:33:28 +02:00
*/
#include "PdfCreator.h"
2017-12-20 14:54:05 +01:00
#include <QDebug>
#include <QPagedPaintDevice>
2017-07-03 09:33:28 +02:00
#include <QPainter>
#include <QSvgRenderer>
using namespace governikus;
2017-12-20 14:54:05 +01:00
PdfCreator::PdfCreator(const QString& pFilename, const QString& pTitle, const QString& pHeadline, const QString& pContent)
: mPdfWriter(pFilename)
, mHeader()
, mContent()
, mFooter()
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
mHeader.setUndoRedoEnabled(false);
mContent.setUndoRedoEnabled(false);
mFooter.setUndoRedoEnabled(false);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
qDebug() << "Use filename for PDF:" << pFilename;
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
const QPageLayout layout(QPageSize(QPageSize::A4), QPageLayout::Portrait, QMargins(20, 20, 20, 15), QPageLayout::Millimeter);
mPdfWriter.setPageLayout(layout);
mPdfWriter.setCreator(QCoreApplication::applicationName());
mPdfWriter.setTitle(pTitle);
mPdfWriter.setPdfVersion(QPagedPaintDevice::PdfVersion_A1b);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
createHeader(pTitle, pHeadline);
createFooter();
createContent(pContent);
2017-07-03 09:33:28 +02:00
}
2017-12-20 14:54:05 +01:00
void PdfCreator::createHeader(const QString& pTitle, const QString& pHeadline)
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
const auto& header = QStringLiteral("<table cellspacing='5' cellpadding='5' width='100%'>"
"<tr><td valign='middle'>"
"<h1>%1 - %2</h1>"
"<h3>%3"
"<th rowspan='2' width='100'><img src='pdflogo' width='100' height='100'></th>"
"</h3>"
"</td></tr>"
"<tr><td valign='middle'>"
"<br><h3>%4</h3>"
"</td></tr>"
"</table>").arg(
pTitle,
QCoreApplication::applicationName(),
2018-03-28 15:10:51 +02:00
tr("AusweisApp2 is a product of Governikus GmbH & Co. KG - on behalf of the Federal Ministry of the Interior, Building and Community."),
2017-12-20 14:54:05 +01:00
pHeadline);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QSvgRenderer renderer(QStringLiteral(":/images/npa.svg"));
QImage image(768, 768, QImage::Format_RGB32);
image.fill(0x00FFFFFF);
QPainter imagePainter(&image);
renderer.render(&imagePainter);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
mHeader.addResource(QTextDocument::ImageResource, QUrl(QStringLiteral("pdflogo")), image);
mHeader.setHtml(header);
2017-07-03 09:33:28 +02:00
}
2017-12-20 14:54:05 +01:00
void PdfCreator::createContent(const QString& pContent)
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
mContent.setHtml(pContent);
2017-07-03 09:33:28 +02:00
}
2017-12-20 14:54:05 +01:00
void PdfCreator::createFooter()
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
const auto& footer = QStringLiteral("<h3>%1</h3>").arg(
tr("For further information, please see <a href='https://www.ausweisapp.bund.de/'>https://www.ausweisapp.bund.de/</a>"));
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
mFooter.setHtml(footer);
2017-07-03 09:33:28 +02:00
}
2017-12-20 14:54:05 +01:00
int qt_defaultDpi();
bool PdfCreator::save()
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
mPdfWriter.setResolution(qt_defaultDpi());
const QRect pageArea(mPdfWriter.pageLayout().paintRectPixels(mPdfWriter.resolution()));
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QPainter painter(&mPdfWriter);
if (!painter.isActive())
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
qCritical() << "Cannot paint into pdf file. Check file system permissions!";
return false;
2017-07-03 09:33:28 +02:00
}
2017-12-20 14:54:05 +01:00
mHeader.setPageSize(pageArea.size());
mFooter.setPageSize(pageArea.size());
const qreal headerHeight = mHeader.size().height();
const QSizeF contentMaxPageSize(pageArea.width(), pageArea.height() - headerHeight - mFooter.size().height());
mContent.setPageSize(contentMaxPageSize);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
const QRect contentRect = QRect(QPoint(0, 0), mContent.size().toSize());
QRect currentRect = QRect(QPoint(0, 0), contentMaxPageSize.toSize());
while (currentRect.intersects(contentRect))
{
painter.resetTransform();
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
mHeader.drawContents(&painter);
painter.translate(0, headerHeight);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
painter.save();
painter.translate(0, -currentRect.y());
mContent.drawContents(&painter, currentRect);
painter.restore();
painter.translate(0, currentRect.height());
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
mFooter.drawContents(&painter);
2017-07-03 09:33:28 +02:00
currentRect.translate(0, currentRect.height());
if (currentRect.intersects(contentRect))
{
2017-12-20 14:54:05 +01:00
mPdfWriter.newPage();
2017-07-03 09:33:28 +02:00
}
}
2017-12-20 14:54:05 +01:00
return true;
2017-07-03 09:33:28 +02:00
}