diff --git a/js/qz-tray.js b/js/qz-tray.js index 316e4c150..f3ecda178 100644 --- a/js/qz-tray.js +++ b/js/qz-tray.js @@ -403,6 +403,7 @@ var qz = (function() { defaultConfig: { //value purposes are explained in the qz.configs.setDefaults docs + bounds: null, colorType: 'color', copies: 1, density: 0, @@ -1091,6 +1092,11 @@ var qz = (function() { * * @param {Object} options Default options used by printer configs if not overridden. * + * @param {Object} [option.bounds=null] Bounding box rectangle. + * @param {number} [options.bounds.x=0] Distance from left for bounding box starting corner + * @param {number} [options.bounds.y=0] Distance from top for bounding box starting corner + * @param {number} [options.bounds.width=0] Width of bounding box + * @param {number} [options.bounds.height=0] Height of bounding box * @param {string} [options.colorType='color'] Valid values [color | grayscale | blackwhite] * @param {number} [options.copies=1] Number of copies to be printed. * @param {number|Array} [options.density=0] Pixel density (DPI, DPMM, or DPCM depending on [options.units]). diff --git a/sample.html b/sample.html index f8567f65e..c96a2c6c4 100644 --- a/sample.html +++ b/sample.html @@ -486,6 +486,40 @@

Pixel Printing

+ +
+ + ( + + + ) +
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
@@ -2242,6 +2276,13 @@

Options

$("#pxlSizeActive").prop('checked', false); $("#pxlSizeGroup").css('display', 'none'); + $("#pxlBoundX").val(0); + $("#pxlBoundY").val(0); + $("#pxlBoundWidth").val(''); + $("#pxlBoundHeight").val(''); + $("#pxlBoundsActive").prop('checked', false); + $("#pxlBoundsGroup").css('display', 'none'); + //printer $("#pPxlWidth").val(''); $("#pPxlHeight").val(''); @@ -2680,6 +2721,16 @@

Options

}; } + var pxlBounds = null; + if (isChecked($("#pxlBoundsActive"), cleanConditions['pxlBoundsActive'])) { + pxlBounds = { + x: $("#pxlBoundX").val(), + y: $("#pxlBoundY").val(), + width: $("#pxlBoundWidth").val(), + height: $("#pxlBoundHeight").val() + }; + } + var pxlMargins = $("#pxlMargins").val(); if (isChecked($("#pxlMarginsActive"), cleanConditions['pxlMarginsActive'])) { pxlMargins = { @@ -2706,6 +2757,7 @@

Options

endOfDoc: $("#rawEndOfDoc").val(), perSpool: $("#rawPerSpool").val(), + bounds: pxlBounds, colorType: $("#pxlColorType").val(), copies: copies, density: $("#pxlDensity").val(), diff --git a/src/qz/printer/PrintOptions.java b/src/qz/printer/PrintOptions.java index ddcb2c47a..829081bd6 100644 --- a/src/qz/printer/PrintOptions.java +++ b/src/qz/printer/PrintOptions.java @@ -69,6 +69,15 @@ public PrintOptions(JSONObject configOpts, PrintOutput output, PrintingUtilities } //check for pixel options + if (!configOpts.isNull("bounds")) { + try { + JSONObject bounds = configOpts.getJSONObject("bounds"); + psOptions.bounds = new Bounds(bounds.optDouble("x", 0), bounds.optDouble("y", 0), bounds.optDouble("width", 0), bounds.optDouble("height", 0)); + } + catch(JSONException e) { + LoggerUtilities.optionWarn(log, "JSONObject", "bounds", configOpts.opt("bounds")); + } + } if (!configOpts.isNull("colorType")) { try { psOptions.colorType = ColorType.valueOf(configOpts.optString("colorType").toUpperCase(Locale.ENGLISH)); @@ -319,6 +328,7 @@ public String getJobName(String defaultVal) { /** Pixel printing options */ public class Pixel { + private Bounds bounds = null; //Bounding box rectangle private ColorType colorType = ColorType.COLOR; //Color / black&white private int copies = 1; //Job copies private double density = 0; //Pixel density (DPI or DPMM) @@ -338,6 +348,10 @@ public class Pixel { private Unit units = Unit.INCH; //Units for density, margins, size + public Bounds getBounds() { + return bounds; + } + public ColorType getColorType() { return colorType; } @@ -478,6 +492,37 @@ public double left() { } } + /* Bounding box generic rectangle */ + public class Bounds { + private double x; + private double y; + private double width; + private double height; + + public Bounds(double x, double y, double width, double height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + public double getX() { + return x; + } + + public double getY() { + return y; + } + + public double getWidth() { + return width; + } + + public double getHeight() { + return height; + } + } + /** Pixel dimension values */ public enum Unit { INCH(ResolutionSyntax.DPI, 1.0f, 1.0f, Size2DSyntax.INCH), //1in = 1in diff --git a/src/qz/printer/action/PrintPDF.java b/src/qz/printer/action/PrintPDF.java index a06c0d710..d169cf1e8 100644 --- a/src/qz/printer/action/PrintPDF.java +++ b/src/qz/printer/action/PrintPDF.java @@ -25,9 +25,9 @@ import qz.utils.SystemUtilities; import javax.print.attribute.PrintRequestAttributeSet; -import java.awt.*; import javax.print.attribute.standard.Media; import javax.print.attribute.standard.MediaPrintableArea; +import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.print.PageFormat; import java.awt.print.Paper; @@ -62,6 +62,9 @@ public PrintingUtilities.Format getFormat() { @Override public void parseData(JSONArray printData, PrintOptions options) throws JSONException, UnsupportedOperationException { + PrintOptions.Pixel pxlOpts = options.getPixelOptions(); + double convert = 72.0 / pxlOpts.getUnits().as1Inch(); + for(int i = 0; i < printData.length(); i++) { JSONObject data = printData.getJSONObject(i); @@ -69,10 +72,10 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce JSONObject dataOpt = data.getJSONObject("options"); if (!dataOpt.isNull("pageWidth") && dataOpt.optDouble("pageWidth") > 0) { - docWidth = dataOpt.optDouble("pageWidth") * (72.0 / options.getPixelOptions().getUnits().as1Inch()); + docWidth = dataOpt.optDouble("pageWidth") * convert; } if (!dataOpt.isNull("pageHeight") && dataOpt.optDouble("pageHeight") > 0) { - docHeight = dataOpt.optDouble("pageHeight") * (72.0 / options.getPixelOptions().getUnits().as1Inch()); + docHeight = dataOpt.optDouble("pageHeight") * convert; } } @@ -86,6 +89,19 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce doc = PDDocument.load(ConnectionUtilities.getInputStream(data.getString("data"))); } + if (pxlOpts.getBounds() != null) { + PrintOptions.Bounds bnd = pxlOpts.getBounds(); + + for(PDPage page : doc.getPages()) { + PDRectangle box = new PDRectangle( + (float)(bnd.getX() * convert), + page.getMediaBox().getUpperRightY() - (float)((bnd.getHeight() + bnd.getY()) * convert), + (float)(bnd.getWidth() * convert), + (float)(bnd.getHeight() * convert)); + page.setMediaBox(box); + } + } + originals.add(doc); printables.addAll(splitter.split(doc)); }