Fixed PDF export page size

In some cases, export of a document containing a PDF background to PDF
caused the contents to be truncated.

The "simple" PDF exporter will now set the output page size to be equal
either to the document nominal size or, if the document has a background
PDF item, to the size of this item.
preferencesAboutTextFull
Craig Watson 8 years ago
parent b7f5cc2714
commit 716314fe73
  1. 49
      src/adaptors/UBExportPDF.cpp

@ -32,6 +32,7 @@
#include <QtCore>
#include <QtSvg>
#include <QPrinter>
#include <QPdfWriter>
#include "core/UBApplication.h"
#include "core/UBSettings.h"
@ -40,6 +41,7 @@
#include "domain/UBGraphicsScene.h"
#include "domain/UBGraphicsSvgItem.h"
#include "domain/UBGraphicsPDFItem.h"
#include "document/UBDocumentProxy.h"
@ -66,56 +68,67 @@ void UBExportPDF::persist(UBDocumentProxy* pDocumentProxy)
bool UBExportPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename)
{
QPrinter pdfPrinter;
QPdfWriter pdfWriter(filename);
qDebug() << "exporting document to PDF" << filename;
pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
pdfPrinter.setResolution(UBSettings::settings()->pdfResolution->get().toInt());
pdfPrinter.setOutputFileName(filename);
pdfPrinter.setFullPage(true);
pdfWriter.setResolution(UBSettings::settings()->pdfResolution->get().toInt());
pdfWriter.setPageMargins(QMarginsF());
pdfWriter.setTitle(pDocumentProxy->name());
pdfWriter.setCreator("OpenBoard PDF export");
//need to calculate screen resolution
QDesktopWidget* desktop = UBApplication::desktop();
int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2;
float scaleFactor = 72.0f / dpiCommon;
QPainter pdfPainter;
bool painterNeedsBegin = true;
int existingPageCount = pDocumentProxy->pageCount();
for(int pageIndex = 0 ; pageIndex < existingPageCount; pageIndex++)
{
for(int pageIndex = 0 ; pageIndex < existingPageCount; pageIndex++) {
UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex);
UBApplication::showMessage(tr("Exporting page %1 of %2").arg(pageIndex + 1).arg(existingPageCount));
// set background to white, no crossing for PDF output
bool isDark = scene->isDarkBackground();
bool isCrossed = scene->isCrossedBackground();
scene->setBackground(false, false);
QSize pageSize = scene->nominalSize();
// pageSize is the output PDF page size; it is set to equal the scene's boundary size; if the contents
// of the scene overflow from the boundaries, they will be scaled down.
QSize pageSize = scene->sceneSize();
// set high res rendering
scene->setRenderingQuality(UBItem::RenderingQualityHigh);
scene->setRenderingContext(UBGraphicsScene::NonScreen);
//setting page size to appropriate value
pdfPrinter.setPaperSize(QSizeF(pageSize.width()*scaleFactor, pageSize.height()*scaleFactor), QPrinter::Point);
if(painterNeedsBegin) painterNeedsBegin = !pdfPainter.begin(&pdfPrinter);
//render to PDF
scene->render(&pdfPainter, QRectF(), scene->normalizedSceneRect());
// Setting output page size
QPageSize outputPageSize = QPageSize(QSizeF(pageSize.width()*scaleFactor, pageSize.height()*scaleFactor), QPageSize::Point);
pdfWriter.setPageSize(outputPageSize);
if (pageIndex < existingPageCount - 1) pdfPrinter.newPage();
// Call begin only once
if(painterNeedsBegin)
painterNeedsBegin = !pdfPainter.begin(&pdfWriter);
//restore screen rendering quality
else if (pageIndex < existingPageCount)
pdfWriter.newPage();
// Render the scene
scene->render(&pdfPainter, QRectF(), scene->normalizedSceneRect());
// Restore screen rendering quality
scene->setRenderingContext(UBGraphicsScene::Screen);
scene->setRenderingQuality(UBItem::RenderingQualityNormal);
//restore background state
// Restore background state
scene->setBackground(isDark, isCrossed);
}
if(!painterNeedsBegin) pdfPainter.end();
if(!painterNeedsBegin)
pdfPainter.end();
return true;
}

Loading…
Cancel
Save