Add image to PDF/Postscript conversion

This commit is contained in:
Anton Thomasson 2021-06-13 13:56:44 +02:00
parent 0f7b776899
commit 79653dc244
3 changed files with 86 additions and 45 deletions

View file

@ -38,7 +38,8 @@ function supported_formats(printer, ConvertChecker, considerAdditionalFormats)
mimetypes = mimetypes.concat(Mimer.OfficeFormats); mimetypes = mimetypes.concat(Mimer.OfficeFormats);
} }
if (raster || has(formats, Mimer.JPEG) || has(formats, Mimer.PNG)) if (raster || has(formats, Mimer.JPEG) || has(formats, Mimer.PNG) ||
has(formats, Mimer.PDF) || (ConvertChecker.pdf && has(formats, Mimer.Postscript)))
{ {
images = true; images = true;
mimetypes.push(Mimer.JPEG); mimetypes.push(Mimer.JPEG);
@ -293,7 +294,7 @@ function canConvertOfficeDocumentTo(type)
function canConvertImageTo(type) function canConvertImageTo(type)
{ {
var targets = [Mimer.OctetStream, Mimer.JPEG, Mimer.PNG, Mimer.PWG, Mimer.URF]; var targets = [Mimer.OctetStream, Mimer.JPEG, Mimer.PNG, Mimer.PWG, Mimer.URF, Mimer.PDF, Mimer.Postscript];
return has(targets, type) return has(targets, type)
} }

View file

@ -147,6 +147,7 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
try { try {
bool urf = false; bool urf = false;
bool pdfOrPostscript = false;
QString imageFormat = ""; QString imageFormat = "";
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG}; QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
@ -158,6 +159,11 @@ try {
{ {
//ok //ok
} }
else if(targetFormat == Mimer::PDF || targetFormat == Mimer::Postscript)
{
HwResX = HwResY = std::min(HwResX, HwResY);
pdfOrPostscript = true;
}
else if(supportedImageFormats.contains(targetFormat)) else if(supportedImageFormats.contains(targetFormat))
{ {
imageFormat = targetFormat.split("/")[1]; imageFormat = targetFormat.split("/")[1];
@ -196,57 +202,91 @@ try {
inImage = inImage.transformed(QMatrix().rotate(90.0)); inImage = inImage.transformed(QMatrix().rotate(90.0));
} }
inImage = inImage.scaled(Width, Height, Qt::KeepAspectRatio, Qt::SmoothTransformation); inImage = inImage.scaled(Width, Height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
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 != "") if(pdfOrPostscript)
{ // We are converting to a supported image format {
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);
}
QFile tempfileAsFile(tempfile->fileName());
tempfileAsFile.open(QIODevice::Append);
outImage.save(&tempfileAsFile, imageFormat.toStdString().c_str());
tempfileAsFile.close();
} }
else else
{ // We are converting to a raster format {
QProcess ppm2pwg(this); QImage outImage = QImage(Width, Height, inImage.format());
// Yo dawg, I heard you like programs... outImage.fill(Qt::white);
ppm2pwg.setProgram("harbour-seaprint"); QPainter painter(&outImage);
ppm2pwg.setArguments({"ppm2pwg"}); int xOffset = (outImage.width()-inImage.width())/2;
int yOffset = (outImage.height()-inImage.height())/2;
painter.drawImage(xOffset, yOffset, inImage);
painter.end();
QStringList env; if(imageFormat != "")
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, false, 0); { // We are converting to a supported image format
qDebug() << "ppm2pwg env is " << env;
ppm2pwg.setEnvironment(env); QFile tempfileAsFile(tempfile->fileName());
ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::Append); tempfileAsFile.open(QIODevice::Append);
outImage.save(&tempfileAsFile, imageFormat.toStdString().c_str());
qDebug() << "All connected"; tempfileAsFile.close();
ppm2pwg.start();
bool gray = Colors == 0 ? inImage.allGray() : Colors == 1;
outImage.save(&ppm2pwg, gray ? "pgm" : "ppm");
qDebug() << "Starting";
if(!ppm2pwg.waitForStarted())
{
qDebug() << "ppm2pwg died";
throw ConvertFailedException();
} }
qDebug() << "All started"; 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"});
ppm2pwg.waitForFinished(); QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, false, 0);
qDebug() << "ppm2pwg env is " << env;
qDebug() << "Finished"; ppm2pwg.setEnvironment(env);
ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
qDebug() << "All connected";
ppm2pwg.start();
bool gray = Colors == 0 ? inImage.allGray() : Colors == 1;
outImage.save(&ppm2pwg, gray ? "pgm" : "ppm");
qDebug() << "Starting";
if(!ppm2pwg.waitForStarted())
{
qDebug() << "ppm2pwg died";
throw ConvertFailedException();
}
qDebug() << "All started";
ppm2pwg.waitForFinished();
qDebug() << "Finished";
}
} }
emit done(request, tempfile); emit done(request, tempfile);
qDebug() << "posted"; qDebug() << "posted";
@ -410,6 +450,8 @@ try {
QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter); QPageSize pageSize(QSizeF {wh.first, wh.second}, QPageSize::Millimeter);
pdfWriter.setPageSize(pageSize); pdfWriter.setPageSize(pageSize);
pdfWriter.setResolution(resolution); pdfWriter.setResolution(resolution);
// Needs to be before painter
pdfWriter.setMargins({0, 0, 0, 0});
qreal docHeight = pageSize.sizePixels(resolution).height(); qreal docHeight = pageSize.sizePixels(resolution).height();
@ -445,8 +487,6 @@ try {
doc.setDefaultFont(font); doc.setDefaultFont(font);
(void)doc.documentLayout(); // wat (void)doc.documentLayout(); // wat
// Needs to be before painter
pdfWriter.setMargins({0, 0, 0, 0});
QPainter painter(&pdfWriter); QPainter painter(&pdfWriter);

View file

@ -325,7 +325,7 @@ QString targetFormatIfAuto(QString documentFormat, QString mimeType, QJsonArray
} }
else if(Mimer::isImage(mimeType)) else if(Mimer::isImage(mimeType))
{ {
QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::JPEG}; QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::PDF, Mimer::Postscript, Mimer::JPEG};
if(mimeType == Mimer::JPEG) if(mimeType == Mimer::JPEG)
{ {
// Prioritize transferring JPEG as JPEG, as it will not be transcoded // Prioritize transferring JPEG as JPEG, as it will not be transcoded