test-render-to-file.cpp 1.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QGuiApplication>
#include <QImage>

#include <poppler-qt5.h>

int main( int argc, char **argv )
{
    QGuiApplication a( argc, argv );               // QApplication required!

    if ( argc < 2 ||
        (argc == 3 && strcmp(argv[2], "-arthur") != 0) ||
        argc > 3)
    {
        // use argument as file name
Oliver Sander's avatar
Oliver Sander committed
17
        qWarning() << "usage: test-render-to-file-qt5 filename [-arthur]";
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        exit(1);
    }
  
    Poppler::Document *doc = Poppler::Document::load(QFile::decodeName(argv[1]));
    if (!doc)
    {
        qWarning() << "doc not loaded";
        exit(1);
    }

    if (doc->isLocked())
    {
        qWarning() << "document locked (needs password)";
        exit(0);
    }
  
    if (doc->numPages() <= 0)
    {
        delete doc;
        qDebug() << "Doc has no pages";
        return 0;
    }

    QString backendString;
    if (argc == 3 && strcmp(argv[2], "-arthur") == 0)
    {
44
        backendString = QStringLiteral("Arthur");
45 46 47 48
        doc->setRenderBackend(Poppler::Document::ArthurBackend);
    }
    else
    {
49
        backendString = QStringLiteral("Splash");
50 51 52 53 54 55 56 57 58 59 60 61 62
        doc->setRenderBackend(Poppler::Document::SplashBackend);
    }
    doc->setRenderHint(Poppler::Document::Antialiasing, true);
    doc->setRenderHint(Poppler::Document::TextAntialiasing, true);
    
    for (int i = 0; i < doc->numPages(); ++i)
    {
        Poppler::Page *page = doc->page(i);
        if (page) {
            qDebug() << "Rendering page using" << backendString << "backend: " << i;
            QTime t = QTime::currentTime();
            QImage image = page->renderToImage();
            qDebug() << "Rendering took" << t.msecsTo(QTime::currentTime()) << "msecs";
63
            image.save(QStringLiteral("test-render-to-file%1.png").arg(i));
64 65 66 67 68 69
            delete page;
        }
    }
    
    return 0;
}