From 59503c21dc7f59c025d4ace2bfa57643b04dfdab Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 28 May 2023 09:07:57 +0530 Subject: [PATCH] PDF Output: Fix regression in previous release that caused blank pages when generating headers or footers Needed to prepend the header/footer content stream to the page content stream. Appending only worked with Qt >= 6.5 which is what is present on my dev machine, so didnt catch it. Before 6.5, when drawing header footers chromium does not occlude the body area even though nothing is drawn there, thus prepending is required. --- src/calibre/utils/podofo/impose.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/calibre/utils/podofo/impose.cpp b/src/calibre/utils/podofo/impose.cpp index e695fe9c0f..2923e6369f 100644 --- a/src/calibre/utils/podofo/impose.cpp +++ b/src/calibre/utils/podofo/impose.cpp @@ -6,6 +6,7 @@ */ #include "global.h" +#include #include using namespace pdf; @@ -16,10 +17,14 @@ impose_page(PdfMemDocument *doc, unsigned int dest_page_num, unsigned int src_pa auto xobj = doc->CreateXObjectForm(src_page.GetMediaBox(), "HeaderFooter"); xobj->FillFromPage(src_page); auto &dest = doc->GetPages().GetPageAt(dest_page_num); - PdfPainter painter; - painter.SetCanvas(dest); - painter.DrawXObject(*xobj, 0, 0); - painter.FinishDrawing(); + dest.GetOrCreateResources().AddResource("XObject", xobj->GetIdentifier(), xobj->GetObject().GetIndirectReference()); + // prepend the header footer xobject to the stream. This means header/footer is drawn first then the contents, which works + // since chromium does not draw in margin areas. The reverse, i.e. appending, does not work with older WebEngine before Qt 6.5. + PdfContents *contents = dest.GetContents(); + std::ostringstream s; + s << "q\n1 0 0 1 0 0 cm\n/" << xobj->GetIdentifier().GetString() << " Do\nQ\n" << contents->GetCopy(); + contents->Reset(); + contents->GetStreamForAppending().SetData(s.str()); } static PyObject*