Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Print pdf file to local printer in Windows
Forum Updated to NodeBB v4.3 + New Features

Print pdf file to local printer in Windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.8k Views 2 Watching
  • 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 Offline
    M Offline
    Marcus Barnet
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      1
      • M Offline
        M Offline
        Marcus Barnet
        wrote on last edited by
        #3

        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
        0
        • M Marcus Barnet

          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 Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

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

          • Login

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