Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. help with printing

help with printing

Scheduled Pinned Locked Moved Unsolved Qt for Python
2 Posts 1 Posters 350 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    danoni
    wrote on last edited by
    #1

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

    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.

    i feel like i dont need to render each page to an image as the QPdfView it self does not do that, it loads it much faster and not as images (text is searchable etc)

    also another 2 things id appricate help with

    1. how to render and print from QWebEngineView
    2. the preview it self is very blurry until i zoom in

    any help will be greatly appricated!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      danoni
      wrote on last edited by
      #2

      By the preview I mean the QPrintPreviewWidget

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved