Apply page layout settings to PDF format

This commit is contained in:
Veronica Berglyd Olsen
2024-10-16 17:26:10 +02:00
parent 17c93c2172
commit 40817e73b4
2 changed files with 30 additions and 18 deletions
+1 -3
View File
@@ -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")
)
+29 -15
View File
@@ -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: