QWebEnginePage printToPDF not creating a PDF file

25 views Asked by At

I am following this example: How to print from QWebEngineView:

QWebEnginePage page;
QString outputPath="/home/output.pdf";
QString inputPath="/home/input.html";
QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](){page.printToPdf(outputPath); qDebug()<<123;});         
page.load(QUrl::fromLocalFile(inputPath));

The code executed without error, but there is no PDF printed, and no debug message either (the slot never executed?).

What am I missing?

1

There are 1 answers

0
A.R.M On

QWebEnginePage::printToPdf says:

[...]

This method issues an asynchronous request for printing the web page into a PDF and returns immediately. To be informed about the result of the request, connect to the signal pdfPrintingFinished().

QWebEnginePage::pdfPrintingFinished says:

This signal is emitted when printing the web page into a PDF file has finished. filePath will contain the path the file was requested to be created at, and success will be true if the file was successfully created and false otherwise.

#include <QApplication>
#include <QtWebEngineCore/QWebEnginePage>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebEnginePage page;

    QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](bool ok)
    {
        if(!ok)
        {
            qDebug()<<"Loading failed";
            return;
        }
        page.printToPdf("/home/output.pdf");
    });
    QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, [](const QString &filePath, bool success)
    {
        qDebug() << (success ? "PDF printed and saved successfully at: " + filePath : "PDF printing failed");
    });

    page.load(QUrl::fromLocalFile("/home/someUser/Desktop/input.html"));

    return a.exec();
}

This outputs:

PDF printing failed

This means there's no problem with loading the HTML file, since it got to the printing.


Check for writing permission using QFileInfo::isWritable:

#include <QApplication>
#include <QtWebEngineCore/QWebEnginePage>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebEnginePage page;

    QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](bool ok)
    {
        if(!ok)
        {
            qDebug()<<"Loading failed";
            return;
        }
        QString outputPath = "/home/";
        QFileInfo outputDir(outputPath);

        if(outputDir.isDir())
        {
            if(outputDir.isWritable())
                page.printToPdf(outputPath+"output.pdf");
            else
                qDebug() << outputPath << "directory does not have write permission";
        }
        else
        {
            qDebug() << outputPath << "is not a directory";
        }
    });
    QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, [](const QString &filePath, bool success)
    {
        qDebug() << (success ? "PDF printed and saved successfully at: " + filePath : "PDF printing failed");
    });

    page.load(QUrl::fromLocalFile("/home/someUser/Desktop/input.html"));

    return a.exec();
}

Use QDir::homePath() to get the absolute path of the user's home directory:

#include <QApplication>
#include <QtWebEngineCore/QWebEnginePage>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebEnginePage page;

    QObject::connect(&page, &QWebEnginePage::loadFinished, [&page](bool ok)
    {
        if(!ok)
        {
            qDebug()<<"Loading failed";
            return;
        }
        const QFileInfo outputDir(QDir::homePath());
        const QString outputPath = QDir::homePath() + "/";

        if(outputDir.isDir())
        {

            if(outputDir.isWritable())
            {
                page.printToPdf(outputPath+"output.pdf");
                qDebug()<<outputPath+"output.pdf";
            }
            else
                qDebug() << outputPath << "directory does not have write permission";
        }
        else
        {
            qDebug() << outputPath << "is not a directory";
        }
    });
    QObject::connect(&page, &QWebEnginePage::pdfPrintingFinished, [](const QString &filePath, bool success)
    {
        qDebug() << (success ? "PDF printed and saved successfully at: " + filePath : "PDF printing failed");
    });

    page.load(QUrl::fromLocalFile("/home/someUser/Desktop/input.html"));

    return a.exec();
}

This outputs:

PDF printed and saved successfully at: /home/someUser/output.pdf