Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Print pdf file to local printer in Windows



  • I'm trying to send a PDF file from my local hard disk to my local printer (HP Deskjet) under Windows 10 in C++ but I'm having problems because the printing dialog is not displayed and nothing is printed.

        QTextDocument document;
        document.setHtml(html);
    
        QPrinter printer(QPrinter::PrinterResolution);
        printer.setOutputFormat(QPrinter::PdfFormat);
        printer.setPaperSize(QPrinter::A4);
        printer.setOutputFileName("debug/scheda.pdf");
        printer.setPageMargins(QMarginsF(15, 15, 15, 15));
    
        document.print(&printer);
    
        QPrinter printer1(QPrinter::HighResolution);
                printer1.setOutputFormat(QPrinter::PdfFormat);
                printer1.setOutputFileName("scheda.pdf");
                QPrintDialog *dlg = new QPrintDialog(&printer1,this);
                dlg->setWindowTitle(QObject::tr("Print Document"));
    
                if(dlg->exec() == QDialog::Accepted) {
                    document.print(&printer1);
                }
                delete dlg;
    

    The first piece of code works properly because the HTML document is created and saved to my local hard drive.
    The second piece of code doesn't work and the printing dialog is not displayed.

    I also get this error:

    QPrintDialog: Cannot be used on non-native printers
    

    How can I solve it? I tried to search in this forum about the same problem and I tried different solutions but nothing worked.


  • Lifetime Qt Champion

    Hi,

    Based on your code, you are in fact generating a PDF from some html input. Hence you have a file output.

    The fact that there's no dialog is related to that as well.



  • Thank you for your answer and your support.

    I would like to both generate a PDF and load a PDF file from the disk and to send it to my printer.

    How can I do it?



  • @Marcus-Barnet
    I think you should at least delete these two lines

    printer1.setOutputFormat(QPrinter::PdfFormat);
    printer1.setOutputFileName("scheda.pdf");
    

    That won't load your pdf file.
    Without them you can have your dialog displayed.


Log in to reply