Skip to content

Commit

Permalink
PDF font render improvements (qzind#1108)
Browse files Browse the repository at this point in the history
* PDF font rendering improvements
* Improvements to "ignoreTransparency" logic

Co-authored-by: Berenz <thebrettberenz@gmail.com>
  • Loading branch information
tresf and Berenz authored Jun 22, 2023
1 parent 42ba86a commit 1d37b1f
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 98 deletions.
2 changes: 2 additions & 0 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,8 @@ var qz = (function() {
* @param {string} [data.options.pageRanges] Optional with <code>[pdf]</code> formats. Comma-separated list of page ranges to include.
* @param {boolean} [data.options.ignoreTransparency=false] Optional with <code>[pdf]</code> formats. Instructs transparent PDF elements to be ignored.
* Transparent PDF elements are known to degrade performance and quality when printing.
* @param {boolean} [data.options.altFontRendering=false] Optional with <code>[pdf]</code> formats. Instructs PDF to be rendered using PDFBOX 1.8 techniques.
* Drastically improves low-DPI PDF print quality on Windows.
* @param {...*} [arguments] Additionally three more parameters can be specified:<p/>
* <code>{boolean} [resumeOnError=false]</code> Whether the chain should continue printing if it hits an error on one the the prints.<p/>
* <code>{string|Array<string>} [signature]</code> Pre-signed signature(s) of the JSON string for containing <code>call</code>, <code>params</code>, and <code>timestamp</code>.<p/>
Expand Down
9 changes: 7 additions & 2 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,13 @@ <h3>Pixel Printing</h3>
<input type="number" id="pPxlHeight" class="form-control pull-right" />
</div>
<div class="form-group form-inline">
<label for="pPxlTransparent">Ignore Transparency:</label>
<label for="pPxlTransparent">Ignore Transparency (PDF only):</label>
<input type="checkbox" id="pPxlTransparent" class="pull-right" />
</div>
<div class="form-group form-inline">
<label for="pPxlAltFontRendering">Alternate Font Rendering (PDF only):</label>
<input type="checkbox" id="pPxlAltFontRendering" class="pull-right" />
</div>
</div>
</div>
</fieldset>
Expand Down Expand Up @@ -3231,7 +3235,8 @@ <h4 class="panel-title">Options</h4>
pageWidth: $("#pPxlWidth").val(),
pageHeight: $("#pPxlHeight").val(),
pageRanges: $("#pPxlRange").val(),
ignoreTransparency: $("#pPxlTransparent").prop('checked')
ignoreTransparency: $("#pPxlTransparent").prop('checked'),
altFontRendering: $("#pPxlAltFontRendering").prop('checked')
};
} else {
return {
Expand Down
6 changes: 4 additions & 2 deletions src/qz/printer/action/PrintPDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand All @@ -51,6 +50,7 @@ public class PrintPDF extends PrintPixel implements PrintProcessor {
private double docWidth = 0;
private double docHeight = 0;
private boolean ignoreTransparency = false;
private boolean altFontRendering = false;


public PrintPDF() {
Expand Down Expand Up @@ -83,6 +83,7 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce
}

ignoreTransparency = dataOpt.optBoolean("ignoreTransparency", false);
altFontRendering = dataOpt.optBoolean("altFontRendering", false);

if (!dataOpt.isNull("pageRanges")) {
String[] ranges = dataOpt.optString("pageRanges", "").split(",");
Expand Down Expand Up @@ -252,7 +253,7 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
}
}

PDFWrapper wrapper = new PDFWrapper(doc, scale, false, ignoreTransparency,
PDFWrapper wrapper = new PDFWrapper(doc, scale, false, ignoreTransparency, altFontRendering,
(float)(useDensity * pxlOpts.getUnits().as1Inch()),
false, pxlOpts.getOrientation(), hints);

Expand Down Expand Up @@ -320,5 +321,6 @@ public void cleanup() {
docWidth = 0;
docHeight = 0;
ignoreTransparency = false;
altFontRendering = false;
}
}
86 changes: 0 additions & 86 deletions src/qz/printer/action/pdf/OpaquePDFRenderer.java

This file was deleted.

10 changes: 2 additions & 8 deletions src/qz/printer/action/pdf/PDFWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,14 @@ public class PDFWrapper implements Printable {

private PDFPrintable printable;

public PDFWrapper(PDDocument document, Scaling scaling, boolean showPageBorder, boolean ignoreTransparency, float dpi, boolean center, PrintOptions.Orientation orientation, RenderingHints hints) {
public PDFWrapper(PDDocument document, Scaling scaling, boolean showPageBorder, boolean ignoreTransparency, boolean useAlternateFontRendering, float dpi, boolean center, PrintOptions.Orientation orientation, RenderingHints hints) {
this.document = document;
this.scaling = scaling;
if (orientation != null) {
this.orientation = orientation.getAsOrientRequested();
}

PDFRenderer renderer;
if (ignoreTransparency) {
renderer = new OpaquePDFRenderer(document);
} else {
renderer = new PDFRenderer(document);
}

PDFRenderer renderer = new ParamPdfRenderer(document, useAlternateFontRendering, ignoreTransparency);
printable = new PDFPrintable(document, scaling, showPageBorder, dpi, center, renderer);
printable.setRenderingHints(hints);
}
Expand Down
47 changes: 47 additions & 0 deletions src/qz/printer/action/pdf/ParamPdfRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package qz.printer.action.pdf;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.rendering.PageDrawer;
import org.apache.pdfbox.rendering.PageDrawerParameters;
import qz.printer.rendering.OpaqueDrawObject;
import qz.printer.rendering.OpaqueGraphicStateParameters;
import qz.printer.rendering.PdfFontPageDrawer;

import java.io.IOException;

public class ParamPdfRenderer extends PDFRenderer {

private boolean useAlternateFontRendering;
private boolean ignoreTransparency;

public ParamPdfRenderer(PDDocument document, boolean useAlternateFontRendering, boolean ignoreTransparency) {
super(document);

this.useAlternateFontRendering = useAlternateFontRendering;
this.ignoreTransparency = ignoreTransparency;
}

@Override
protected PageDrawer createPageDrawer(PageDrawerParameters parameters) throws IOException {
if (useAlternateFontRendering) {
return new PdfFontPageDrawer(parameters, ignoreTransparency);
} else if(ignoreTransparency) {
return new OpaquePageDrawer(parameters);
}
// Fallback to default PageDrawer
return new PageDrawer(parameters);
}

// override drawer to make use of customized draw object
private static class OpaquePageDrawer extends PageDrawer {
public OpaquePageDrawer(PageDrawerParameters parameters) throws IOException {
super(parameters);

// Note: These must match PdfFontPageDrawer's ignoreTransparency condition
addOperator(new OpaqueDrawObject());
addOperator(new OpaqueGraphicStateParameters());
}
}
}

Loading

0 comments on commit 1d37b1f

Please sign in to comment.