diff --git a/src/Mod/TechDraw/Gui/PagePrinter.cpp b/src/Mod/TechDraw/Gui/PagePrinter.cpp index b51bee0a60..3f7c86e7a0 100644 --- a/src/Mod/TechDraw/Gui/PagePrinter.cpp +++ b/src/Mod/TechDraw/Gui/PagePrinter.cpp @@ -52,7 +52,6 @@ #include #include "PagePrinter.h" -#include "QGITemplate.h" #include "QGSPage.h" #include "Rez.h" #include "ViewProviderPage.h" @@ -113,6 +112,7 @@ void PagePrinter::getPaperAttributes() m_orientation = attr.orientation; } +/// print the Page associated with the parent MDIViewPage as a Pdf file void PagePrinter::printPdf(std::string file) { // Base::Console().Message("PP::printPdf(%s)\n", file.c_str()); @@ -122,26 +122,34 @@ void PagePrinter::printPdf(std::string file) } QString filename = QString::fromUtf8(file.data(), file.size()); QPrinter printer(QPrinter::HighResolution); + QPdfWriter pdfWriter(filename); + QString documentName = QString::fromUtf8(m_vpPage->getDrawPage()->getNameInDocument()); + pdfWriter.setTitle(documentName); + pdfWriter.setResolution(printer.resolution()); - getPaperAttributes(); + PaperAttributes attr = getPaperAttributes(m_vpPage->getDrawPage()); + double width = attr.pagewidth; + double height = attr.pageheight; + QPageLayout pageLayout = printer.pageLayout(); + setPageLayout(pageLayout, m_vpPage->getDrawPage(), width, height); + pdfWriter.setPageLayout(pageLayout); - // setPdfVersion sets the printied PDF Version to comply with PDF/A-1b, more details under: https://www.kdab.com/creating-pdfa-documents-qt/ -// printer.setPdfVersion(QPagedPaintDevice::PdfVersion_A1b); - printer.setFullPage(true); - printer.setOutputFileName(filename); - printer.setPageOrientation(m_orientation); - printer.setPageSize(QPageSize(m_paperSize)); + // first page does not respect page layout unless painter is created after + // pdfWriter layout is established. + QPainter painter(&pdfWriter); - m_scene->setExportingPdf(true); - print(&printer); - m_scene->setExportingPdf(false); + QRectF sourceRect(0.0, Rez::guiX(-height), Rez::guiX(width), Rez::guiX(height)); + double dpmm = printer.resolution() / 25.4; + QRect targetRect(0, 0, width * dpmm, height * dpmm); + renderPage(m_vpPage, painter, sourceRect, targetRect); + painter.end(); } + /// print the Page associated with the parent MDIViewPage void PagePrinter::print(QPrinter* printer) { // Base::Console().Message("PP::print(printer)\n"); - QPainter painter(printer); QPageLayout pageLayout = printer->pageLayout(); TechDraw::DrawPage* dp = m_vpPage->getDrawPage(); @@ -150,18 +158,12 @@ void PagePrinter::print(QPrinter* printer) setPageLayout(pageLayout, dp, width, height); printer->setPageLayout(pageLayout); + QPainter painter(printer); + QRect targetRect = printer->pageLayout().fullRectPixels(printer->resolution()); QRectF sourceRect(0.0, Rez::guiX(-height), Rez::guiX(width), Rez::guiX(height)); - if (!printer->outputFileName().isEmpty()) { - // file name is not empty so we must be printing to pdf? - m_scene->setExportingPdf(true); - } renderPage(m_vpPage, painter, sourceRect, targetRect); painter.end(); - if (!printer->outputFileName().isEmpty()) { - // file name is not empty so we must be printing to pdf? - m_scene->setExportingPdf(false); - } } //static routine to print all pages in a document @@ -192,6 +194,9 @@ void PagePrinter::printAll(QPrinter* printer, App::Document* doc) //for some reason the first page doesn't obey the pageLayout, so we have to print //a sacrificial blank page, but we make it a feature instead of a bug by printing a //table of contents on the sacrificial page. + // Note: if the painter(printer) occurs after the printer->setPageLayout, then the + // first page will obey the layout. This would mean creating the painter inside the + // loop. if (firstTime) { firstTime = false; printBannerPage(printer, painter, pageLayout, doc, docObjs); @@ -247,6 +252,7 @@ void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) //for some reason the first page doesn't obey the pageLayout, so we have to print //a sacrificial blank page, but we make it a feature instead of a bug by printing a //table of contents on the sacrificial page. + // see the note about this in printAll() if (firstTime) { firstTime = false; printBannerPage(printer, painter, pageLayout, doc, docObjs); @@ -255,9 +261,7 @@ void PagePrinter::printAllPdf(QPrinter* printer, App::Document* doc) QRectF sourceRect(0.0, Rez::guiX(-height), Rez::guiX(width), Rez::guiX(height)); QRect targetRect(0, 0, width * dpmm, height * dpmm); - vpp->getQGSPage()->setExportingPdf(true); renderPage(vpp, painter, sourceRect, targetRect); - vpp->getQGSPage()->setExportingPdf(false); } painter.end(); } diff --git a/src/Mod/TechDraw/Gui/QGIViewPart.cpp b/src/Mod/TechDraw/Gui/QGIViewPart.cpp index c71a66824a..fe4e60d42a 100644 --- a/src/Mod/TechDraw/Gui/QGIViewPart.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewPart.cpp @@ -75,7 +75,7 @@ using DU = DrawUtil; const float lineScaleFactor = Rez::guiX(1.);// temp fiddle for devel -QGIViewPart::QGIViewPart() : m_isExporting(false) +QGIViewPart::QGIViewPart() { setCacheMode(QGraphicsItem::NoCache); setHandlesChildEvents(false); @@ -237,13 +237,7 @@ void QGIViewPart::drawAllFaces(void) if (fHatch->isSvgHatch()) { // svg tile hatch newFace->setFillMode(QGIFace::SvgFill); - if (getExporting()) { - // SVG hatches don't work correctly when exported to PDF, so we need - // to tell the face to use a bitmap substitution for the SVG - newFace->hideSvg(true); - } else { - newFace->hideSvg(false); - } + newFace->hideSvg(false); } else { //bitmap hatch newFace->setFillMode(QGIFace::BitmapFill); diff --git a/src/Mod/TechDraw/Gui/QGIViewPart.h b/src/Mod/TechDraw/Gui/QGIViewPart.h index aeb8312219..98f8745a7e 100644 --- a/src/Mod/TechDraw/Gui/QGIViewPart.h +++ b/src/Mod/TechDraw/Gui/QGIViewPart.h @@ -107,8 +107,6 @@ public: bool large_arc_flag, bool sweep_flag, double x, double y, double curx, double cury); - void setExporting(bool b) { m_isExporting = b; } - bool getExporting() { return m_isExporting; } protected: QPainterPath drawPainterPath(TechDraw::BaseGeomPtr baseGeom) const; @@ -128,8 +126,6 @@ protected: bool formatGeomFromCosmetic(std::string cTag, QGIEdge* item); bool formatGeomFromCenterLine(std::string cTag, QGIEdge* item); - bool m_isExporting; - private: QList deleteItems; PathBuilder* m_pathBuilder; diff --git a/src/Mod/TechDraw/Gui/QGIViewSection.cpp b/src/Mod/TechDraw/Gui/QGIViewSection.cpp index 2fcb617645..0e0f73dd87 100644 --- a/src/Mod/TechDraw/Gui/QGIViewSection.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewSection.cpp @@ -90,11 +90,6 @@ void QGIViewSection::drawSectionFace() newFace->setFillColor(faceColor); newFace->setFillStyle(Qt::SolidPattern); } else if (section->CutSurfaceDisplay.isValue("SvgHatch")) { - if (getExporting()) { - newFace->hideSvg(true); - } else { - newFace->hideSvg(false); - } newFace->setFillMode(QGIFace::SvgFill); newFace->setHatchColor(sectionVp->HatchColor.getValue()); newFace->setHatchScale(section->HatchScale.getValue()); diff --git a/src/Mod/TechDraw/Gui/QGSPage.cpp b/src/Mod/TechDraw/Gui/QGSPage.cpp index c7c319e592..c2b0391ed2 100644 --- a/src/Mod/TechDraw/Gui/QGSPage.cpp +++ b/src/Mod/TechDraw/Gui/QGSPage.cpp @@ -61,7 +61,6 @@ #include #include -#include "MDIViewPage.h" #include "QGIDrawingTemplate.h" #include "QGILeaderLine.h" #include "QGIProjGroup.h" @@ -1053,26 +1052,6 @@ void QGSPage::redraw1View(TechDraw::DrawView* dView) } } -void QGSPage::setExportingPdf(bool enable) -{ - QList sceneItems = items(); - std::vector dvps; - for (auto& qgi : sceneItems) { - QGIViewPart* qgiPart = dynamic_cast(qgi); - QGIRichAnno* qgiRTA = dynamic_cast(qgi); - if (qgiPart) { - qgiPart->setExporting(enable); - dvps.push_back(qgiPart); - } - if (qgiRTA) { - qgiRTA->setExportingPdf(enable); - } - } - for (auto& v : dvps) { - v->draw(); - } -} - // RichTextAnno needs to know when it is rendering an Svg as the font size // is handled differently in Svg compared to the screen or Pdf. void QGSPage::setExportingSvg(bool enable) diff --git a/src/Mod/TechDraw/Gui/QGSPage.h b/src/Mod/TechDraw/Gui/QGSPage.h index dfb1a6b899..2279de1fa1 100644 --- a/src/Mod/TechDraw/Gui/QGSPage.h +++ b/src/Mod/TechDraw/Gui/QGSPage.h @@ -132,7 +132,6 @@ public: TechDraw::DrawPage* getDrawPage(); - void setExportingPdf(bool enable); void setExportingSvg(bool enable); virtual void refreshViews();