Use pdfToRaster in convertPdf too.

This commit is contained in:
Anton Thomasson 2021-06-12 11:00:25 +02:00
parent 8c18418f26
commit e6d9a80e1c
2 changed files with 70 additions and 96 deletions

View file

@ -203,98 +203,11 @@ try {
}
else
{
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
PageRangeLow, PageRangeHigh, pages,
filename, tempfile, true);
QProcess* pdftocairo = new QProcess(this);
pdftocairo->setProgram("pdftocairo");
QStringList PdfToCairoArgs;
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, filename, "-"};
pdftocairo->setArguments(PdfToCairoArgs);
QProcess* pdftoppm = new QProcess(this);
pdftoppm->setProgram("pdftoppm");
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
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"});
QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages);
qDebug() << "ppm2pwg env is " << env;
ppm2pwg->setEnvironment(env);
pdftocairo->setStandardOutputProcess(pdftoppm);
pdftoppm->setStandardOutputProcess(ppm2pwg);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(pdftocairo, SIGNAL(finished(int, QProcess::ExitStatus)), pdftocairo, SLOT(deleteLater()));
connect(pdftoppm, SIGNAL(finished(int, QProcess::ExitStatus)), pdftoppm, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected";
pdftocairo->start();
pdftoppm->start();
ppm2pwg->start();
qDebug() << "Starting";
if(!pdftocairo->waitForStarted())
{
qDebug() << "pdftocairo died";
throw ConvertFailedException();
}
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<QByteArray> ppm2pwgOutput = ppm2pwg->readAllStandardError().split('\n');
for(QList<QByteArray>::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++)
{
if(it->startsWith("Page"))
{
QList<QByteArray> ppm2pwgTokens = it->split(' ');
emit progress(ppm2pwgTokens.last().toInt()-1, pages);
}
}
}
}
if(!ppm2pwgSuccess)
{
qDebug() << "ppm2pwg failed";
throw ConvertFailedException();
}
}
@ -606,7 +519,7 @@ try {
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
PageRangeLow, PageRangeHigh, pages,
tmpPdfFile, tempfile);
tmpPdfFile.fileName(), tempfile, false);
}
@ -626,14 +539,72 @@ catch(const ConvertFailedException& e)
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)
QString pdfFileName, QTemporaryFile* tempfile, bool resize)
{
if(PageRangeLow==0)
{
PageRangeLow=1;
}
if(PageRangeHigh==0)
{
PageRangeHigh=pages;
}
// Actual number of pages to print
pages = PageRangeHigh-PageRangeLow+1;
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
QProcess* pdftocairo = new QProcess(this);
pdftocairo->setProgram("pdftocairo");
QStringList PdfToCairoArgs;
QProcess* pdftoppm = new QProcess(this);
pdftoppm->setProgram("pdftoppm");
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
if (PageRangeLow != 0 && PageRangeHigh !=0)
if(resize)
{
QString ShortPaperSize;
if(PaperSize == "iso_a4_210x297mm")
{
ShortPaperSize = "A4";
}
else if (PaperSize == "iso_a3_297x420mm")
{
ShortPaperSize = "A3";
}
else if (PaperSize == "na_letter_8.5x11in")
{
ShortPaperSize = "letter";
}
else if (PaperSize == "na_legal_8.5x14in")
{
ShortPaperSize = "legal";
}
else
{
qDebug() << "Unsupported PDF paper size" << PaperSize;
throw ConvertFailedException(tr("Unsupported PDF paper size"));
}
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PageRangeLow = PageRangeHigh = 0;
PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, pdfFileName, "-"};
pdftocairo->setArguments(PdfToCairoArgs);
connect(pdftocairo, SIGNAL(finished(int, QProcess::ExitStatus)), pdftocairo, SLOT(deleteLater()));
pdftocairo->setStandardOutputProcess(pdftoppm);
}
else
{
Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
pdftoppm->setStandardInputFile(pdfFileName);
}
if(Colors == 1)
@ -657,7 +628,6 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
ppm2pwg->setEnvironment(env);
pdftoppm->setStandardInputFile(tmpPdfFile.fileName());
pdftoppm->setStandardOutputProcess(ppm2pwg);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
@ -666,6 +636,10 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
qDebug() << "All connected";
if(resize)
{
pdftocairo->start();
}
pdftoppm->start();
ppm2pwg->start();

View file

@ -45,7 +45,7 @@ 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);
QString pdfFileName, QTemporaryFile* tempfile, bool resize);
};
#endif // CONVERTWORKER_H