Improved PDF export. Now it exports in proper size.

preferencesAboutTextFull
Anatoly Mihalchenko 13 years ago
parent 3aaedfa7e9
commit 2ea30bf987
  1. 97
      src/adaptors/UBExportFullPDF.cpp
  2. 7
      src/adaptors/UBExportFullPDF.h
  3. 1
      src/adaptors/UBExportPDF.cpp

@ -46,7 +46,10 @@ using namespace merge_lib;
UBExportFullPDF::UBExportFullPDF(QObject *parent) UBExportFullPDF::UBExportFullPDF(QObject *parent)
: UBExportAdaptor(parent) : UBExportAdaptor(parent)
{ {
// NOOP //need to calculate screen resolution
QDesktopWidget* desktop = UBApplication::desktop();
int dpiCommon = (desktop->physicalDpiX() + desktop->physicalDpiY()) / 2;
mScaleFactor = 72.0f / dpiCommon;
} }
@ -56,7 +59,7 @@ UBExportFullPDF::~UBExportFullPDF()
} }
void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, QString filename) void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, const QString& filename)
{ {
if (!pDocumentProxy || filename.length() == 0 || pDocumentProxy->pageCount() == 0) if (!pDocumentProxy || filename.length() == 0 || pDocumentProxy->pageCount() == 0)
return; return;
@ -70,9 +73,6 @@ void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, QString fi
pdfPrinter.setOutputFileName(filename); pdfPrinter.setOutputFileName(filename);
pdfPrinter.setFullPage(true); pdfPrinter.setFullPage(true);
const qreal margin = UBSettings::settings()->pdfMargin->get().toDouble() * pdfPrinter.resolution() / 25.4;
mMargin = margin;
QPainter* pdfPainter = 0; QPainter* pdfPainter = 0;
for(int pageIndex = 0 ; pageIndex < pDocumentProxy->pageCount(); pageIndex++) for(int pageIndex = 0 ; pageIndex < pDocumentProxy->pageCount(); pageIndex++)
@ -87,54 +87,20 @@ void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, QString fi
scene->setRenderingQuality(UBItem::RenderingQualityHigh); scene->setRenderingQuality(UBItem::RenderingQualityHigh);
scene->setRenderingContext(UBGraphicsScene::PdfExport); scene->setRenderingContext(UBGraphicsScene::PdfExport);
UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject()); QSize pageSize = scene->nominalSize();
if (pdfItem)
{
QSizeF sceneItemsBound = scene->itemsBoundingRect().size();
qreal ratio = (qreal)pdfPrinter.resolution() / 72.0;
QSizeF scaled = sceneItemsBound * ratio;
pdfPrinter.setPaperSize(scaled, QPrinter::DevicePixel);
if (pageIndex != 0)
pdfPrinter.newPage();
if (!pdfPainter)
pdfPainter = new QPainter(&pdfPrinter);
//render to PDF
scene->render(pdfPainter, QRectF(0, 0, sceneItemsBound.width() * ratio
, sceneItemsBound.height() * ratio), scene->itemsBoundingRect());
mHasPDFBackgrounds = true;
}
else
{
if (UBSettings::settings()->pdfPageFormat->get().toString() == "Letter")
pdfPrinter.setPageSize(QPrinter::Letter);
else
pdfPrinter.setPageSize(QPrinter::A4);
QSize docSize = pDocumentProxy->defaultDocumentSize(); UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject());
if(docSize.width() > docSize.height())
{
pdfPrinter.setOrientation(QPrinter::Landscape);
}
if (pageIndex != 0) if (pdfItem) mHasPDFBackgrounds = true;
pdfPrinter.newPage();
pdfPrinter.setPaperSize(QSizeF(pageSize.width()*mScaleFactor, pageSize.height()*mScaleFactor), QPrinter::Point);
mDefaultPageRect = pdfPrinter.paperRect(); if (!pdfPainter) pdfPainter = new QPainter(&pdfPrinter);
QRectF paperRect = mDefaultPageRect.adjusted(margin, margin, -margin, -margin);
QRectF normalized = scene->normalizedSceneRect(paperRect.width() / paperRect.height());
if (!pdfPainter) if (pageIndex != 0) pdfPrinter.newPage();
pdfPainter = new QPainter(&pdfPrinter);
//render to PDF //render to PDF
scene->render(pdfPainter, paperRect, normalized); scene->render(pdfPainter);
}
//restore screen rendering quality //restore screen rendering quality
scene->setRenderingContext(UBGraphicsScene::Screen); scene->setRenderingContext(UBGraphicsScene::Screen);
@ -144,8 +110,7 @@ void UBExportFullPDF::saveOverlayPdf(UBDocumentProxy* pDocumentProxy, QString fi
scene->setBackground(isDark, isCrossed); scene->setBackground(isDark, isCrossed);
} }
if (pdfPainter) if (pdfPainter) delete pdfPainter;
delete pdfPainter;
} }
@ -171,12 +136,10 @@ void UBExportFullPDF::persist(UBDocumentProxy* pDocumentProxy)
} }
void UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, QString filename) void UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QString& filename)
{ {
QFile file(filename); QFile file(filename);
if (file.exists()) if (file.exists()) file.remove();
file.remove();
QString overlayName = filename; QString overlayName = filename;
overlayName.replace(".pdf", "_overlay.pdf"); overlayName.replace(".pdf", "_overlay.pdf");
@ -209,31 +172,27 @@ void UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, QString
UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex); UBGraphicsScene* scene = UBPersistenceManager::persistenceManager()->loadDocumentScene(pDocumentProxy, pageIndex);
UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject()); UBGraphicsPDFItem *pdfItem = qgraphicsitem_cast<UBGraphicsPDFItem*>(scene->backgroundObject());
if (pdfItem) QSize pageSize = scene->nominalSize();
if (pdfItem)
{ {
QString pdfName = UBPersistenceManager::objectDirectory + "/" + pdfItem->fileUuid().toString() + ".pdf"; QString pdfName = UBPersistenceManager::objectDirectory + "/" + pdfItem->fileUuid().toString() + ".pdf";
QString backgroundPath = pDocumentProxy->persistencePath() + "/" + pdfName; QString backgroundPath = pDocumentProxy->persistencePath() + "/" + pdfName;
QPointF boudingRectBottomLeft = scene->itemsBoundingRect().bottomLeft(); QPointF boudingRectBottomLeft = scene->itemsBoundingRect().bottomLeft();
QPointF pdfItemBottomLeft = pdfItem->sceneBoundingRect().bottomLeft(); QPointF pdfItemBottomLeft = pdfItem->sceneBoundingRect().bottomLeft();
QPointF offset = pdfItemBottomLeft - boudingRectBottomLeft;
qDebug() << "scene->itemsBoundingRect()" << scene->itemsBoundingRect(); qDebug() << "scene->itemsBoundingRect()" << scene->itemsBoundingRect();
qDebug() << "pdfItem->boundingRect()" << pdfItem->boundingRect(); qDebug() << "pdfItem->boundingRect()" << pdfItem->boundingRect();
qDebug() << "pdfItem->sceneBoundingRect()" << pdfItem->sceneBoundingRect(); qDebug() << "pdfItem->sceneBoundingRect()" << pdfItem->sceneBoundingRect();
qDebug() << offset;
TransformationDescription baseTrans(offset.x(), offset.y() * -1, 1, 0);
//TransformationDescription baseTrans(0, 0, 1, 0);
TransformationDescription overlayTrans(0, 0, 1, 0);
MergePageDescription pageDescription(scene->itemsBoundingRect().width(), MergePageDescription pageDescription(pageSize.width() * mScaleFactor,
scene->itemsBoundingRect().height(), pageSize.height() * mScaleFactor,
pdfItem->pageNumber(), pdfItem->pageNumber(),
QFile::encodeName(backgroundPath).constData(), QFile::encodeName(backgroundPath).constData(),
baseTrans, TransformationDescription(),
pageIndex + 1, pageIndex + 1,
overlayTrans, TransformationDescription(),
false, false); false, false);
mergeInfo.push_back(pageDescription); mergeInfo.push_back(pageDescription);
@ -242,12 +201,8 @@ void UBExportFullPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, QString
} }
else else
{ {
QRectF paperRect = mDefaultPageRect.adjusted(mMargin, mMargin, -mMargin, -mMargin); MergePageDescription pageDescription(pageSize.width() * mScaleFactor,
QRectF normalized = scene->normalizedSceneRect(paperRect.width() / paperRect.height()); pageSize.height() * mScaleFactor,
MergePageDescription pageDescription(normalized.width(),
normalized.height(),
0, 0,
"", "",
TransformationDescription(), TransformationDescription(),

@ -33,14 +33,13 @@ class UBExportFullPDF : public UBExportAdaptor
virtual QString exportExtention(); virtual QString exportExtention();
virtual void persist(UBDocumentProxy* pDocument); virtual void persist(UBDocumentProxy* pDocument);
virtual void persistsDocument(UBDocumentProxy* pDocument, QString filename); virtual void persistsDocument(UBDocumentProxy* pDocument, const QString& filename);
protected: protected:
void saveOverlayPdf(UBDocumentProxy* pDocumentProxy, QString filename); void saveOverlayPdf(UBDocumentProxy* pDocumentProxy, const QString& filename);
private: private:
QRect mDefaultPageRect; float mScaleFactor;
int mMargin;
bool mHasPDFBackgrounds; bool mHasPDFBackgrounds;
}; };

@ -101,7 +101,6 @@ void UBExportPDF::persistsDocument(UBDocumentProxy* pDocumentProxy, const QStrin
//setting page size to appropriate value //setting page size to appropriate value
pdfPrinter.setPaperSize(QSizeF(pageSize.width()*scaleFactor, pageSize.height()*scaleFactor), QPrinter::Point); pdfPrinter.setPaperSize(QSizeF(pageSize.width()*scaleFactor, pageSize.height()*scaleFactor), QPrinter::Point);
pdfPrinter.setOrientation((pageSize.width() > pageSize.height())? QPrinter::Landscape : QPrinter::Portrait);
if(painterNeedsBegin) painterNeedsBegin = !pdfPainter.begin(&pdfPrinter); if(painterNeedsBegin) painterNeedsBegin = !pdfPainter.begin(&pdfPrinter);
//render to PDF //render to PDF
scene->render(&pdfPainter); scene->render(&pdfPainter);

Loading…
Cancel
Save