Break out ConvertWorker::pdftoPs

This commit is contained in:
Anton Thomasson 2021-06-12 11:17:34 +02:00
parent e6d9a80e1c
commit 5125c48654
2 changed files with 86 additions and 118 deletions

View file

@ -104,73 +104,19 @@ try {
throw ConvertFailedException(tr("Unsupported resolution (dpi)")); throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
} }
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"));
}
if(ps) if(ps)
{ {
QProcess* pdftops = new QProcess(this); pdftoPs(PaperSize, TwoSided, PageRangeLow, PageRangeHigh, filename, tempfile);
pdftops->setProgram("pdftops");
QStringList PdfToPsArgs;
if(TwoSided)
{
PdfToPsArgs.append("-duplex");
}
PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PdfToPsArgs << QStringList {"-paper", ShortPaperSize, filename, "-"};
qDebug() << "pdftops args is " << PdfToPsArgs;
pdftops->setArguments(PdfToPsArgs);
pdftops->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(pdftops, SIGNAL(finished(int, QProcess::ExitStatus)), pdftops, SLOT(deleteLater()));
pdftops->start();
qDebug() << "Starting";
if(!pdftops->waitForStarted())
{
qDebug() << "pdftops died";
throw ConvertFailedException();
}
qDebug() << "Started";
if(!pdftops->waitForFinished(-1))
{
qDebug() << "pdftops failed";
throw ConvertFailedException();
}
} }
else if(pdf) else if(pdf)
{ { // adjusting page range
QProcess* pdftocairo = new QProcess(this); QProcess* pdftocairo = new QProcess(this);
pdftocairo->setProgram("pdftocairo"); pdftocairo->setProgram("pdftocairo");
QStringList PdfToCairoArgs = {"-pdf"}; QStringList PdfToCairoArgs = {"-pdf"};
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
@ -476,42 +422,7 @@ try {
} }
else if(targetFormat == Mimer::Postscript) else if(targetFormat == Mimer::Postscript)
{ {
QProcess* PdfToPs = new QProcess(this); pdftoPs(PaperSize, TwoSided, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), tempfile);
PdfToPs->setProgram("pdftops");
QStringList PdfToPsArgs;
if(TwoSided)
{
PdfToPsArgs.append("-duplex");
}
PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PdfToPsArgs << QStringList {tmpPdfFile.fileName(), "-"};
qDebug() << "pdftops args is " << PdfToPsArgs;
PdfToPs->setArguments(PdfToPsArgs);
PdfToPs->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(PdfToPs, SIGNAL(finished(int, QProcess::ExitStatus)), PdfToPs, SLOT(deleteLater()));
PdfToPs->start();
qDebug() << "PdfToPs Starting";
if(!PdfToPs->waitForStarted())
{
qDebug() << "PdfToPs died";
throw ConvertFailedException();
}
qDebug() << "PdfToPs Started";
if(!PdfToPs->waitForFinished(-1))
{
qDebug() << "PdfToPs failed";
throw ConvertFailedException();
}
} }
else else
{ {
@ -522,7 +433,6 @@ try {
tmpPdfFile.fileName(), tempfile, false); tmpPdfFile.fileName(), tempfile, false);
} }
qDebug() << "Finished"; qDebug() << "Finished";
emit done(request, tempfile); emit done(request, tempfile);
@ -536,6 +446,75 @@ catch(const ConvertFailedException& e)
} }
} }
QString ConvertWorker::getPopplerShortPaperSize(QString PaperSize)
{
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"));
}
return ShortPaperSize;
}
void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile)
{
QProcess* pdftops = new QProcess(this);
pdftops->setProgram("pdftops");
QStringList PdfToPsArgs;
if(TwoSided)
{
PdfToPsArgs.append("-duplex");
}
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PdfToPsArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftops args is " << PdfToPsArgs;
pdftops->setArguments(PdfToPsArgs);
pdftops->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(pdftops, SIGNAL(finished(int, QProcess::ExitStatus)), pdftops, SLOT(deleteLater()));
pdftops->start();
qDebug() << "Starting";
if(!pdftops->waitForStarted())
{
qDebug() << "pdftops died";
throw ConvertFailedException();
}
qDebug() << "Started";
if(!pdftops->waitForFinished(-1))
{
qDebug() << "pdftops failed";
throw ConvertFailedException();
}
}
void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages, quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages,
@ -566,28 +545,7 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
if(resize) if(resize)
{ {
QString ShortPaperSize; QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
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)}; PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PageRangeLow = PageRangeHigh = 0; PageRangeLow = PageRangeHigh = 0;
@ -601,7 +559,6 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
} }
else else
{ {
Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
pdftoppm->setStandardInputFile(pdfFileName); pdftoppm->setStandardInputFile(pdfFileName);
@ -640,6 +597,11 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
{ {
pdftocairo->start(); pdftocairo->start();
} }
else
{
delete pdftocairo;
}
pdftoppm->start(); pdftoppm->start();
ppm2pwg->start(); ppm2pwg->start();

View file

@ -42,6 +42,12 @@ signals:
void failed(QString message); void failed(QString message);
private: private:
QString getPopplerShortPaperSize(QString PaperSize);
void pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile);
void pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, void pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages, quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages,