Qt Forum

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

    Qt Academy Launch in California!

    Unsolved Qt printing document to PDF crashes

    General and Desktop
    print-to-pdf printer
    2
    8
    445
    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.
    • A
      Alex Lyapin last edited by

      Hi all,

      I am trying to run official example for generating pdf with Qt from QtWiki but getting crash with code -1073740940. Exact crash goes at line QPrinter printer(QPrinter::PrinterResolution); .pro file has printsupport as well.

      I am not changing anything given by example's code:

      #include <QtWidgets>
      #ifndef QT_NO_PRINTER
      #include <QPrinter>
      #endif
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
      
          QString fileName = QFileDialog::getSaveFileName((QWidget* )0, "Export PDF", QString(), "*.pdf");
          if (QFileInfo(fileName).suffix().isEmpty()) { fileName.append(".pdf"); }
      
          QPrinter printer(QPrinter::PrinterResolution);
          printer.setOutputFormat(QPrinter::PdfFormat);
          printer.setPaperSize(QPrinter::A4);
          printer.setOutputFileName(fileName);
      
          QTextDocument doc;
          doc.setHtml("<h1>Hello, World!</h1>\n<p>Lorem ipsum dolor sit amet, consectitur adipisci elit.</p>");
          doc.setPageSize(printer.pageRect().size()); // This is necessary if you want to hide the page number
          doc.print(&printer);
      }
      

      I have "Microsoft print to pdf" in devices just in case...

      Any thoughts?

      Thanks.

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

        Hi,

        Which version of Qt ?
        Which compiler ?
        Which version of Windows ?

        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 0
        • A
          Alex Lyapin last edited by

          @SGaist , thanks for the reply.

          I'm using:

          • Qt 5.14.1
          • MinGW 64 bit
          • Win 10 64 bit
          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            What is the stack trace ?

            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 0
            • A
              Alex Lyapin last edited by

              Line 13 in main.cpp is:

              QPrinter printer(QPrinter::PrinterResolution);
              

              And this is the debugger stack:

              1  ntdll!RtlIsNonEmptyDirectoryReparsePointAllowed                                                    0x7ffa1b1b91b3 
              2  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1c15e2 
              3  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1c18ea 
              4  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1ca8a9 
              5  ntdll!RtlGetCurrentServiceSessionId                                                                0x7ffa1b10253a 
              6  ntdll!RtlGetCurrentServiceSessionId                                                                0x7ffa1b100790 
              7  ntdll!RtlFreeHeap                                                                                  0x7ffa1b0ffb91 
              8  ntdll!RtlpNtMakeTemporaryKey                                                                       0x7ffa1b1c53c9 
              9  ntdll!memset                                                                                       0x7ffa1b175670 
              10 ntdll!RtlGetCurrentServiceSessionId                                                                0x7ffa1b100790 
              11 ntdll!RtlFreeHeap                                                                                  0x7ffa1b0ffb91 
              12 msvcrt!free                                                                                        0x7ffa19189cfc 
              13 QWindowsPrintDevice::defaultPageSize() const                                                       0x6f882e41     
              14 QPrintDevice::defaultPageSize() const                                                              0x6df43efd     
              15 QWin32PrintEngine::QWin32PrintEngine(QPrinter::PrinterMode, QString const&)                        0x6df528b2     
              16 QWindowsPrinterSupport::createNativePrintEngine(QPrinter::PrinterMode, QString const&)             0x6f88169a     
              17 QPrinterPrivate::initEngines(QPrinter::OutputFormat, QPrinterInfo const&)                          0x6df4a523     
              18 QPrinterPrivate::init(QPrinterInfo const&, QPrinter::PrinterMode)                                  0x6df4a70e     
              19 QPrinter::QPrinter(QPrinter::PrinterMode)                                                          0x6df4a818     
              20 main                                                                                   main.cpp 13 0x4016cc
              

              Is that what you were asking for?

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

                Yes that's that.

                Looks like something is going wrong under the hood.

                In between, did your try QPdfWriter ?

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

                A 1 Reply Last reply Reply Quote 0
                • A
                  Alex Lyapin @SGaist last edited by

                  @SGaist yes QPdfWriter is another option, but I was thinking about generating html and printing it while QPdfWriter is something like low level solution (I think so).

                  For instance, if I want to print block of text, is it possible to determine in advance the height of this block sufficient to fit all the text? drawText method can update some QRect object, but the text is already printed...
                  is it possible to estimate bounding rectangle sizes without printing the text?

                  Thanks

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

                    Take a look at the C++ GUI Programming with Qt 4 book, there's a chapter dedicated to document creation and printing that should give you a good overview of what is possible. Even if for a Qt 4, there are still relevant stuff for Qt 5 in there.

                    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 0
                    • First post
                      Last post