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

Allow spool limits for pdf prints #728

Merged
merged 7 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions js/qz-tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ var qz = (function() {
altPrinting: false,
encoding: null,
endOfDoc: null,
perSpool: 1
perSpool: null // 1 for raw
}
},

Expand Down Expand Up @@ -1306,7 +1306,7 @@ var qz = (function() {
* @param {boolean} [options.altPrinting=false] Print the specified file using CUPS command line arguments. Has no effect on Windows.
* @param {string} [options.encoding=null] Character set
* @param {string} [options.endOfDoc=null]
* @param {number} [options.perSpool=1] Number of pages per spool.
* @param {number} [options.perSpool=null] Number of pages per spool. Defaults to <code>1</code> for raw print jobs.
tresf marked this conversation as resolved.
Show resolved Hide resolved
*
* @memberof qz.configs
*/
Expand Down
11 changes: 10 additions & 1 deletion sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,11 @@ <h3>Pixel Printing</h3>
<label for="pxlRotation">Rotation</label>
<input type="number" step="any" id="pxlRotation" class="form-control pull-right" />
</div>

<div class="form-group form-inline">
<label for="pxlSpool">Per Spool</label>
<input type="number" id="pxlSpool" class="form-control pull-right" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
Expand Down Expand Up @@ -2302,6 +2307,7 @@ <h4 class="panel-title">Options</h4>
$("#pxlPrinterTray").val(null);
$("#pxlRasterize").prop('checked', false);
$("#pxlRotation").val(0);
$("#pxlSpool").val("");
$("#pxlScale").prop('checked', true);
$("#pxlUnitsIN").prop('checked', true);

Expand Down Expand Up @@ -2825,20 +2831,23 @@ <h4 class="panel-title">Options</h4>
}

var copies = 1;
var perSpool = null;
var jobName = null;
if ($("#rawTab").hasClass("active")) {
copies = includedValue($("#rawCopies"));
perSpool = includedValue($("#rawPerSpool"));
jobName = includedValue($("#rawJobName"));
} else {
copies = includedValue($("#pxlCopies"));
perSpool = includedValue($("#pxlSpool"));
jobName = includedValue($("#pxlJobName"));
}

cfg.reconfigure({
altPrinting: includedValue($("#rawAltPrinting"), isChecked($("#rawAltPrinting"), cleanConditions['rawAltPrinting'])),
encoding: includedValue($("#rawEncoding")),
endOfDoc: includedValue($("#rawEndOfDoc")),
perSpool: includedValue($("#rawPerSpool")),
perSpool: perSpool,

bounds: pxlBounds,
colorType: includedValue($("#pxlColorType")),
Expand Down
68 changes: 43 additions & 25 deletions src/qz/printer/BookBundle.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package qz.printer;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qz.utils.SystemUtilities;

import javax.print.attribute.standard.OrientationRequested;
import java.awt.*;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;

/**
* Wrapper of the {@code Book} class as a {@code Printable} type,
* since PrinterJob implementations do not seem to handle the {@code Pageable} interface properly.
*/
public class BookBundle extends Book implements Printable {
public class BookBundle extends Book {

private static final Logger log = LoggerFactory.getLogger(BookBundle.class);

Expand All @@ -30,33 +24,57 @@ public BookBundle() {
super();
}

@Override
public int print(Graphics g, PageFormat format, int pageIndex) throws PrinterException {
log.trace("Requested page {} for printing", pageIndex);

if (pageIndex < getNumberOfPages()) {
Printable printable = getPrintable(pageIndex);
if (printable != lastPrint) {
lastPrint = printable;
lastStarted = pageIndex;
}

return printable.print(g, format, pageIndex - lastStarted);
}

return NO_SUCH_PAGE;
}

/**
* Wrapper of the wrapper class so that PrinterJob implementations will handle it as proper pageable
*/
public Book wrapAndPresent() {
Book cover = new Book();
for(int i = 0; i < getNumberOfPages(); i++) {
cover.append(this, getPageFormat(i));
cover.append(new PrintingPress(), getPageFormat(i));
}

return cover;
}

public Book wrapAndPresent(int offset, int length) {
Book coverSubset = new Book();
for(int i = offset; i < offset + length && i < getNumberOfPages(); i++) {
coverSubset.append(new PrintingPress(offset), getPageFormat(i));
}

return coverSubset;
}


/** Printable wrapper to ensure proper reading of multiple documents across spooling */
private class PrintingPress implements Printable {
private int pageOffset;

public PrintingPress() {
this(0);
}

public PrintingPress(int offset) {
pageOffset = offset;
}

@Override
public int print(Graphics g, PageFormat format, int pageIndex) throws PrinterException {
pageIndex += pageOffset;
log.trace("Requested page {} for printing", pageIndex);

if (pageIndex < getNumberOfPages()) {
Printable printable = getPrintable(pageIndex);
if (printable != lastPrint) {
lastPrint = printable;
lastStarted = pageIndex;
}

return printable.print(g, format, pageIndex - lastStarted);
}

return NO_SUCH_PAGE;
}
}

}
8 changes: 8 additions & 0 deletions src/qz/printer/PrintOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ public PrintOptions(JSONObject configOpts, PrintOutput output, PrintingUtilities
try { psOptions.paperThickness = configOpts.getDouble("paperThickness"); }
catch(JSONException e) { LoggerUtilities.optionWarn(log, "double", "paperThickness", configOpts.opt("paperThickness")); }
}
if (!configOpts.isNull("perSpool")) {
psOptions.perSpool = configOpts.optInt("perSpool", 0);
}
if (!configOpts.isNull("printerTray")) {
psOptions.printerTray = configOpts.optString("printerTray", null);
}
Expand Down Expand Up @@ -404,6 +407,7 @@ public class Pixel {
private Margins margins = new Margins(); //Page margins
private Orientation orientation = null; //Page orientation
private double paperThickness = -1; //Paper thickness
private int perSpool = 0; //Pages before sending to printer
private String printerTray = null; //Printer tray to use
private boolean rasterize = true; //Whether documents are rasterized before printing
private double rotation = 0; //Image rotation
Expand Down Expand Up @@ -464,6 +468,10 @@ public double getPaperThickness() {
return paperThickness;
}

public int getPerSpool() {
return perSpool;
}

public String getPrinterTray() {
return printerTray;
}
Expand Down
19 changes: 16 additions & 3 deletions src/qz/printer/action/PrintPDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,23 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
bundle.append(new PDFWrapper(doc, scale, false, (float)(useDensity * pxlOpts.getUnits().as1Inch()), false, pxlOpts.getOrientation(), hints), page, doc.getNumberOfPages());
}

job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT));
job.setPageable(bundle.wrapAndPresent());
if (pxlOpts.getPerSpool() > 0 && bundle.getNumberOfPages() > pxlOpts.getPerSpool()) {
int jobNum = 1;
int offset = 0;
while(offset < bundle.getNumberOfPages()) {
job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT) + "-" + jobNum++);
job.setPageable(bundle.wrapAndPresent(offset, pxlOpts.getPerSpool()));

printCopies(output, pxlOpts, job, attributes);
printCopies(output, pxlOpts, job, attributes);

offset += pxlOpts.getPerSpool();
}
} else {
job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT));
job.setPageable(bundle.wrapAndPresent());

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

private void rotatePage(PDDocument doc, PDPage page, double rotation) {
Expand Down