Create and write into a PDF File



  • I know this topic has been discussed a million times, but i dont understand why it isnt working for me.
    I want to create and write into a PDF file, when i click on a button.

    void MainWindow::on_button_clicked()
    {
    QPrinter printer2;
    printer2.setOutputFormat(QPrinter::PdfFormat);
    printer2.setOutputFileName("test.pdf");
    QPainter painter2;
    painter2.drawText(10,10, "Test");
    painter2.end();
    }

    This is just one of many different approaches i tried.
    The problem is, QT doesnt create any file.

    Thanks in advance!


  • Moderators

    Hi,

    do you have

    QT += printsupport
    

    in your pro file?


  • Moderators

    Hi! Here's a working example.



  • @Eddy Yes.

    @Wieland

    Thanks for your answers.
    I am not sure about the arguments in painter.drawText().
    When i run the programm and press the button, QT says "QPainter::begin(): Returned false
    Could you help me with that?

    void MainWindow::writePdf()
    {
    const QString filename("D://Programme/QT/MeineProjekte/Projekt/test.pdf");

    QString testData = "test";
    QPdfWriter pdfwriter(filename);
    pdfwriter.setPageSize(QPageSize(QPageSize::A4));
    QPainter painter(&pdfwriter);
    painter.drawText(0,0, testData);
    

    }



  • Hi, it seems to be 2 slashes after D:, maybe try
    ``const QString filename("`D:/Programme/QT/MeineProjekte/Projekt/test.pdf");```



  • This post is deleted!


  • @hskoglund

    Thanks a lot. It works now.


Log in to reply