void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, const QString& filename) { if (!pDocumentProxy || filename.length() == 0 || pDocumentProxy->pageCount() == 0) return; //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); 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(); bool isCrossed = scene->isCrossedBackground(); scene->setBackground(false, false); // set high res rendering scene->setRenderingQuality(UBItem::RenderingQualityHigh); scene->setRenderingContext(UBGraphicsScene::PdfExport); QSize pageSize = scene->nominalSize(); UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject()); if (pdfItem) mHasPDFBackgrounds = true; pdfPrinter.setPaperSize(QSizeF(pageSize.width()*mScaleFactor, pageSize.height()*mScaleFactor), QPrinter::Point); if (!pdfPainter) pdfPainter = new QPainter(&pdfPrinter); if (pageIndex != 0) pdfPrinter.newPage(); //render to PDF scene->setDrawingMode(true); scene->render(pdfPainter, QRectF(), scene->normalizedSceneRect()); //restore screen rendering quality scene->setRenderingContext(UBGraphicsScene::Screen); scene->setRenderingQuality(UBItem::RenderingQualityNormal); //restore background state scene->setDrawingMode(false); scene->setBackground(isDark, isCrossed); } if (pdfPainter) delete pdfPainter; }
void UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename) { QFile file(filename); if (file.exists()) file.remove(); QString overlayName = filename; overlayName.replace(".pdf", "_overlay.pdf"); QFile previousOverlay(overlayName); if (previousOverlay.exists()) previousOverlay.remove(); mHasPDFBackgrounds = false; saveOverlayPdf(pDocumentProxy, overlayName); if (!mHasPDFBackgrounds) { QFile f(overlayName); f.rename(filename); } else { Merger merger; try { merger.addOverlayDocument(QFile::encodeName(overlayName).constData()); MergeDescription mergeInfo; int existingPageCount = pDocumentProxy->pageCount(); for(int pageIndex = 0 ; pageIndex < existingPageCount; pageIndex++) { UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject()); QSize pageSize = scene->nominalSize(); if (pdfItem) { QString pdfName = UBPersistenceManager::objectDirectory + "/" + pdfItem->fileUuid().toString() + ".pdf"; QString backgroundPath = pDocumentProxy->persistencePath() + "/" + pdfName; qDebug() << "scene->itemsBoundingRect()" << scene->itemsBoundingRect(); qDebug() << "pdfItem->boundingRect()" << pdfItem->boundingRect(); qDebug() << "pdfItem->sceneBoundingRect()" << pdfItem->sceneBoundingRect(); MergePageDescription pageDescription(pageSize.width() * mScaleFactor, pageSize.height() * mScaleFactor, pdfItem->pageNumber(), QFile::encodeName(backgroundPath).constData(), TransformationDescription(), pageIndex + 1, TransformationDescription(), false, false); mergeInfo.push_back(pageDescription); merger.addBaseDocument(QFile::encodeName(backgroundPath).constData()); } else { MergePageDescription pageDescription(pageSize.width() * mScaleFactor, pageSize.height() * mScaleFactor, 0, "", TransformationDescription(), pageIndex + 1, TransformationDescription(), false, true); mergeInfo.push_back(pageDescription); } } merger.merge(QFile::encodeName(overlayName).constData(), mergeInfo); merger.saveMergedDocumentsAs(QFile::encodeName(filename).constData()); } catch(Exception e) { qDebug() << "PdfMerger failed to merge documents to " << filename << " - Exception : " << e.what(); // default to raster export UBExportPDF::persistsDocument(pDocumentProxy, filename); } if (!UBApplication::app()->isVerbose()) { QFile::remove(overlayName); } } }