From 0530ab151b15682954f106d783530bdff77578a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fauconnier?= Date: Fri, 29 Apr 2022 14:43:45 +0200 Subject: [PATCH] add a setting to enable/disable pdfMerger use --- resources/etc/OpenBoard.config | 1 + src/adaptors/UBExportFullPDF.cpp | 107 ++++++++++++++++++------------- src/core/UBSettings.cpp | 1 + src/core/UBSettings.h | 1 + 4 files changed, 65 insertions(+), 45 deletions(-) diff --git a/resources/etc/OpenBoard.config b/resources/etc/OpenBoard.config index 77eea8d1..a3ba2530 100644 --- a/resources/etc/OpenBoard.config +++ b/resources/etc/OpenBoard.config @@ -129,6 +129,7 @@ ExportBackgroundColor=false Margin=20 PageFormat=A4 Resolution=300 +UsePDFMerger=true ZoomBehavior=4 [Podcast] diff --git a/src/adaptors/UBExportFullPDF.cpp b/src/adaptors/UBExportFullPDF.cpp index f15afb2a..2f7c0ba9 100644 --- a/src/adaptors/UBExportFullPDF.cpp +++ b/src/adaptors/UBExportFullPDF.cpp @@ -82,67 +82,84 @@ void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, const QStr if (!pDocumentProxy || filename.length() == 0 || pDocumentProxy->pageCount() == 0) return; - //PDF - qDebug() << "exporting document to PDF Merger" << filename; - QPrinter pdfPrinter; + /* + PDFMerger is supposed to be working only for PDFs using 1.0 to 1.4 standard, but I encountered no issue using 1.7 documents. + Just tested with a few documents though so it could be a total mess to restore it without any possibility to disable it if an issue is encountered + A new setting is introduced to handle that possibility. + Also, calling simpleExporter here instead of the following code was done because I (wrongly) assumed that both codes was doing nearly the same thing, as I never + tested to remove the part of pdf-merger throwing an exception if pdf version is > 1.4 and that calling the following lines without modifying the version check + throws an exception and finally calls simpleExporter->persistsDocument + calling simpleExporter directly also resulted in fixing two issues where pdf overlay could be badly scaled (not matching with scale of annotations) + */ + bool usePDFMerger = UBSettings::settings()->pdfUsePDFMerger->get().toBool(); + if (usePDFMerger) + { + //PDF + qDebug() << "exporting document to PDF Merger" << filename; + QPrinter pdfPrinter; - pdfPrinter.setOutputFormat(QPrinter::PdfFormat); - pdfPrinter.setResolution(UBSettings::settings()->pdfResolution->get().toInt()); - pdfPrinter.setOutputFileName(filename); - pdfPrinter.setFullPage(true); + pdfPrinter.setOutputFormat(QPrinter::PdfFormat); + pdfPrinter.setResolution(UBSettings::settings()->pdfResolution->get().toInt()); + pdfPrinter.setOutputFileName(filename); + pdfPrinter.setFullPage(true); - QPainter* pdfPainter = 0; + QPainter* pdfPainter = 0; - for(int pageIndex = 0 ; pageIndex < pDocumentProxy->pageCount(); pageIndex++) - { - UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); - // set background to white, no grid for PDF output - bool isDark = scene->isDarkBackground(); - UBPageBackground pageBackground = scene->pageBackground(); + for(int pageIndex = 0 ; pageIndex < pDocumentProxy->pageCount(); pageIndex++) + { + UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); + // set background to white, no grid for PDF output + bool isDark = scene->isDarkBackground(); + UBPageBackground pageBackground = scene->pageBackground(); - bool exportDark = isDark && UBSettings::settings()->exportBackgroundColor->get().toBool(); + bool exportDark = isDark && UBSettings::settings()->exportBackgroundColor->get().toBool(); - if (UBSettings::settings()->exportBackgroundGrid->get().toBool()) - { - scene->setBackground(exportDark, pageBackground); - } - else - { - scene->setBackground(exportDark, UBPageBackground::plain); - } + if (UBSettings::settings()->exportBackgroundGrid->get().toBool()) + { + scene->setBackground(exportDark, pageBackground); + } + else + { + scene->setBackground(exportDark, UBPageBackground::plain); + } - // set high res rendering - scene->setRenderingQuality(UBItem::RenderingQualityHigh, UBItem::CacheNotAllowed); - scene->setRenderingContext(UBGraphicsScene::PdfExport); + // set high res rendering + scene->setRenderingQuality(UBItem::RenderingQualityHigh, UBItem::CacheNotAllowed); + scene->setRenderingContext(UBGraphicsScene::PdfExport); - // 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(); + // 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(); - UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast(scene->backgroundObject()); + UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast(scene->backgroundObject()); - if (pdfItem) mHasPDFBackgrounds = true; + if (pdfItem) mHasPDFBackgrounds = true; - pdfPrinter.setPaperSize(QSizeF(pageSize.width()*mScaleFactor, pageSize.height()*mScaleFactor), QPrinter::Point); + pdfPrinter.setPaperSize(QSizeF(pageSize.width()*mScaleFactor, pageSize.height()*mScaleFactor), QPrinter::Point); - if (!pdfPainter) pdfPainter = new QPainter(&pdfPrinter); + if (!pdfPainter) pdfPainter = new QPainter(&pdfPrinter); - if (pageIndex != 0) pdfPrinter.newPage(); + if (pageIndex != 0) pdfPrinter.newPage(); - //render to PDF - scene->setDrawingMode(true); - scene->render(pdfPainter, QRectF(), scene->normalizedSceneRect()); + //render to PDF + scene->setDrawingMode(true); + scene->render(pdfPainter, QRectF(), scene->normalizedSceneRect()); - //restore screen rendering quality - scene->setRenderingContext(UBGraphicsScene::Screen); - scene->setRenderingQuality(UBItem::RenderingQualityNormal, UBItem::CacheAllowed); + //restore screen rendering quality + scene->setRenderingContext(UBGraphicsScene::Screen); + scene->setRenderingQuality(UBItem::RenderingQualityNormal, UBItem::CacheAllowed); - //restore background state - scene->setDrawingMode(false); - scene->setBackground(isDark, pageBackground); - } + //restore background state + scene->setDrawingMode(false); + scene->setBackground(isDark, pageBackground); + } - if (pdfPainter) delete pdfPainter; + if (pdfPainter) delete pdfPainter; + } + else + { + mSimpleExporter->persistsDocument(pDocumentProxy, filename); + } } diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index c98f1d70..d3635957 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -405,6 +405,7 @@ void UBSettings::init() pdfMargin = new UBSetting(this, "PDF", "Margin", "20"); pdfPageFormat = new UBSetting(this, "PDF", "PageFormat", "A4"); + pdfUsePDFMerger = new UBSetting(this, "PDF", "UsePDFMerger", "true"); pdfResolution = new UBSetting(this, "PDF", "Resolution", "300"); pdfZoomBehavior = new UBSetting(this, "PDF", "ZoomBehavior", "4"); diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index 6067db5b..67b452f0 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -357,6 +357,7 @@ class UBSettings : public QObject UBSetting* svgViewBoxMargin; UBSetting* pdfMargin; UBSetting* pdfPageFormat; + UBSetting* pdfUsePDFMerger; UBSetting* pdfResolution; UBSetting* pdfZoomBehavior;