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,6 +202,38 @@ 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);
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);
}
}
else
{
QImage outImage = QImage(Width, Height, inImage.format()); QImage outImage = QImage(Width, Height, inImage.format());
outImage.fill(Qt::white); outImage.fill(Qt::white);
QPainter painter(&outImage); QPainter painter(&outImage);
@ -246,6 +284,8 @@ try {
qDebug() << "Finished"; 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