From f6d6b51b9f6048e02fef67f7fcea84358b78bc7b Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Wed, 23 Sep 2020 20:14:33 +0200 Subject: [PATCH] Allow the page range setting in more cases =if the attribute is supported or pdf processing tools are installed --- qml/pages/PrinterPage.qml | 2 +- src/convertworker.cpp | 46 +++++++++++++++++++++++++++++++++++++++ src/ippprinter.cpp | 36 ++++++++++++++++++++---------- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/qml/pages/PrinterPage.qml b/qml/pages/PrinterPage.qml index f1ad6de..7529f7f 100644 --- a/qml/pages/PrinterPage.qml +++ b/qml/pages/PrinterPage.qml @@ -115,7 +115,7 @@ Page { }) break case IppMsg.IntegerRange: - var valid = printer.attrs.hasOwnProperty(name+"-supported") && + var valid = (printer.attrs.hasOwnProperty(name+"-supported") || ConvertChecker.pdf)&& name=="page-ranges" && Mimer.get_type(selectedFile) == "application/pdf"; var pdfpages = ConvertChecker.pdfPages(selectedFile); loader.setSource("../components/RangeSetting.qml", diff --git a/src/convertworker.cpp b/src/convertworker.cpp index 7365d3a..6d75b73 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -63,6 +63,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo bool urf = false; bool ps = false; + bool pdf = false; if(targetFormat == "image/urf") { @@ -76,6 +77,10 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo { ps = true; } + else if (targetFormat == "application/pdf") + { + pdf = true; + } else { emit failed(tr("Unsupported target format")); @@ -130,6 +135,7 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo } PdfToPsArgs << QStringList {"-paper", ShortPaperSize, filename, "-"}; + qDebug() << "pdftops args is " << PdfToPsArgs; pdftops->setArguments(PdfToPsArgs); pdftops->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); @@ -157,6 +163,46 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo return; } } + else if(pdf) + { + QProcess* pdftocairo = new QProcess(this); + pdftocairo->setProgram("pdftocairo"); + QStringList PdfToCairoArgs = {"-pdf"}; + + if(PageRangeLow != 0) + { + PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; + } + 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"; + tempfile->deleteLater(); + emit failed(tr("Conversion error")); + return; + } + + qDebug() << "Started"; + + if(!pdftocairo->waitForFinished(10000+5000*pages)) + { + qDebug() << "pdftocairo failed"; + tempfile->deleteLater(); + emit failed(tr("Conversion error")); + return; + } + } else { diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 12ccef2..a18460c 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -422,6 +422,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, QString PrintColorMode = getAttrOrDefault(attrs, "print-color-mode").toString(); quint32 Colors = PrintColorMode.contains("color") ? 3 : PrintColorMode.contains("monochrome") ? 1 : 0; + bool pdfPageRangeAdjustNeeded = false; if(!PaperSizes.contains(PaperSize)) { @@ -429,26 +430,34 @@ void IppPrinter::print(QJsonObject attrs, QString filename, return; } + quint32 PageRangeLow = 0; + quint32 PageRangeHigh = 0; + if(attrs.contains("page-ranges")) + { + QJsonObject PageRanges = getAttrOrDefault(attrs, "page-ranges").toObject(); + PageRangeLow = PageRanges["low"].toInt(); + PageRangeHigh = PageRanges["high"].toInt(); + } + QString Sides = getAttrOrDefault(attrs, "sides").toString(); if(removeRedundantConvertAttrs && (documentFormat=="image/pwg-raster" || - documentFormat=="image/urf")) + documentFormat=="image/urf")) { attrs.remove("sides"); attrs.remove("print-color-mode"); + attrs.remove("page-ranges"); } - if(removeRedundantConvertAttrs && documentFormat == "application/postscript") + else if(removeRedundantConvertAttrs && documentFormat == "application/postscript") { attrs.remove("sides"); + attrs.remove("page-ranges"); } - quint32 PageRangeLow = 0; - quint32 PageRangeHigh = 0; - if(mimeType == "application/pdf" && documentFormat != "application/pdf") + else if (documentFormat == "application/pdf") { - if(attrs.contains("page-ranges")) + if(attrs.contains("page-ranges") && !_attrs.contains("page-ranges-supported")) { - QJsonObject PageRanges = getAttrOrDefault(attrs, "page-ranges").toObject(); - PageRangeLow = PageRanges["low"].toInt(); - PageRangeHigh = PageRanges["high"].toInt(); + pdfPageRangeAdjustNeeded = true; + attrs.remove("page-ranges"); } } @@ -457,8 +466,13 @@ void IppPrinter::print(QJsonObject attrs, QString filename, IppMsg job = mk_msg(o, attrs); QByteArray contents = job.encode(IppMsg::PrintJob); - // Always convert non-jpeg images to get resizing - if((mimeType == documentFormat) && (documentFormat == "image/jpeg" || !mimeType.contains("image"))) + + // Non-jpeg images, Postscript and PDF (when not adjusting pages locally) + // Always convert non-jpeg images to get resizing + // TODO: make this sane + if((mimeType == documentFormat) + && (documentFormat == "image/jpeg" || !mimeType.contains("image")) + && !((documentFormat == "application/pdf") && pdfPageRangeAdjustNeeded)) { QByteArray filedata = file.readAll(); contents = contents.append(filedata);