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 7 years ago
parent b7f5cc2714
commit 716314fe73
  1. 49
      src/adaptors/UBExportPDF.cpp

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

Loading…
Cancel
Save