use of qz.printer.PrintOptions in project tray by qzind.
the class PrintPDF method parseData.
@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);
HashSet<Integer> pagesToPrint = new HashSet<>();
if (!data.isNull("options")) {
JSONObject dataOpt = data.getJSONObject("options");
if (!dataOpt.isNull("pageWidth") && dataOpt.optDouble("pageWidth") > 0) {
docWidth = dataOpt.optDouble("pageWidth") * convert;
}
if (!dataOpt.isNull("pageHeight") && dataOpt.optDouble("pageHeight") > 0) {
docHeight = dataOpt.optDouble("pageHeight") * convert;
}
if (!dataOpt.isNull("pageRanges")) {
String[] ranges = dataOpt.optString("pageRanges", "").split(",");
for (String range : ranges) {
String[] period = range.split("-");
try {
int start = Integer.parseInt(period[0]);
pagesToPrint.add(start);
if (period.length > 1) {
int end = Integer.parseInt(period[period.length - 1]);
pagesToPrint.addAll(IntStream.rangeClosed(start, end).boxed().collect(Collectors.toSet()));
}
} catch (NumberFormatException nfe) {
log.warn("Unable to parse page range {}.", range);
}
}
}
}
PrintingUtilities.Flavor flavor = PrintingUtilities.Flavor.valueOf(data.optString("flavor", "FILE").toUpperCase(Locale.ENGLISH));
try {
PDDocument doc;
if (flavor == PrintingUtilities.Flavor.BASE64) {
doc = PDDocument.load(new ByteArrayInputStream(Base64.decodeBase64(data.getString("data"))));
} else {
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);
}
}
if (pagesToPrint.isEmpty()) {
pagesToPrint.addAll(IntStream.rangeClosed(1, doc.getNumberOfPages()).boxed().collect(Collectors.toSet()));
}
originals.add(doc);
List<PDDocument> splitPages = splitter.split(doc);
// ensures non-ranged page will still get closed
originals.addAll(splitPages);
for (int pg = 0; pg < splitPages.size(); pg++) {
if (pagesToPrint.contains(pg + 1)) {
// ranges are 1-indexed
printables.add(splitPages.get(pg));
}
}
} catch (FileNotFoundException e) {
throw new UnsupportedOperationException("PDF file specified could not be found.", e);
} catch (IOException e) {
throw new UnsupportedOperationException(String.format("Cannot parse (%s)%s as a PDF file: %s", flavor, data.getString("data"), e.getLocalizedMessage()), e);
}
}
log.debug("Parsed {} files for printing", printables.size());
}
use of qz.printer.PrintOptions in project tray by qzind.
the class PrintingUtilities method processPrintRequest.
/**
* Determine print variables and send data to printer
*
* @param session WebSocket session
* @param UID ID of call from web API
* @param params Params of call from web API
*/
public static void processPrintRequest(Session session, String UID, JSONObject params) throws JSONException {
Format format = getPrintFormat(params.getJSONArray("data"));
PrintProcessor processor = PrintingUtilities.getPrintProcessor(format);
log.debug("Using {} to print", processor.getClass().getName());
try {
PrintOutput output = new PrintOutput(params.optJSONObject("printer"));
PrintOptions options = new PrintOptions(params.optJSONObject("options"), output, format);
processor.parseData(params.getJSONArray("data"), options);
processor.print(output, options);
log.info("Printing complete");
PrintSocketClient.sendResult(session, UID, null);
} catch (PrinterAbortException e) {
log.warn("Printing cancelled");
PrintSocketClient.sendError(session, UID, "Printing cancelled");
} catch (Exception e) {
log.error("Failed to print", e);
PrintSocketClient.sendError(session, UID, e);
} finally {
PrintingUtilities.releasePrintProcessor(processor);
}
}
use of qz.printer.PrintOptions in project tray by qzind.
the class PrintRaw method print.
@Override
public void print(PrintOutput output, PrintOptions options) throws PrintException {
PrintOptions.Raw rawOpts = options.getRawOptions();
List<ByteArrayBuilder> pages;
if (rawOpts.getSpoolSize() > 0 && rawOpts.getSpoolEnd() != null && !rawOpts.getSpoolEnd().isEmpty()) {
try {
pages = ByteUtilities.splitByteArray(commands.getByteArray(), rawOpts.getSpoolEnd().getBytes(destEncoding), rawOpts.getSpoolSize());
} catch (UnsupportedEncodingException e) {
throw new PrintException(e);
}
} else {
pages = new ArrayList<>();
pages.add(commands);
}
for (int i = 0; i < rawOpts.getCopies(); i++) {
for (ByteArrayBuilder bab : pages) {
try {
if (output.isSetHost()) {
printToHost(output.getHost(), output.getPort(), bab.getByteArray());
} else if (output.isSetFile()) {
printToFile(output.getFile(), bab.getByteArray());
} else {
if (rawOpts.isForceRaw()) {
if (SystemUtilities.isWindows()) {
// Placeholder only; not yet supported
printToBackend(output.getNativePrinter(), bab.getByteArray(), rawOpts.isRetainTemp(), Backend.WIN32_WMI);
} else {
// Try CUPS backend first, fallback to LPR
printToBackend(output.getNativePrinter(), bab.getByteArray(), rawOpts.isRetainTemp(), Backend.CUPS_RSS, Backend.CUPS_LPR);
}
} else {
printToPrinter(output.getPrintService(), bab.getByteArray(), rawOpts);
}
}
} catch (IOException e) {
throw new PrintException(e);
}
}
}
}
Aggregations