Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pdf bounding box #563

Merged
merged 3 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 <code>[color | grayscale | blackwhite]</code>
* @param {number} [options.copies=1] Number of copies to be printed.
* @param {number|Array<number>} [options.density=0] Pixel density (DPI, DPMM, or DPCM depending on <code>[options.units]</code>).
Expand Down
52 changes: 52 additions & 0 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,40 @@ <h3>Pixel Printing</h3>
<input type="number" step="any" id="pxlSizeHeight" class="form-control pull-right" />
</div>
</div>

<div class="form-group">
<label class="inline">Bounds</label>
(
<label for="pxlBoundsActive" class="inline">Enable:</label>
<input type="checkbox" id="pxlBoundsActive" onclick="checkGroupActive('pxlBoundsActive', 'pxlBoundsGroup');" />
)
</div>
<div class="inline" id="pxlBoundsGroup">
<div class="form-group form-inline">
<label for="pxlBoundX" class="tip" data-toggle="tooltip" title="In relation to units specified">
&nbsp; X:
</label>
<input type="number" step="any" id="pxlBoundX" class="form-control pull-right" />
</div>
<div class="form-group form-inline">
<label for="pxlBoundY" class="tip" data-toggle="tooltip" title="In relation to units specified">
&nbsp; Y:
</label>
<input type="number" step="any" id="pxlBoundY" class="form-control pull-right" />
</div>
<div class="form-group form-inline">
<label for="pxlBoundWidth" class="tip" data-toggle="tooltip" title="In relation to units specified">
&nbsp; Width:
</label>
<input type="number" step="any" id="pxlBoundWidth" class="form-control pull-right" />
</div>
<div class="form-group form-inline">
<label for="pxlBoundHeight" class="tip" data-toggle="tooltip" title="In relation to units specified">
&nbsp; Height:
</label>
<input type="number" step="any" id="pxlBoundHeight" class="form-control pull-right" />
</div>
</div>
</div>
</div>
</fieldset>
Expand Down Expand Up @@ -2242,6 +2276,13 @@ <h4 class="panel-title">Options</h4>
$("#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('');
Expand Down Expand Up @@ -2680,6 +2721,16 @@ <h4 class="panel-title">Options</h4>
};
}

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 = {
Expand All @@ -2706,6 +2757,7 @@ <h4 class="panel-title">Options</h4>
endOfDoc: $("#rawEndOfDoc").val(),
perSpool: $("#rawPerSpool").val(),

bounds: pxlBounds,
colorType: $("#pxlColorType").val(),
copies: copies,
density: $("#pxlDensity").val(),
Expand Down
45 changes: 45 additions & 0 deletions src/qz/printer/PrintOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions src/qz/printer/action/PrintPDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,17 +62,20 @@ 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);

if (!data.isNull("options")) {
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;
}
}

Expand All @@ -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));
}
Expand Down