harbour-seaprint/src/convertworker.cpp

833 lines
23 KiB
C++
Raw Normal View History

#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"
2021-06-19 17:34:17 +03:00
#include "seaprint_version.h"
#include <QImage>
#include <QMatrix>
#include <QPainter>
2021-06-12 13:45:58 +03:00
#include <QTextDocument>
#include <QPdfWriter>
2021-06-12 19:40:18 +03:00
#include <QAbstractTextDocumentLayout>
2020-05-16 16:17:42 +03:00
void ppm2PwgEnv(QStringList& env, bool urf, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
bool ForcePortrait, quint32 pages, bool BackHFlip, bool BackVFlip)
{
env.append("HWRES_X="+QString::number(HwResX));
env.append("HWRES_Y="+QString::number(HwResY));
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);
}
env.append("DUPLEX="+QString::number(TwoSided));
env.append("TUMBLE="+QString::number(Tumble));
if(ForcePortrait)
{
env.append("FORCE_PORTRAIT=true");
}
if(pages != 0)
{
env.append("PAGES="+QString::number(pages));
}
if(BackHFlip)
{
env.append("BACK_HFLIP=true");
}
if(BackVFlip)
{
env.append("BACK_VFLIP=true");
}
}
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, bool BackHFlip, bool BackVFlip)
{
try {
2020-08-01 21:18:47 +03:00
quint32 pages = ConvertChecker::instance()->pdfPages(filename);
if (!pages)
{
2020-08-01 21:18:47 +03:00
qDebug() << "pdfinfo returned 0 pages";
throw ConvertFailedException(tr("Failed to get info about PDF file"));
}
2020-08-01 21:18:47 +03:00
if(PageRangeLow==0)
{
PageRangeLow=1;
}
2020-08-01 21:18:47 +03:00
if(PageRangeHigh==0)
{
2020-08-01 21:18:47 +03:00
PageRangeHigh=pages;
}
// Actual number of pages to print
pages = PageRangeHigh-PageRangeLow+1;
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
2020-06-04 22:31:46 +03:00
bool urf = false;
2020-06-06 16:47:43 +03:00
bool ps = false;
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)
{
pdf = true;
}
2020-06-04 22:31:46 +03:00
else
{
throw ConvertFailedException(tr("Unsupported target format"));
2020-06-04 22:31:46 +03:00
}
if(urf && (HwResX != HwResY))
{ // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution" << PaperSize;
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
}
2021-06-12 12:17:34 +03:00
2020-05-16 16:17:42 +03:00
2020-06-06 16:47:43 +03:00
if(ps)
{
2021-06-12 12:17:34 +03:00
pdftoPs(PaperSize, TwoSided, PageRangeLow, PageRangeHigh, filename, tempfile);
}
else if(pdf)
{
adjustPageRange(PaperSize, PageRangeLow, PageRangeHigh, filename, tempfile);
}
2020-06-06 16:47:43 +03:00
else
{
2021-06-12 12:00:25 +03:00
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
PageRangeLow, PageRangeHigh, pages, BackHFlip, BackVFlip,
2021-06-12 12:00:25 +03:00
filename, tempfile, true);
}
qDebug() << "Finished";
emit done(request, tempfile);
qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
tempfile->deleteLater();
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}
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
{
try {
2020-06-04 22:31:46 +03:00
bool urf = false;
2021-06-13 14:56:44 +03:00
bool pdfOrPostscript = false;
2020-06-04 22:31:46 +03:00
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
}
2021-06-13 14:56:44 +03:00
else if(targetFormat == Mimer::PDF || targetFormat == Mimer::Postscript)
{
HwResX = HwResY = std::min(HwResX, HwResY);
pdfOrPostscript = true;
}
2020-06-04 22:31:46 +03:00
else if(supportedImageFormats.contains(targetFormat))
{
imageFormat = targetFormat.split("/")[1];
}
else
{
throw ConvertFailedException(tr("Unsupported target format"));
2020-06-04 22:31:46 +03:00
}
if(urf && (HwResX != HwResY))
{ // URF only supports symmetric resolutions
qDebug() << "Unsupported URF resolution" << PaperSize;
throw ConvertFailedException(tr("Unsupported resolution (dpi)"));
}
2020-05-16 16:17:42 +03:00
if(!PaperSizes.contains(PaperSize))
{
qDebug() << "Unsupported paper size" << PaperSize;
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";
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);
2021-06-13 14:56:44 +03:00
if(pdfOrPostscript)
{
QTemporaryFile tmpPdfFile;
tmpPdfFile.open();
QPdfWriter pdfWriter(tmpPdfFile.fileName());
QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter);
pdfWriter.setPageSize(pageSize);
pdfWriter.setResolution(HwResX);
// Needs to be before painter
pdfWriter.setMargins({0, 0, 0, 0});
QPainter painter(&pdfWriter);
int xOffset = (pdfWriter.width()-inImage.width())/2;
int yOffset = (pdfWriter.height()-inImage.height())/2;
painter.drawImage(xOffset, yOffset, inImage);
painter.end();
if(targetFormat == Mimer::PDF)
{
QFile tempfileAsFile(tempfile->fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpPdfFile.readAll());
tempfileAsFile.close();
}
else if(targetFormat == Mimer::Postscript)
{
pdftoPs(PaperSize, false, 0, 0, tmpPdfFile.fileName(), tempfile);
}
2020-06-04 22:31:46 +03:00
}
else
2021-06-13 14:56:44 +03:00
{
QImage outImage = QImage(Width, Height, inImage.format());
outImage.fill(Qt::white);
QPainter painter(&outImage);
int xOffset = (outImage.width()-inImage.width())/2;
int yOffset = (outImage.height()-inImage.height())/2;
painter.drawImage(xOffset, yOffset, inImage);
painter.end();
if(imageFormat != "")
{ // We are converting to a supported image format
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
QFile tempfileAsFile(tempfile->fileName());
tempfileAsFile.open(QIODevice::Append);
outImage.save(&tempfileAsFile, imageFormat.toStdString().c_str());
tempfileAsFile.close();
}
else
{ // We are converting to a raster format
QProcess ppm2pwg(this);
// Yo dawg, I heard you like programs...
ppm2pwg.setProgram("harbour-seaprint");
ppm2pwg.setArguments({"ppm2pwg"});
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, false, 0, false, false);
2021-06-13 14:56:44 +03:00
qDebug() << "ppm2pwg env is " << env;
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
ppm2pwg.setEnvironment(env);
ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
qDebug() << "All connected";
ppm2pwg.start();
2020-11-30 22:46:26 +03:00
2021-06-13 14:56:44 +03:00
bool gray = Colors == 0 ? inImage.allGray() : Colors == 1;
2020-11-24 20:15:31 +03:00
2021-06-13 14:56:44 +03:00
outImage.save(&ppm2pwg, gray ? "pgm" : "ppm");
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
qDebug() << "Starting";
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
if(!ppm2pwg.waitForStarted())
{
qDebug() << "ppm2pwg died";
throw ConvertFailedException();
}
qDebug() << "All started";
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
ppm2pwg.waitForFinished();
qDebug() << "Finished";
}
2020-06-04 22:31:46 +03:00
}
2020-05-13 20:53:44 +03:00
2021-06-13 14:56:44 +03:00
2020-05-13 20:53:44 +03:00
emit done(request, tempfile);
qDebug() << "posted";
}
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, bool BackHFlip, bool BackVFlip)
2021-03-06 14:59:47 +03:00
{
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"));
}
2021-06-12 12:57:39 +03:00
QProcess CalligraConverter(this);
CalligraConverter.setProgram("calligraconverter");
2021-03-06 14:59:47 +03:00
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;
2021-06-12 12:57:39 +03:00
CalligraConverter.setArguments(CalligraConverterArgs);
2021-03-06 14:59:47 +03:00
2021-06-12 12:57:39 +03:00
CalligraConverter.start();
2021-03-06 14:59:47 +03:00
qDebug() << "CalligraConverter Starting";
2021-06-12 12:57:39 +03:00
if(!CalligraConverter.waitForStarted())
2021-03-06 14:59:47 +03:00
{
qDebug() << "CalligraConverter died";
throw ConvertFailedException();
}
qDebug() << "CalligraConverter Started";
2021-06-12 12:57:39 +03:00
if(!CalligraConverter.waitForFinished(-1))
2021-03-06 14:59:47 +03:00
{
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
adjustPageRange(PaperSize, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), tempfile);
2021-03-06 19:20:51 +03:00
}
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)
{
2021-06-12 12:17:34 +03:00
pdftoPs(PaperSize, TwoSided, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), tempfile);
2021-03-06 14:59:47 +03:00
}
else
{
2021-06-12 11:34:46 +03:00
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
PageRangeLow, PageRangeHigh, pages, BackHFlip, BackVFlip,
2021-06-12 12:00:25 +03:00
tmpPdfFile.fileName(), tempfile, false);
2021-06-12 11:34:46 +03:00
}
qDebug() << "Finished";
emit done(request, tempfile);
qDebug() << "posted";
}
catch(const ConvertFailedException& e)
{
tempfile->deleteLater();
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}
2021-06-12 13:45:58 +03:00
void ConvertWorker::convertPlaintext(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
bool BackHFlip, bool BackVFlip)
2021-06-12 13:45:58 +03:00
{
try {
if(!PaperSizes.contains(PaperSize))
{
qDebug() << "Unsupported paper size" << PaperSize;
throw ConvertFailedException(tr("Unsupported paper size"));
}
QPair<float,float> wh = PaperSizes[PaperSize];
QFile inFile(filename);
if(!inFile.open(QIODevice::ReadOnly))
{
throw ConvertFailedException(tr("Failed to open file"));
}
quint32 resolution = std::min(HwResX, HwResY);
QTemporaryFile tmpPdfFile;
tmpPdfFile.open();
QPdfWriter pdfWriter(tmpPdfFile.fileName());
2021-06-19 17:34:17 +03:00
pdfWriter.setCreator("SeaPrint " SEAPRINT_VERSION);
2021-06-12 13:45:58 +03:00
QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter);
pdfWriter.setPageSize(pageSize);
pdfWriter.setResolution(resolution);
2021-06-13 14:56:44 +03:00
// Needs to be before painter
pdfWriter.setMargins({0, 0, 0, 0});
2021-06-12 13:45:58 +03:00
qreal docHeight = pageSize.sizePixels(resolution).height();
QTextDocument doc;
QFont font = QFont("Courier");
font.setPointSizeF(1);
qreal charHeight = 0;
// Find the optimal font size
while(true) {
QFont tmpFont = font;
tmpFont.setPointSizeF(font.pointSizeF()+0.5);
QFontMetricsF qfm(tmpFont, &pdfWriter);
charHeight = qfm.lineSpacing();
if(charHeight*66 > docHeight)
{
break;
}
font=tmpFont;
}
QFontMetricsF qfm(font, &pdfWriter);
charHeight = qfm.height();
int textHeight = 60*charHeight;
qreal margin = ((docHeight-textHeight)/2);
doc.setDefaultFont(font);
(void)doc.documentLayout(); // wat
QPainter painter(&pdfWriter);
doc.documentLayout()->setPaintDevice(painter.device());
doc.setDocumentMargin(margin);
QRectF body = QRectF(0, 0, pdfWriter.width(), pdfWriter.height());
doc.setPageSize(body.size());
QString allText = inFile.readAll();
if(allText.startsWith("\f"))
{
allText.remove(0, 1);
}
if(allText.endsWith("\f"))
{
allText.chop(1);
}
else if(allText.endsWith("\f\n"))
{
allText.chop(2);
}
QStringList pages = allText.split('\f');
bool first = true;
int pageCount = 0;
foreach(QString page, pages)
{
if(!first)
{
pdfWriter.newPage();
}
first = false;
if(page.endsWith("\n"))
{
page.chop(1);
}
doc.setPlainText(page);
int p = 0; // Page number in this document, starting from 0
while(true)
{
painter.translate(body.left(), body.top() - p*body.height());
QRectF view(0, p*body.height(), body.width(), body.height());
painter.setClipRect(view);
QAbstractTextDocumentLayout::PaintContext context;
context.clip = view;
context.palette.setColor(QPalette::Text, Qt::black);
doc.documentLayout()->draw(&painter, context);
p++;
pageCount++;
if(p >= doc.pageCount())
break;
pdfWriter.newPage();
}
}
painter.end();
if(targetFormat == Mimer::PDF)
{
QFile tempfileAsFile(tempfile->fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpPdfFile.readAll());
tempfileAsFile.close();
}
else if(targetFormat == Mimer::Postscript)
{
pdftoPs(PaperSize, TwoSided, 0, 0, tmpPdfFile.fileName(), tempfile);
}
else
{
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
0, 0, pageCount, BackHFlip, BackVFlip,
2021-06-12 13:45:58 +03:00
tmpPdfFile.fileName(), tempfile, false);
}
qDebug() << "Finished";
emit done(request, tempfile);
}
catch(const ConvertFailedException& e)
{
tempfile->deleteLater();
emit failed(e.what() == QString("") ? tr("Conversion error") : e.what());
}
}
2021-06-12 12:17:34 +03:00
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::adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile)
{
2021-06-12 12:57:39 +03:00
QProcess pdftocairo(this);
pdftocairo.setProgram("pdftocairo");
QStringList PdfToCairoArgs = {"-pdf"};
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PdfToCairoArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftocairo args is " << PdfToCairoArgs;
2021-06-12 12:57:39 +03:00
pdftocairo.setArguments(PdfToCairoArgs);
2021-06-12 12:57:39 +03:00
pdftocairo.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
2021-06-12 12:57:39 +03:00
pdftocairo.start();
qDebug() << "Starting";
2021-06-12 12:57:39 +03:00
if(!pdftocairo.waitForStarted())
{
qDebug() << "pdftocairo died";
throw ConvertFailedException();
}
qDebug() << "Started";
2021-06-12 12:57:39 +03:00
if(!pdftocairo.waitForFinished(-1))
{
qDebug() << "pdftocairo failed";
throw ConvertFailedException();
}
}
2021-06-12 12:17:34 +03:00
void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile)
{
2021-06-12 12:57:39 +03:00
QProcess pdftops(this);
pdftops.setProgram("pdftops");
2021-06-12 12:17:34 +03:00
QStringList PdfToPsArgs;
if(TwoSided)
{
PdfToPsArgs.append("-duplex");
}
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
2021-06-12 13:45:58 +03:00
if(PageRangeLow != 0 && PageRangeHigh != 0)
{
PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
}
2021-06-12 12:17:34 +03:00
PdfToPsArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftops args is " << PdfToPsArgs;
2021-06-12 12:57:39 +03:00
pdftops.setArguments(PdfToPsArgs);
2021-06-12 12:17:34 +03:00
2021-06-12 12:57:39 +03:00
pdftops.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
2021-06-12 12:17:34 +03:00
2021-06-12 12:57:39 +03:00
pdftops.start();
2021-06-12 12:17:34 +03:00
qDebug() << "Starting";
2021-06-12 12:57:39 +03:00
if(!pdftops.waitForStarted())
2021-06-12 12:17:34 +03:00
{
qDebug() << "pdftops died";
throw ConvertFailedException();
}
qDebug() << "Started";
2021-06-12 12:57:39 +03:00
if(!pdftops.waitForFinished(-1))
2021-06-12 12:17:34 +03:00
{
qDebug() << "pdftops failed";
throw ConvertFailedException();
}
}
2021-06-12 11:34:46 +03:00
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,
bool BackHFlip, bool BackVFlip,
2021-06-12 12:00:25 +03:00
QString pdfFileName, QTemporaryFile* tempfile, bool resize)
2021-06-12 11:34:46 +03:00
{
2021-06-12 12:00:25 +03:00
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;
2021-06-12 12:57:39 +03:00
QProcess pdftocairo(this);
pdftocairo.setProgram("pdftocairo");
2021-06-12 12:00:25 +03:00
QStringList PdfToCairoArgs;
2021-06-12 12:57:39 +03:00
QProcess pdftoppm(this);
pdftoppm.setProgram("pdftoppm");
2021-06-12 11:34:46 +03:00
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
2021-06-12 12:00:25 +03:00
if(resize)
2021-06-12 11:34:46 +03:00
{
2021-06-12 12:17:34 +03:00
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
2021-06-12 12:00:25 +03:00
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PageRangeLow = PageRangeHigh = 0;
PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, pdfFileName, "-"};
2021-06-12 12:57:39 +03:00
pdftocairo.setArguments(PdfToCairoArgs);
2021-06-12 12:00:25 +03:00
2021-06-12 12:57:39 +03:00
pdftocairo.setStandardOutputProcess(&pdftoppm);
2021-06-12 12:00:25 +03:00
}
else
{
2021-03-06 14:59:47 +03:00
Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
2021-06-12 12:57:39 +03:00
pdftoppm.setStandardInputFile(pdfFileName);
2021-06-12 12:00:25 +03:00
2021-06-12 11:34:46 +03:00
}
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
if(Colors == 1)
{
Pdf2PpmArgs.append("-gray");
}
qDebug() << "pdf2ppm args is " << Pdf2PpmArgs;
2021-06-12 12:57:39 +03:00
pdftoppm.setArguments(Pdf2PpmArgs);
2021-03-06 14:59:47 +03:00
2021-06-12 12:57:39 +03:00
QProcess ppm2pwg(this);
2021-06-12 11:34:46 +03:00
// Yo dawg, I heard you like programs...
2021-06-12 12:57:39 +03:00
ppm2pwg.setProgram("harbour-seaprint");
ppm2pwg.setArguments({"ppm2pwg"});
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
bool urf = targetFormat == Mimer::URF;
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages, BackHFlip, BackVFlip);
2021-06-12 11:34:46 +03:00
qDebug() << "ppm2pwg env is " << env;
2021-03-06 14:59:47 +03:00
2021-06-12 12:57:39 +03:00
ppm2pwg.setEnvironment(env);
2021-03-06 14:59:47 +03:00
2021-06-12 12:57:39 +03:00
pdftoppm.setStandardOutputProcess(&ppm2pwg);
ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
qDebug() << "All connected";
2021-03-06 14:59:47 +03:00
2021-06-12 12:00:25 +03:00
if(resize)
{
2021-06-12 12:57:39 +03:00
pdftocairo.start();
2021-06-12 12:17:34 +03:00
}
2021-06-12 12:57:39 +03:00
pdftoppm.start();
ppm2pwg.start();
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
qDebug() << "Starting";
2021-03-06 14:59:47 +03:00
2021-06-12 12:57:39 +03:00
if(!pdftoppm.waitForStarted())
2021-06-12 11:34:46 +03:00
{
qDebug() << "pdftoppm died";
throw ConvertFailedException();
}
2021-06-12 12:57:39 +03:00
if(!ppm2pwg.waitForStarted())
2021-06-12 11:34:46 +03:00
{
qDebug() << "ppm2pwg died";
throw ConvertFailedException();
}
qDebug() << "All started";
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
bool ppm2pwgSuccess = false;
2021-03-06 14:59:47 +03:00
2021-06-12 11:34:46 +03:00
for(;;)
{
2021-06-12 12:57:39 +03:00
if(ppm2pwg.waitForFinished(250))
2021-03-06 14:59:47 +03:00
{
2021-06-12 11:34:46 +03:00
ppm2pwgSuccess = true;
break;
}
else
{
2021-06-12 12:57:39 +03:00
QList<QByteArray> ppm2pwgOutput = ppm2pwg.readAllStandardError().split('\n');
2021-06-12 11:34:46 +03:00
for(QList<QByteArray>::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++)
2021-03-06 14:59:47 +03:00
{
2021-06-12 11:34:46 +03:00
if(it->startsWith("Page"))
2021-03-06 14:59:47 +03:00
{
2021-06-12 11:34:46 +03:00
QList<QByteArray> ppm2pwgTokens = it->split(' ');
emit progress(ppm2pwgTokens.last().toInt()-1, pages);
2021-03-06 14:59:47 +03:00
}
}
}
}
2021-06-12 11:34:46 +03:00
if(!ppm2pwgSuccess)
{
qDebug() << "ppm2pwg failed";
throw ConvertFailedException();
}
2021-03-06 14:59:47 +03:00
}