Skip to content

Commit 9626758

Browse files
committed
Allow for custom page sizes on image prints
1 parent 38f6434 commit 9626758

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

js/qz-tray.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,7 @@ var qz = (function() {
15491549
* @param {Object} [options.size=null] Paper size.
15501550
* @param {number} [options.size.width=null] Page width.
15511551
* @param {number} [options.size.height=null] Page height.
1552+
* @param {boolean} [options.size.custom=false] If the provided page size is not included in the driver.
15521553
* @param {string} [options.units='in'] Page units, applies to paper size, margins, and density. Valid value <code>[in | cm | mm]</code>
15531554
*
15541555
* @param {boolean} [options.forceRaw=false] Print the specified raw data using direct method, skipping the driver. Not yet supported on Windows.

sample.html

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,11 @@ <h3>Pixel Printing</h3>
589589
</label>
590590
<input type="number" step="any" id="pxlSizeHeight" class="form-control pull-right" />
591591
</div>
592+
593+
<div class="form-group form-inline">
594+
<label for="pxlSizeForce">&nbsp; As Custom Size</label>
595+
<input type="checkbox" id="pxlSizeForce" class="pull-right" />
596+
</div>
592597
</div>
593598

594599
<div class="form-group">
@@ -2150,7 +2155,7 @@ <h4 class="panel-title">Options</h4>
21502155
//print options not used with this flavor
21512156

21522157
var printData = [
2153-
{ type: 'pixel', format: 'image', flavor: 'file', data: 'assets/img/image_sample.png' }
2158+
{ type: 'pixel', format: 'image', flavor: 'file', data: 'assets/test/custom_sized_tester.png' }
21542159
//also valid, as format and flavor will default to proper values:
21552160
// { type: 'pixel', data: 'assets/img/image_sample.png' }
21562161
];
@@ -2698,6 +2703,7 @@ <h4 class="panel-title">Options</h4>
26982703

26992704
$("#pxlSizeWidth").val('');
27002705
$("#pxlSizeHeight").val('');
2706+
$("#pxlSizeForce").prop('checked', false);
27012707
$("#pxlSizeActive").prop('checked', false);
27022708
$("#pxlSizeGroup").css('display', 'none');
27032709

@@ -3252,7 +3258,8 @@ <h4 class="panel-title">Options</h4>
32523258
if (isChecked($("#pxlSizeActive"), cleanConditions['pxlSizeActive']) && (($("#pxlSizeWidth").val() !== '') || ($("#pxlSizeHeight").val() !== ''))) {
32533259
pxlSize = {
32543260
width: $("#pxlSizeWidth").val(),
3255-
height: $("#pxlSizeHeight").val()
3261+
height: $("#pxlSizeHeight").val(),
3262+
custom: $("#pxlSizeForce").prop('checked')
32563263
};
32573264
}
32583265

@@ -3446,7 +3453,7 @@ <h4 class="panel-title">Options</h4>
34463453
// Ensure we're on the right tab
34473454
stayOnTab = true;
34483455
}
3449-
3456+
34503457
var setupDropButton = function(format, name, data, stayOnTab) {
34513458
var tabHref;
34523459
var buttonId;
@@ -3461,7 +3468,7 @@ <h4 class="panel-title">Options</h4>
34613468
buttonId = "#rawDropBtn"
34623469
tabHref = "#rawContent";
34633470
}
3464-
3471+
34653472
var button = $(buttonId);
34663473
if(stayOnTab) {
34673474
// File upload button can't change tabs
@@ -3489,7 +3496,7 @@ <h4 class="panel-title">Options</h4>
34893496

34903497
if (file) {
34913498
console.log("Dropped file:", file);
3492-
getBase64(file).then(function(base64) {
3499+
getBase64(file).then(function(base64) {
34933500
var name = file.name;
34943501
var extension = name.substring(name.lastIndexOf('.') + 1, name.length) || '';
34953502
switch(extension.toLowerCase()) {
@@ -3511,7 +3518,7 @@ <h4 class="panel-title">Options</h4>
35113518
default:
35123519
setupDropButton("command", name, base64, stayOnTab);
35133520
}
3514-
}).catch(displayError);
3521+
}).catch(displayError);
35153522
}
35163523
}
35173524

src/qz/printer/PrintOptions.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ public PrintOptions(JSONObject configOpts, PrintOutput output, PrintingUtilities
349349
catch(JSONException e) { LoggerUtilities.optionWarn(log, "double", "size.height", subSize.opt("height")); }
350350
}
351351

352+
if (!subSize.isNull("custom")) {
353+
try { s.custom = subSize.getBoolean("custom"); }
354+
catch(JSONException e) { LoggerUtilities.optionWarn(log, "boolean", "size.custom", subSize.opt("custom")); }
355+
}
356+
352357
if (s.height <= 0 && s.width <= 0) {
353358
log.warn("Page size has been set without dimensions, using default");
354359
} else {
@@ -586,13 +591,18 @@ public Size getPageSize() {
586591
public class Size {
587592
private double width = -1; //Page width
588593
private double height = -1; //Page height
589-
594+
private boolean custom = false; //Force a custom size
590595

591596
public Size() {}
592597

593598
public Size(double width, double height) {
599+
this(width, height, false);
600+
}
601+
602+
public Size(double width, double height, boolean custom) {
594603
this.width = width;
595604
this.height = height;
605+
this.custom = custom;
596606
}
597607

598608
public double getWidth() {
@@ -602,6 +612,10 @@ public double getWidth() {
602612
public double getHeight() {
603613
return height;
604614
}
615+
616+
public boolean isCustom() {
617+
return custom;
618+
}
605619
}
606620

607621
/** Pixel page margins options */

src/qz/printer/action/PrintImage.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
188188
}
189189

190190
job.setJobName(pxlOpts.getJobName(Constants.IMAGE_PRINT));
191-
job.setPrintable(this, job.validatePage(page));
191+
192+
if (pxlOpts.getSize() != null && pxlOpts.getSize().isCustom()) {
193+
// forcing a custom size, skip validation
194+
job.setPrintable(this, page);
195+
} else {
196+
//expecting a valid paper size, validate
197+
job.setPrintable(this, job.validatePage(page));
198+
}
192199

193200
printCopies(output, pxlOpts, job, attributes);
194201
}

0 commit comments

Comments
 (0)