From 40817e73b42ade7a431f6565ccf8ecf02e78772f Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:26:10 +0200 Subject: [PATCH] Apply page layout settings to PDF format --- novelwriter/core/docbuild.py | 4 +--- novelwriter/formats/toqdoc.py | 44 +++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/novelwriter/core/docbuild.py b/novelwriter/core/docbuild.py index 664cc1a5..1556c5a5 100644 --- a/novelwriter/core/docbuild.py +++ b/novelwriter/core/docbuild.py @@ -301,6 +301,7 @@ class NWBuildDocument: self._build.getInt("odt.pageCountOffset"), ) + if isinstance(bldObj, (ToOdt, ToQTextDocument)): scale = nwLabels.UNIT_SCALE.get(self._build.getStr("format.pageUnit"), 1.0) pW, pH = nwLabels.PAPER_SIZE.get(self._build.getStr("format.pageSize"), (-1.0, -1.0)) bldObj.setPageLayout( @@ -312,9 +313,6 @@ class NWBuildDocument: scale*self._build.getFloat("format.rightMargin"), ) - if isinstance(bldObj, ToQTextDocument): - pass - filtered = self._build.buildItemFilter( self._project, withRoots=self._build.getBool("text.addNoteHeadings") ) diff --git a/novelwriter/formats/toqdoc.py b/novelwriter/formats/toqdoc.py index 4a46bdd5..03dea01b 100644 --- a/novelwriter/formats/toqdoc.py +++ b/novelwriter/formats/toqdoc.py @@ -27,8 +27,9 @@ import logging from pathlib import Path +from PyQt5.QtCore import QMarginsF, QSizeF from PyQt5.QtGui import ( - QColor, QFont, QFontMetricsF, QTextBlockFormat, QTextCharFormat, + QColor, QFont, QFontMetricsF, QPageSize, QTextBlockFormat, QTextCharFormat, QTextCursor, QTextDocument ) from PyQt5.QtPrintSupport import QPrinter @@ -90,8 +91,20 @@ class ToQTextDocument(Tokenizer): self._bold = QFont.Weight.Bold self._normal = QFont.Weight.Normal + self._pageSize = QPageSize(QPageSize.PageSizeId.A4) + self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0) + return + ## + # Properties + ## + + @property + def document(self) -> QTextDocument: + """Return the document.""" + return self._document + ## # Setters ## @@ -101,6 +114,14 @@ class ToQTextDocument(Tokenizer): self._theme = theme return + def setPageLayout( + self, width: float, height: float, top: float, bottom: float, left: float, right: float + ) -> None: + """Set the document page size and margins in millimetres.""" + self._pageSize = QPageSize(QSizeF(width, height), QPageSize.Unit.Millimeter) + self._pageMargins = QMarginsF(left, top, right, bottom) + return + ## # Class Methods ## @@ -191,19 +212,6 @@ class ToQTextDocument(Tokenizer): return - ## - # Properties - ## - - @property - def document(self) -> QTextDocument: - """Return the document.""" - return self._document - - ## - # Class Methods - ## - def doConvert(self) -> None: """Write text tokens into the document.""" if not self._init: @@ -288,11 +296,17 @@ class ToQTextDocument(Tokenizer): def saveDocument(self, path: Path) -> None: """Save the document to a PDF file.""" + m = self._pageMargins + printer = QPrinter(QPrinter.PrinterMode.PrinterResolution) printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat) - printer.setPaperSize(QPrinter.PageSize.A4) + printer.setPageSize(self._pageSize) + printer.setPageMargins(m.left(), m.top(), m.right(), m.bottom(), QPrinter.Unit.Millimeter) printer.setOutputFileName(str(path)) + + self._document.setPageSize(self._pageSize.size(QPageSize.Unit.Point)) self._document.print(printer) + return def appendFootnotes(self) -> None: