Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Print pdf file to local printer in Windows

    General and Desktop
    3
    4
    880
    Loading More Posts
    • 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.
    • M
      Marcus Barnet last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • M
          Marcus Barnet last edited by

          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?

          B 1 Reply Last reply Reply Quote 0
          • B
            Bonnie @Marcus Barnet last edited by Bonnie

            @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.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post