src/eric7/WebBrowser/Tools/PrintToPdfDialog.py

branch
eric7
changeset 10240
cbd566d49a88
parent 9653
e67609152c5e
child 10439
21c28b0f9e41
diff -r 63fe209a1e71 -r cbd566d49a88 src/eric7/WebBrowser/Tools/PrintToPdfDialog.py
--- a/src/eric7/WebBrowser/Tools/PrintToPdfDialog.py	Thu Oct 12 17:03:41 2023 +0200
+++ b/src/eric7/WebBrowser/Tools/PrintToPdfDialog.py	Thu Oct 12 17:05:08 2023 +0200
@@ -7,11 +7,9 @@
 Module implementing a dialog to enter the data for printing a web page to PDF.
 """
 
-import os
-
-from PyQt6.QtCore import QMarginsF, QStandardPaths, pyqtSlot
-from PyQt6.QtGui import QPageLayout, QPageSize
-from PyQt6.QtPrintSupport import QPageSetupDialog, QPrinter
+from PyQt6.QtCore import pyqtSlot
+from PyQt6.QtGui import QPageLayout
+from PyQt6.QtPrintSupport import QPageSetupDialog
 from PyQt6.QtWidgets import QDialog
 
 from eric7.EricWidgets.EricPathPicker import EricPathPickerModes
@@ -25,35 +23,23 @@
     PDF.
     """
 
-    def __init__(self, filePath, parent=None):
+    def __init__(self, printer, parent=None):
         """
         Constructor
 
-        @param filePath path of the file to write into
-        @type str
+        @param printer reference to an initialized QPrinter object
+        @type QPrinter
         @param parent reference to the parent widget
         @type QWidget
         """
         super().__init__(parent)
         self.setupUi(self)
 
+        self.__printer = printer
+
         self.pdfFilePicker.setMode(EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE)
         self.pdfFilePicker.setFilters(self.tr("PDF Files (*.pdf);;All Files (*)"))
-        if not os.path.isabs(filePath):
-            documentsPath = QStandardPaths.writableLocation(
-                QStandardPaths.StandardLocation.DocumentsLocation
-            )
-            if documentsPath:
-                filePath = os.path.join(documentsPath, filePath)
-            else:
-                filePath = os.path.abspath(filePath)
-        self.pdfFilePicker.setText(filePath, toNative=True)
-
-        self.__currentPageLayout = QPageLayout(
-            QPageSize(QPageSize.PageSizeId.A4),
-            QPageLayout.Orientation.Portrait,
-            QMarginsF(0.0, 0.0, 0.0, 0.0),
-        )
+        self.pdfFilePicker.setText(self.__printer.outputFileName(), toNative=True)
 
         self.__updatePageLayoutLabel()
 
@@ -62,12 +48,8 @@
         """
         Private slot to define the page layout.
         """
-        printer = QPrinter()
-        printer.setPageLayout(self.__currentPageLayout)
-
-        dlg = QPageSetupDialog(printer, self)
+        dlg = QPageSetupDialog(self.__printer, self)
         if dlg.exec() == QDialog.DialogCode.Accepted:
-            self.__currentPageLayout = printer.pageLayout()
             self.__updatePageLayoutLabel()
 
     def __updatePageLayoutLabel(self):
@@ -77,14 +59,14 @@
         orientation = (
             self.tr("Portrait")
             if (
-                self.__currentPageLayout.orientation()
+                self.__printer.pageLayout().orientation()
                 == QPageLayout.Orientation.Portrait
             )
             else self.tr("Landscape")
         )
         self.pageLayoutLabel.setText(
             self.tr("{0}, {1}", "page size, page orientation").format(
-                self.__currentPageLayout.pageSize().name(), orientation
+                self.__printer.pageLayout().pageSize().name(), orientation
             )
         )
 
@@ -97,5 +79,5 @@
         """
         return (
             self.pdfFilePicker.text(toNative=True),
-            self.__currentPageLayout,
+            self.__printer.pageLayout(),
         )

eric ide

mercurial