0

I am trying to print page from url using QWebEngineView in PyQt5. In preview QWebEngineView.show() it shows with normal quality, but when I print the page the quality is the lowest

First I thought that the problem is the bad printing device. But when I print the content of QTextEdit() using QTextEdit().document().print_() the quality was perfect


import os
import sys
import argparse

from PyQt5.QtCore import QUrl
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
import win32print

class PrinterView(QWebEngineView):
    def __init__(self, url=None, filename=None, do_preview=False, parent=None):
        super(PrinterView, self).__init__(parent)
        win32print.SetDefaultPrinter("XP-80C") 
        self.do_preview = do_preview
       
        self.load(QUrl('https://stackoverflow.com/questions/41710994/create-a-pdf-with-fillable-fields-python'))
        self.setZoomFactor(1)
      
      
        self.printer = QPrinter(QPrinter.HighResolution)
        self.printer.setOrientation(QPrinter.Portrait)
        self.printer.setFullPage(True)

        self.loadFinished.connect(self.onloadFinished)

    def print_completed(self, dsd):
        pass

    def onloadFinished(self):
        if self.do_preview:
            self.show()
        else:
            self.page().print(self.printer, self.print_completed)

Here is the printed version of stackoverflow page

Printed version

Scrz
  • 195
  • 3
  • 11
  • It might be a problem related to the font color, and the printer driver may try to use that mode when the color is not plain black. Ensure that you're using [`setColorMode`](https://doc.qt.io/qt-5/qprinter.html#setColorMode) to `GrayScale`, and also do some tests with local files using different colors to see the result. You may need to override the font color with a custom stylesheet to ensure that it's completely black, not a lighter shade. – musicamante Apr 29 '22 at 23:26
  • @musicamante, Thank you for your response. I tried to set color mode, but nothing changes – Scrz Apr 29 '22 at 23:32
  • Then I suggest to do some tests with local files and see the result as suggested above. Also, check other options in the printer, possibly using [QPrinterDialog](https://doc.qt.io/qt-5/qprintdialog.html), you might find some options that can be set for the QPrinter object. – musicamante Apr 29 '22 at 23:42
  • @musicamante I did some tests with local files, I printed an .pdf file and there is low quality only on images and snapshots . It seems when printing from QWebEnginePage it prints page as image – Scrz Apr 30 '22 at 00:03
  • Did you test with *local files* on QWebEnginePage? That's the issue, if you try to use QPrinter on a different object (like QTextDocument) it won't matter, the source of the problem is clearly the web engine – musicamante Apr 30 '22 at 00:07
  • @musicamante, Yes, of course first I tried with local files with `QWebEngineView.setHtml()`, I did not bring it here in stackoverflow because it would be a long code. After printing QWebEngineView into PDF I understand that the content was the image. – Scrz Apr 30 '22 at 06:01

0 Answers0