2020-05-01 16:02:29 +03:00
|
|
|
#include "convertworker.h"
|
2020-05-01 21:54:33 +03:00
|
|
|
#include <sailfishapp.h>
|
2020-05-16 16:17:42 +03:00
|
|
|
#include "papersizes.h"
|
2020-08-01 21:18:47 +03:00
|
|
|
#include "convertchecker.h"
|
2021-03-04 23:49:27 +03:00
|
|
|
#include "mimer.h"
|
2020-06-01 22:12:29 +03:00
|
|
|
#include <QImage>
|
|
|
|
#include <QMatrix>
|
|
|
|
#include <QPainter>
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
void ppm2PwgEnv(QStringList& env, bool urf, quint32 Quality, QString PaperSize,
|
2020-08-01 18:03:17 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
|
|
|
|
bool ForcePortrait, quint32 pages)
|
2020-05-01 16:02:29 +03:00
|
|
|
{
|
2020-05-09 21:39:50 +03:00
|
|
|
env.append("HWRES_X="+QString::number(HwResX));
|
|
|
|
env.append("HWRES_Y="+QString::number(HwResY));
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-05-09 21:39:50 +03:00
|
|
|
if(urf)
|
|
|
|
{
|
|
|
|
env.append("URF=true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Quality >= 3 && Quality <= 5)
|
|
|
|
{
|
|
|
|
env.append("QUALITY="+QString::number(Quality));
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
if(PaperSize != "")
|
|
|
|
{
|
|
|
|
env.append("PAGE_SIZE_NAME="+PaperSize);
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:39:50 +03:00
|
|
|
env.append("DUPLEX="+QString::number(TwoSided));
|
|
|
|
env.append("TUMBLE="+QString::number(Tumble));
|
2020-08-01 18:03:17 +03:00
|
|
|
|
|
|
|
if(ForcePortrait)
|
|
|
|
{
|
|
|
|
env.append("FORCE_PORTRAIT=true");
|
|
|
|
}
|
|
|
|
|
2020-06-06 15:59:44 +03:00
|
|
|
if(pages != 0)
|
|
|
|
{
|
|
|
|
env.append("PAGES="+QString::number(pages));
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
|
2020-06-04 22:31:46 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2020-08-01 21:18:47 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
|
|
|
|
quint32 PageRangeLow, quint32 PageRangeHigh)
|
2020-05-09 21:39:50 +03:00
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
try {
|
|
|
|
|
2020-08-01 21:18:47 +03:00
|
|
|
quint32 pages = ConvertChecker::instance()->pdfPages(filename);
|
|
|
|
if (!pages)
|
2020-06-06 15:59:44 +03:00
|
|
|
{
|
2020-08-01 21:18:47 +03:00
|
|
|
qDebug() << "pdfinfo returned 0 pages";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Failed to get info about PDF file"));
|
2020-06-06 15:59:44 +03:00
|
|
|
}
|
2020-08-01 21:18:47 +03:00
|
|
|
|
2021-02-18 22:07:40 +03:00
|
|
|
if(PageRangeLow==0)
|
|
|
|
{
|
|
|
|
PageRangeLow=1;
|
|
|
|
}
|
|
|
|
|
2020-08-01 21:18:47 +03:00
|
|
|
if(PageRangeHigh==0)
|
2020-06-06 15:59:44 +03:00
|
|
|
{
|
2020-08-01 21:18:47 +03:00
|
|
|
PageRangeHigh=pages;
|
2020-06-06 15:59:44 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 00:15:04 +03:00
|
|
|
// Actual number of pages to print
|
2021-02-18 22:07:40 +03:00
|
|
|
pages = PageRangeHigh-PageRangeLow+1;
|
|
|
|
|
|
|
|
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
|
2021-02-18 00:15:04 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
bool urf = false;
|
2020-06-06 16:47:43 +03:00
|
|
|
bool ps = false;
|
2020-09-23 21:14:33 +03:00
|
|
|
bool pdf = false;
|
2020-06-04 22:31:46 +03:00
|
|
|
|
2021-03-04 23:49:27 +03:00
|
|
|
if(targetFormat == Mimer::URF)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
|
|
|
urf = true;
|
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if(targetFormat == Mimer::PWG)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
|
|
|
//ok
|
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if(targetFormat == Mimer::Postscript)
|
2020-06-06 16:47:43 +03:00
|
|
|
{
|
|
|
|
ps = true;
|
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if (targetFormat == Mimer::PDF)
|
2020-09-23 21:14:33 +03:00
|
|
|
{
|
|
|
|
pdf = true;
|
|
|
|
}
|
2020-06-04 22:31:46 +03:00
|
|
|
else
|
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported target format"));
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
|
|
|
|
2020-06-01 22:56:47 +03:00
|
|
|
if(urf && (HwResX != HwResY))
|
2020-05-09 21:39:50 +03:00
|
|
|
{ // URF only supports symmetric resolutions
|
2020-06-01 22:56:47 +03:00
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
2020-05-05 21:36:01 +03:00
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
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;
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported PDF paper size"));
|
2020-05-16 16:17:42 +03:00
|
|
|
}
|
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
if(ps)
|
2020-05-09 21:39:50 +03:00
|
|
|
{
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* pdftops = new QProcess(this);
|
|
|
|
pdftops->setProgram("pdftops");
|
2020-08-01 21:18:47 +03:00
|
|
|
QStringList PdfToPsArgs;
|
2020-06-07 16:27:27 +03:00
|
|
|
if(TwoSided)
|
|
|
|
{
|
2020-08-01 21:18:47 +03:00
|
|
|
PdfToPsArgs.append("-duplex");
|
2020-06-07 16:27:27 +03:00
|
|
|
}
|
2021-02-18 22:07:40 +03:00
|
|
|
|
|
|
|
PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
|
|
|
|
|
2020-08-01 21:18:47 +03:00
|
|
|
PdfToPsArgs << QStringList {"-paper", ShortPaperSize, filename, "-"};
|
|
|
|
|
2020-09-23 21:14:33 +03:00
|
|
|
qDebug() << "pdftops args is " << PdfToPsArgs;
|
2020-06-07 16:27:27 +03:00
|
|
|
pdftops->setArguments(PdfToPsArgs);
|
2020-06-06 16:47:43 +03:00
|
|
|
|
|
|
|
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";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Started";
|
|
|
|
|
2020-11-30 22:28:33 +03:00
|
|
|
if(!pdftops->waitForFinished(-1))
|
2020-06-06 16:47:43 +03:00
|
|
|
{
|
|
|
|
qDebug() << "pdftops failed";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
2020-09-23 21:14:33 +03:00
|
|
|
else if(pdf)
|
|
|
|
{
|
|
|
|
QProcess* pdftocairo = new QProcess(this);
|
|
|
|
pdftocairo->setProgram("pdftocairo");
|
|
|
|
QStringList PdfToCairoArgs = {"-pdf"};
|
|
|
|
|
2021-02-18 22:07:40 +03:00
|
|
|
|
|
|
|
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
|
|
|
|
|
2020-09-23 21:14:33 +03:00
|
|
|
PdfToCairoArgs << QStringList {"-paper", ShortPaperSize, filename, "-"};
|
|
|
|
|
|
|
|
qDebug() << "pdftocairo args is " << PdfToCairoArgs;
|
|
|
|
pdftocairo->setArguments(PdfToCairoArgs);
|
|
|
|
|
|
|
|
pdftocairo->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
|
|
|
connect(pdftocairo, SIGNAL(finished(int, QProcess::ExitStatus)), pdftocairo, SLOT(deleteLater()));
|
|
|
|
|
|
|
|
pdftocairo->start();
|
|
|
|
|
|
|
|
qDebug() << "Starting";
|
|
|
|
|
|
|
|
if(!pdftocairo->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftocairo died";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-09-23 21:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Started";
|
|
|
|
|
2020-11-30 22:28:33 +03:00
|
|
|
if(!pdftocairo->waitForFinished(-1))
|
2020-09-23 21:14:33 +03:00
|
|
|
{
|
|
|
|
qDebug() << "pdftocairo failed";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-09-23 21:14:33 +03:00
|
|
|
}
|
2021-03-05 22:28:10 +03:00
|
|
|
|
2020-09-23 21:14:33 +03:00
|
|
|
}
|
2020-06-06 16:47:43 +03:00
|
|
|
else
|
|
|
|
{
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* pdftocairo = new QProcess(this);
|
|
|
|
pdftocairo->setProgram("pdftocairo");
|
2020-08-01 21:18:47 +03:00
|
|
|
QStringList PdfToCairoArgs;
|
2021-02-18 22:07:40 +03:00
|
|
|
|
|
|
|
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
|
|
|
|
|
2020-08-01 21:18:47 +03:00
|
|
|
PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, filename, "-"};
|
|
|
|
pdftocairo->setArguments(PdfToCairoArgs);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* pdftoppm = new QProcess(this);
|
|
|
|
pdftoppm->setProgram("pdftoppm");
|
|
|
|
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
|
2020-11-30 22:46:26 +03:00
|
|
|
if(Colors == 1)
|
2020-06-06 16:47:43 +03:00
|
|
|
{
|
|
|
|
Pdf2PpmArgs.append("-gray");
|
|
|
|
}
|
2020-06-21 16:49:50 +03:00
|
|
|
qDebug() << "pdf2ppm args is " << Pdf2PpmArgs;
|
2020-06-06 16:47:43 +03:00
|
|
|
pdftoppm->setArguments(Pdf2PpmArgs);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* ppm2pwg = new QProcess(this);
|
|
|
|
// Yo dawg, I heard you like programs...
|
|
|
|
ppm2pwg->setProgram("harbour-seaprint");
|
|
|
|
ppm2pwg->setArguments({"ppm2pwg"});
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QStringList env;
|
2020-08-01 18:03:17 +03:00
|
|
|
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages);
|
2020-06-06 16:47:43 +03:00
|
|
|
qDebug() << "ppm2pwg env is " << env;
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
ppm2pwg->setEnvironment(env);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
pdftocairo->setStandardOutputProcess(pdftoppm);
|
|
|
|
pdftoppm->setStandardOutputProcess(ppm2pwg);
|
|
|
|
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
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()));
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
qDebug() << "All connected";
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
pdftocairo->start();
|
|
|
|
pdftoppm->start();
|
|
|
|
ppm2pwg->start();
|
|
|
|
|
|
|
|
qDebug() << "Starting";
|
|
|
|
|
|
|
|
if(!pdftocairo->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftocairo died";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
|
|
|
if(!pdftoppm->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftoppm died";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
|
|
|
if(!ppm2pwg->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "ppm2pwg died";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
|
|
|
qDebug() << "All started";
|
|
|
|
|
2020-07-30 23:21:14 +03:00
|
|
|
bool ppm2pwgSuccess = false;
|
2020-06-06 16:47:43 +03:00
|
|
|
|
2020-11-30 22:28:33 +03:00
|
|
|
for(;;)
|
2020-07-30 23:21:14 +03:00
|
|
|
{
|
|
|
|
if(ppm2pwg->waitForFinished(1000))
|
|
|
|
{
|
|
|
|
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(' ');
|
2020-08-01 18:03:17 +03:00
|
|
|
emit progress(ppm2pwgTokens.last().toInt()-1, pages);
|
2020-07-30 23:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!ppm2pwgSuccess)
|
2020-06-06 16:47:43 +03:00
|
|
|
{
|
|
|
|
qDebug() << "ppm2pwg failed";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-06 16:47:43 +03:00
|
|
|
}
|
2020-05-01 16:02:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qDebug() << "Finished";
|
|
|
|
|
|
|
|
emit done(request, tempfile);
|
|
|
|
qDebug() << "posted";
|
2021-03-05 22:28:10 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
2020-05-01 16:02:29 +03:00
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
|
2020-06-04 22:31:46 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2020-05-16 16:17:42 +03:00
|
|
|
quint32 HwResX, quint32 HwResY)
|
2020-05-13 20:53:44 +03:00
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
try {
|
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
bool urf = false;
|
|
|
|
QString imageFormat = "";
|
2021-03-04 23:49:27 +03:00
|
|
|
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
|
2020-06-04 22:31:46 +03:00
|
|
|
|
2021-03-04 23:49:27 +03:00
|
|
|
if(targetFormat == Mimer::URF)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
|
|
|
urf = true;
|
|
|
|
}
|
2021-03-04 23:49:27 +03:00
|
|
|
else if(targetFormat == Mimer::PWG)
|
2020-06-04 22:31:46 +03:00
|
|
|
{
|
|
|
|
//ok
|
|
|
|
}
|
|
|
|
else if(supportedImageFormats.contains(targetFormat))
|
|
|
|
{
|
|
|
|
imageFormat = targetFormat.split("/")[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported target format"));
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
|
|
|
|
2020-06-01 22:56:47 +03:00
|
|
|
if(urf && (HwResX != HwResY))
|
|
|
|
{ // URF only supports symmetric resolutions
|
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
|
2020-06-01 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
if(!PaperSizes.contains(PaperSize))
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported paper size" << PaperSize;
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Unsupported paper size"));
|
2020-05-16 16:17:42 +03:00
|
|
|
}
|
|
|
|
QPair<float,float> wh = PaperSizes[PaperSize];
|
|
|
|
quint32 Width = qRound(wh.first/25.4*HwResX);
|
|
|
|
quint32 Height = qRound(wh.second/25.4*HwResY);
|
|
|
|
|
|
|
|
qDebug() << "Size is" << Width << "x" << Height;
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
QImage inImage;
|
|
|
|
if(!inImage.load(filename))
|
|
|
|
{
|
|
|
|
qDebug() << "failed to load";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException(tr("Failed to load image"));
|
2020-05-13 20:53:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(inImage.width() > inImage.height())
|
|
|
|
{
|
|
|
|
inImage = inImage.transformed(QMatrix().rotate(90.0));
|
|
|
|
}
|
|
|
|
inImage = inImage.scaled(Width, Height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
QImage outImage = QImage(Width, Height, inImage.format());
|
|
|
|
outImage.fill(Qt::white);
|
|
|
|
QPainter painter(&outImage);
|
2020-08-22 18:37:22 +03:00
|
|
|
int xOffset = (outImage.width()-inImage.width())/2;
|
|
|
|
int yOffset = (outImage.height()-inImage.height())/2;
|
|
|
|
painter.drawImage(xOffset, yOffset, inImage);
|
2020-05-13 20:53:44 +03:00
|
|
|
painter.end();
|
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
if(imageFormat != "")
|
|
|
|
{ // We are converting to a supported image format
|
|
|
|
|
|
|
|
QFile tempfileAsFile(tempfile->fileName());
|
|
|
|
tempfileAsFile.open(QIODevice::Append);
|
2020-06-08 19:24:40 +03:00
|
|
|
outImage.save(&tempfileAsFile, imageFormat.toStdString().c_str());
|
2020-06-04 22:31:46 +03:00
|
|
|
tempfileAsFile.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We are converting to a raster format
|
|
|
|
QProcess* ppm2pwg = new QProcess(this);
|
|
|
|
// Yo dawg, I heard you like programs...
|
|
|
|
ppm2pwg->setProgram("harbour-seaprint");
|
|
|
|
ppm2pwg->setArguments({"ppm2pwg"});
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
QStringList env;
|
2020-08-01 18:03:17 +03:00
|
|
|
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, false, 0);
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "ppm2pwg env is " << env;
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
ppm2pwg->setEnvironment(env);
|
|
|
|
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "All connected";
|
|
|
|
ppm2pwg->start();
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-11-30 22:46:26 +03:00
|
|
|
bool gray = Colors == 0 ? inImage.allGray() : Colors == 1;
|
|
|
|
|
|
|
|
outImage.save(ppm2pwg, gray ? "pgm" : "ppm");
|
2020-11-24 20:15:31 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "Starting";
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
if(!ppm2pwg->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "ppm2pwg died";
|
2021-03-05 22:28:10 +03:00
|
|
|
throw ConvertFailedException();
|
2020-06-04 22:31:46 +03:00
|
|
|
}
|
|
|
|
qDebug() << "All started";
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
ppm2pwg->waitForFinished();
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "Finished";
|
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
emit done(request, tempfile);
|
|
|
|
qDebug() << "posted";
|
2021-03-05 22:28:10 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
}
|
2021-03-06 14:59:47 +03:00
|
|
|
|
|
|
|
void ConvertWorker::convertOfficeDocument(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
|
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
|
|
|
|
quint32 PageRangeLow, quint32 PageRangeHigh)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(targetFormat == Mimer::URF && (HwResX != HwResY))
|
|
|
|
{ // URF only supports symmetric resolutions
|
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ShortPaperSize;
|
|
|
|
if(CalligraPaperSizes.contains(PaperSize))
|
|
|
|
{
|
|
|
|
ShortPaperSize = CalligraPaperSizes[PaperSize];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported PDF paper size" << PaperSize;
|
|
|
|
throw ConvertFailedException(tr("Unsupported PDF paper size"));
|
|
|
|
}
|
|
|
|
|
|
|
|
QProcess* CalligraConverter = new QProcess(this);
|
|
|
|
CalligraConverter->setProgram("calligraconverter");
|
|
|
|
QStringList CalligraConverterArgs = {"--batch", "--mimetype", Mimer::PDF, "--print-orientation", "Portrait", "--print-papersize", ShortPaperSize};
|
|
|
|
|
|
|
|
CalligraConverterArgs << filename;
|
|
|
|
|
|
|
|
QTemporaryFile tmpPdfFile;
|
|
|
|
tmpPdfFile.open();
|
|
|
|
CalligraConverterArgs << tmpPdfFile.fileName();
|
|
|
|
|
|
|
|
qDebug() << "CalligraConverteArgs is" << CalligraConverterArgs;
|
|
|
|
CalligraConverter->setArguments(CalligraConverterArgs);
|
|
|
|
|
|
|
|
connect(CalligraConverter, SIGNAL(finished(int, QProcess::ExitStatus)), CalligraConverter, SLOT(deleteLater()));
|
|
|
|
|
|
|
|
CalligraConverter->start();
|
|
|
|
|
|
|
|
qDebug() << "CalligraConverter Starting";
|
|
|
|
|
|
|
|
if(!CalligraConverter->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "CalligraConverter died";
|
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "CalligraConverter Started";
|
|
|
|
|
|
|
|
if(!CalligraConverter->waitForFinished(-1))
|
|
|
|
{
|
|
|
|
qDebug() << "CalligraConverter failed";
|
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// qDebug() << CalligraConverter->readAllStandardError();
|
|
|
|
|
|
|
|
quint32 pages = ConvertChecker::instance()->pdfPages(tmpPdfFile.fileName());
|
|
|
|
if (!pages)
|
|
|
|
{
|
|
|
|
qDebug() << "pdfinfo returned 0 pages";
|
|
|
|
throw ConvertFailedException(tr("Failed to get info about PDF file"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(PageRangeLow==0)
|
|
|
|
{
|
|
|
|
PageRangeLow=1;
|
|
|
|
}
|
|
|
|
|
2021-03-06 19:20:51 +03:00
|
|
|
if(PageRangeHigh==0 || PageRangeHigh > pages)
|
2021-03-06 14:59:47 +03:00
|
|
|
{
|
|
|
|
PageRangeHigh=pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual number of pages to print
|
|
|
|
pages = PageRangeHigh-PageRangeLow+1;
|
|
|
|
|
|
|
|
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
|
|
|
|
|
|
|
|
if(targetFormat == Mimer::PDF)
|
|
|
|
{
|
|
|
|
|
2021-03-06 19:20:51 +03:00
|
|
|
if(PageRangeLow != 1 || PageRangeHigh != pages)
|
|
|
|
{
|
|
|
|
qDebug() << "adjusting pages in PDF" << PageRangeLow << PageRangeHigh;
|
2021-03-06 14:59:47 +03:00
|
|
|
|
2021-03-06 19:20:51 +03:00
|
|
|
QProcess* PdftoCairo = new QProcess(this);
|
|
|
|
PdftoCairo->setProgram("pdftocairo");
|
|
|
|
QStringList PdfToCairoArgs = {"-pdf"};
|
|
|
|
|
|
|
|
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
|
|
|
|
|
|
|
|
PdfToCairoArgs << QStringList {tmpPdfFile.fileName(), "-"};
|
|
|
|
|
|
|
|
qDebug() << "pdftocairo args is " << PdfToCairoArgs;
|
|
|
|
PdftoCairo->setArguments(PdfToCairoArgs);
|
|
|
|
|
|
|
|
PdftoCairo->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
|
|
|
connect(PdftoCairo, SIGNAL(finished(int, QProcess::ExitStatus)), PdftoCairo, SLOT(deleteLater()));
|
|
|
|
|
|
|
|
PdftoCairo->start();
|
|
|
|
|
|
|
|
qDebug() << "PdftoCairo Starting";
|
|
|
|
|
|
|
|
if(!PdftoCairo->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftocairo died";
|
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "PdftoCairo Started";
|
|
|
|
|
|
|
|
if(!PdftoCairo->waitForFinished(-1))
|
|
|
|
{
|
|
|
|
qDebug() << "pdftocairo failed";
|
|
|
|
throw ConvertFailedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QFile tempfileAsFile(tempfile->fileName());
|
|
|
|
tempfileAsFile.open(QIODevice::Append);
|
|
|
|
tempfileAsFile.write(tmpPdfFile.readAll());
|
|
|
|
tempfileAsFile.close();
|
|
|
|
}
|
2021-03-06 14:59:47 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
else if(targetFormat == Mimer::Postscript)
|
|
|
|
{
|
|
|
|
QProcess* PdfToPs = new QProcess(this);
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
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(1000))
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qDebug() << "Finished";
|
|
|
|
|
|
|
|
emit done(request, tempfile);
|
|
|
|
qDebug() << "posted";
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(const ConvertFailedException& e)
|
|
|
|
{
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
|
|
|
|
}
|
|
|
|
}
|