From 8c18418f260da36a6b0703e1928b2759d36cc2d3 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sat, 12 Jun 2021 10:34:46 +0200 Subject: [PATCH] Break out ConvertWorker::pdfToRaster --- src/convertworker.cpp | 172 +++++++++++++++++++++++------------------- src/convertworker.h | 6 ++ 2 files changed, 99 insertions(+), 79 deletions(-) diff --git a/src/convertworker.cpp b/src/convertworker.cpp index 98f3d4c..737086a 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -603,85 +603,10 @@ try { else { - QProcess* pdftoppm = new QProcess(this); - pdftoppm->setProgram("pdftoppm"); - QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)}; - Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; - - if(Colors == 1) - { - Pdf2PpmArgs.append("-gray"); - } - qDebug() << "pdf2ppm args is " << Pdf2PpmArgs; - pdftoppm->setArguments(Pdf2PpmArgs); - - - QProcess* ppm2pwg = new QProcess(this); - // Yo dawg, I heard you like programs... - ppm2pwg->setProgram("harbour-seaprint"); - ppm2pwg->setArguments({"ppm2pwg"}); - - bool urf = targetFormat == Mimer::URF; - - QStringList env; - ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages); - qDebug() << "ppm2pwg env is " << env; - - ppm2pwg->setEnvironment(env); - - pdftoppm->setStandardInputFile(tmpPdfFile.fileName()); - pdftoppm->setStandardOutputProcess(ppm2pwg); - ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); - - connect(pdftoppm, SIGNAL(finished(int, QProcess::ExitStatus)), pdftoppm, SLOT(deleteLater())); - connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater())); - - qDebug() << "All connected"; - - pdftoppm->start(); - ppm2pwg->start(); - - qDebug() << "Starting"; - - if(!pdftoppm->waitForStarted()) - { - qDebug() << "pdftoppm died"; - throw ConvertFailedException(); - } - if(!ppm2pwg->waitForStarted()) - { - qDebug() << "ppm2pwg died"; - throw ConvertFailedException(); - } - qDebug() << "All started"; - - bool ppm2pwgSuccess = false; - - for(;;) - { - if(ppm2pwg->waitForFinished(250)) - { - ppm2pwgSuccess = true; - break; - } - else - { - QList ppm2pwgOutput = ppm2pwg->readAllStandardError().split('\n'); - for(QList::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++) - { - if(it->startsWith("Page")) - { - QList ppm2pwgTokens = it->split(' '); - emit progress(ppm2pwgTokens.last().toInt()-1, pages); - } - } - } - } - if(!ppm2pwgSuccess) - { - qDebug() << "ppm2pwg failed"; - throw ConvertFailedException(); - } + pdfToRaster(targetFormat, Colors, Quality, PaperSize, + HwResX, HwResY, TwoSided, Tumble, + PageRangeLow, PageRangeHigh, pages, + tmpPdfFile, tempfile); } @@ -697,3 +622,92 @@ catch(const ConvertFailedException& e) emit failed(e.what() == QString("") ? tr("Conversion error") : e.what()); } } + +void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, + quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, + quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages, + QTemporaryFile& tmpPdfFile, QTemporaryFile* tempfile) +{ + QProcess* pdftoppm = new QProcess(this); + pdftoppm->setProgram("pdftoppm"); + QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)}; + if (PageRangeLow != 0 && PageRangeHigh !=0) + { + Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; + } + + if(Colors == 1) + { + Pdf2PpmArgs.append("-gray"); + } + qDebug() << "pdf2ppm args is " << Pdf2PpmArgs; + pdftoppm->setArguments(Pdf2PpmArgs); + + + QProcess* ppm2pwg = new QProcess(this); + // Yo dawg, I heard you like programs... + ppm2pwg->setProgram("harbour-seaprint"); + ppm2pwg->setArguments({"ppm2pwg"}); + + bool urf = targetFormat == Mimer::URF; + + QStringList env; + ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages); + qDebug() << "ppm2pwg env is " << env; + + ppm2pwg->setEnvironment(env); + + pdftoppm->setStandardInputFile(tmpPdfFile.fileName()); + pdftoppm->setStandardOutputProcess(ppm2pwg); + ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); + + connect(pdftoppm, SIGNAL(finished(int, QProcess::ExitStatus)), pdftoppm, SLOT(deleteLater())); + connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater())); + + qDebug() << "All connected"; + + pdftoppm->start(); + ppm2pwg->start(); + + qDebug() << "Starting"; + + if(!pdftoppm->waitForStarted()) + { + qDebug() << "pdftoppm died"; + throw ConvertFailedException(); + } + if(!ppm2pwg->waitForStarted()) + { + qDebug() << "ppm2pwg died"; + throw ConvertFailedException(); + } + qDebug() << "All started"; + + bool ppm2pwgSuccess = false; + + for(;;) + { + if(ppm2pwg->waitForFinished(250)) + { + ppm2pwgSuccess = true; + break; + } + else + { + QList ppm2pwgOutput = ppm2pwg->readAllStandardError().split('\n'); + for(QList::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++) + { + if(it->startsWith("Page")) + { + QList ppm2pwgTokens = it->split(' '); + emit progress(ppm2pwgTokens.last().toInt()-1, pages); + } + } + } + } + if(!ppm2pwgSuccess) + { + qDebug() << "ppm2pwg failed"; + throw ConvertFailedException(); + } +} diff --git a/src/convertworker.h b/src/convertworker.h index 768ac07..eb270cd 100644 --- a/src/convertworker.h +++ b/src/convertworker.h @@ -40,6 +40,12 @@ signals: void done(QNetworkRequest request, QTemporaryFile* data); void progress(qint64 done, qint64 pages); void failed(QString message); + +private: + void pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, + quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, + quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages, + QTemporaryFile& tmpPdfFile, QTemporaryFile* tempfile); }; #endif // CONVERTWORKER_H