0

I can't print my pdf file using QPrinter, I figured it needs to be set to html to print it and now I'm getting this error on terminal:

QPrintDialog: Cannot be used on non-native printers.

And does setting it to html like this change what my pdf file look like?.

def print_pdf(self, pdffile):
    printer = QPrinter()
    printer.setOutputFileName(pdffile)  
    printer.setPageSize(QPageSize(QPageSize.PageSizeId.A6))  

    # Create a QTextDocument to hold the contents of the PDF file
    document = QTextDocument()
    document.setHtml("<html><body><img src='file://" + pdffile + "'/></body></html>")  # Load PDF content

    # a print dialog
    print_dialog = QPrintDialog(printer, self)
    
  
    if print_dialog.exec() == QPrintDialog.DialogCode.Accepted:
        # Print the document
        document.print(printer)
9
  • Sorry but that code doesn't make a lot of sense: 1. QTextDocument cannot show PDF content; 2. Even if it were able to do it, PDF is not an image and cannot therefore use the <img> tag; 3. you're trying to print on a pdf file that you're also attempting to display inside the very document you're printing, which makes absolutely no sense at all. It's also unclear what you actually want to do: do you already have a PDF file that you want to print on a physical printer? Commented May 10, 2024 at 14:34
  • 2
    You could use the QPdfDocument class to render each page to a QImage and finally print those images to the QPrinter. In any case, you shall not call setOutputFileName(), since you obviously do not need to print to an output file. Commented May 10, 2024 at 14:59
  • 1
    yes that would be way easier, but the client seems to want it to remain on the app not relying on the browser Commented May 11, 2024 at 15:54
  • 1
    Does this answer your question? How to print pdf file in Qt Commented May 12, 2024 at 13:02
  • 1
    For Qt related stuff, there is not a great difference between C++ and python, I created a new answer under that question to include the QPdfDocument solution (which wasn't available a that time) described by musicamente in his comment. Commented May 12, 2024 at 13:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.