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

Qt printing document to PDF crashes



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


  • Lifetime Qt Champion

    Hi,

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



  • @SGaist , thanks for the reply.

    I'm using:

    • Qt 5.14.1
    • MinGW 64 bit
    • Win 10 64 bit

  • Lifetime Qt Champion

    What is the stack trace ?



  • 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?


  • Lifetime Qt Champion

    Yes that's that.

    Looks like something is going wrong under the hood.

    In between, did your try QPdfWriter ?



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


  • Lifetime Qt Champion

    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.


Log in to reply