OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

pyside6 rendering a QPdfView to a QPrintPreviewDialog

  • Thread starter Thread starter Clueless
  • Start date Start date
C

Clueless

Guest
so first ill start off and say im fairly new to QT or pyside6 in general. this is my class

Code:
class Viewer(QPdfView):
    def __init__(self, parent: QWidget | None = None):
        super().__init__(parent)
        self.setPageMode(QPdfView.PageMode.MultiPage)
        self.setVisible(False)

    def print_(self):
        printer = QPrinter(QPrinter.PrinterMode.HighResolution)
        dialog = QPrintPreviewDialog(printer, self.parent())
        dialog.paintRequested.connect(self.draw)
        dialog.exec()

    def draw(self, printer: QPrinter):
        painter = QPainter(printer)
        painter.setRenderHints(
            QPainter.RenderHint.Antialiasing | QPainter.RenderHint.TextAntialiasing |
            QPainter.RenderHint.SmoothPixmapTransform | QPainter.RenderHint.LosslessImageRendering,
            True
        )
        document = self.document()
        options = QPdfDocumentRenderOptions()
        options.setRenderFlags(
            QPdfDocumentRenderOptions.RenderFlag.ImageAliased |
            QPdfDocumentRenderOptions.RenderFlag.TextAliased |
            QPdfDocumentRenderOptions.RenderFlag.ForceHalftone
        )

        print_range = printer.pageRanges()
        rng = print_range.toString().split("-")
        rng = [i.strip() for i in rng if i.strip().isdigit()]
        if len(rng) == 2:
            range_ = range(int(rng[0]) - 1, int(rng[1]))
        elif len(rng) == 1:
            range_ = tuple([int(rng[0]) - 1])
        else:
            range_ = range(document.pageCount())
        if max(range_) > document.pageCount():
            range_ = range(document.pageCount())
        layout = printer.pageLayout()
        if layout.orientation() == QPageLayout.Orientation.Landscape:
            printer.setPageOrientation(QPageLayout.Orientation.Landscape)
        if layout.pageSize().isValid():
            printer.setPageSize(layout.pageSize())
        for i in range_:
            if i > 0:
                printer.newPage()
            printer.setPageMargins(layout.margins())
            target_rect = printer.pageRect(QPrinter.DevicePixel)
            image_size = QSize(int(target_rect.width()), int(target_rect.height()))
            image = document.render(i, image_size, options)
            if not image.isNull():
                painter.drawImage(target_rect, image)
        painter.end()

right now the current solution is working very slow as i render each page to an image and then render it to the printer, which takes forever, i thought maybe i can somehow render the QPdfView it self to the printer but couldnt figure out how any help will be greatly appricated!

wrote the self.draw method, it works, but very slow and inefficent
<p>so first ill start off and say im fairly new to QT or pyside6 in general.
this is my class</p>
<pre class="lang-py prettyprint-override"><code>class Viewer(QPdfView):
def __init__(self, parent: QWidget | None = None):
super().__init__(parent)
self.setPageMode(QPdfView.PageMode.MultiPage)
self.setVisible(False)

def print_(self):
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
dialog = QPrintPreviewDialog(printer, self.parent())
dialog.paintRequested.connect(self.draw)
dialog.exec()

def draw(self, printer: QPrinter):
painter = QPainter(printer)
painter.setRenderHints(
QPainter.RenderHint.Antialiasing | QPainter.RenderHint.TextAntialiasing |
QPainter.RenderHint.SmoothPixmapTransform | QPainter.RenderHint.LosslessImageRendering,
True
)
document = self.document()
options = QPdfDocumentRenderOptions()
options.setRenderFlags(
QPdfDocumentRenderOptions.RenderFlag.ImageAliased |
QPdfDocumentRenderOptions.RenderFlag.TextAliased |
QPdfDocumentRenderOptions.RenderFlag.ForceHalftone
)

print_range = printer.pageRanges()
rng = print_range.toString().split("-")
rng = [i.strip() for i in rng if i.strip().isdigit()]
if len(rng) == 2:
range_ = range(int(rng[0]) - 1, int(rng[1]))
elif len(rng) == 1:
range_ = tuple([int(rng[0]) - 1])
else:
range_ = range(document.pageCount())
if max(range_) > document.pageCount():
range_ = range(document.pageCount())
layout = printer.pageLayout()
if layout.orientation() == QPageLayout.Orientation.Landscape:
printer.setPageOrientation(QPageLayout.Orientation.Landscape)
if layout.pageSize().isValid():
printer.setPageSize(layout.pageSize())
for i in range_:
if i > 0:
printer.newPage()
printer.setPageMargins(layout.margins())
target_rect = printer.pageRect(QPrinter.DevicePixel)
image_size = QSize(int(target_rect.width()), int(target_rect.height()))
image = document.render(i, image_size, options)
if not image.isNull():
painter.drawImage(target_rect, image)
painter.end()
</code></pre>
<p>right now the current solution is working very slow as i render each page to an image and then render it to the printer, which takes forever, i thought maybe i can somehow render the QPdfView it self to the printer but couldnt figure out how
any help will be greatly appricated!</p>
<p>wrote the self.draw method, it works, but very slow and inefficent</p>
 

Latest posts

Top