use of qz.printer.PrintOptions in project tray by qzind.
the class PrintRaw method parseData.
@Override
public void parseData(JSONArray printData, PrintOptions options) throws JSONException, UnsupportedOperationException {
for (int i = 0; i < printData.length(); i++) {
JSONObject data = printData.optJSONObject(i);
if (data == null) {
data = new JSONObject();
data.put("data", printData.getString(i));
}
String cmd = data.getString("data");
JSONObject opt = data.optJSONObject("options");
if (opt == null) {
opt = new JSONObject();
}
PrintingUtilities.Format format = PrintingUtilities.Format.valueOf(data.optString("format", "COMMAND").toUpperCase(Locale.ENGLISH));
PrintingUtilities.Flavor flavor = PrintingUtilities.Flavor.valueOf(data.optString("flavor", "PLAIN").toUpperCase(Locale.ENGLISH));
PrintOptions.Raw rawOpts = options.getRawOptions();
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
destEncoding = rawOpts.getDestEncoding();
if (destEncoding == null || destEncoding.isEmpty()) {
destEncoding = Charset.defaultCharset().name();
}
try {
switch(format) {
case HTML:
commands.append(getHtmlWrapper(cmd, opt, flavor, rawOpts, pxlOpts).getImageCommand(opt));
break;
case IMAGE:
commands.append(getImageWrapper(cmd, opt, flavor, rawOpts, pxlOpts).getImageCommand(opt));
break;
case PDF:
commands.append(getPdfWrapper(cmd, opt, flavor, rawOpts, pxlOpts).getImageCommand(opt));
break;
case COMMAND:
default:
switch(flavor) {
case BASE64:
commands.append(seekConversion(Base64.decodeBase64(cmd), rawOpts));
break;
case FILE:
commands.append(seekConversion(FileUtilities.readRawFile(cmd), rawOpts));
break;
case HEX:
commands.append(seekConversion(ByteUtilities.hexStringToByteArray(cmd), rawOpts));
break;
case XML:
commands.append(seekConversion(Base64.decodeBase64(FileUtilities.readXMLFile(cmd, opt.optString("xmlTag"))), rawOpts));
break;
case PLAIN:
default:
commands.append(getBytes(cmd, destEncoding));
break;
}
break;
}
} catch (Exception e) {
throw new UnsupportedOperationException(String.format("Cannot parse (%s)%s into a raw %s command: %s", flavor, data.getString("data"), format, e.getLocalizedMessage()), e);
}
}
}
use of qz.printer.PrintOptions in project tray by qzind.
the class PrintHTML method printLegacy.
private void printLegacy(PrintOutput output, PrintOptions options) throws PrinterException {
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
java.awt.print.PrinterJob job = java.awt.print.PrinterJob.getPrinterJob();
job.setPrintService(output.getPrintService());
PageFormat page = job.getPageFormat(null);
PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page, output.getSupportedMedia());
// setup swing ui
JFrame legacyFrame = new JFrame(pxlOpts.getJobName(Constants.HTML_PRINT));
legacyFrame.setUndecorated(true);
legacyFrame.setLayout(new FlowLayout());
legacyFrame.setExtendedState(Frame.ICONIFIED);
legacyLabel = new JLabel();
legacyLabel.setOpaque(true);
legacyLabel.setBackground(Color.WHITE);
legacyLabel.setBorder(null);
legacyLabel.setDoubleBuffered(false);
legacyFrame.add(legacyLabel);
try {
for (WebAppModel model : models) {
if (model.isPlainText()) {
legacyLabel.setText(cleanHtmlContent(model.getSource()));
} else {
try (InputStream fis = new URL(model.getSource()).openStream()) {
String webPage = cleanHtmlContent(IOUtils.toString(fis, "UTF-8"));
legacyLabel.setText(webPage);
}
}
legacyFrame.pack();
legacyFrame.setVisible(true);
job.setPrintable(this);
printCopies(output, pxlOpts, job, attributes);
}
} catch (Exception e) {
throw new PrinterException(e.getMessage());
} finally {
legacyFrame.dispose();
}
}
use of qz.printer.PrintOptions in project tray by qzind.
the class PrintImage method print.
@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
if (images.isEmpty()) {
log.warn("Nothing to print");
return;
}
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(output.getPrintService());
PageFormat page = job.getPageFormat(null);
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, page, output.getSupportedMedia());
scaleImage = pxlOpts.isScaleContent();
dithering = pxlOpts.getDithering();
interpolation = pxlOpts.getInterpolation();
imageRotation = pxlOpts.getRotation();
// reverse fix for OSX
if (SystemUtilities.isMac() && pxlOpts.getOrientation() != null && pxlOpts.getOrientation().getAsOrientRequested() == OrientationRequested.REVERSE_LANDSCAPE) {
imageRotation += 180;
manualReverse = true;
}
if (!scaleImage) {
// breakup large images to print across pages as needed
List<BufferedImage> split = new ArrayList<>();
for (BufferedImage bi : images) {
split.addAll(breakupOverPages(bi, page));
}
images = split;
}
job.setJobName(pxlOpts.getJobName(Constants.IMAGE_PRINT));
job.setPrintable(this, job.validatePage(page));
printCopies(output, pxlOpts, job, attributes);
}
use of qz.printer.PrintOptions in project tray by qzind.
the class PrintHTML method parseData.
@Override
public void parseData(JSONArray printData, PrintOptions options) throws JSONException, UnsupportedOperationException {
try {
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
if (!pxlOpts.isLegacy()) {
WebApp.initialize();
}
for (int i = 0; i < printData.length(); i++) {
JSONObject data = printData.getJSONObject(i);
PrintingUtilities.Flavor flavor = PrintingUtilities.Flavor.valueOf(data.optString("flavor", "FILE").toUpperCase(Locale.ENGLISH));
String source;
if (flavor == PrintingUtilities.Flavor.BASE64) {
source = new String(Base64.decodeBase64(data.getString("data")), StandardCharsets.UTF_8);
} else {
source = data.getString("data");
}
double pageZoom = (pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch()) / 72.0;
if (pageZoom <= 1) {
pageZoom = 1;
}
double pageWidth = 0;
double pageHeight = 0;
double convertFactor = (72.0 / pxlOpts.getUnits().as1Inch());
boolean renderFromHeight = Arrays.asList(PrintOptions.Orientation.LANDSCAPE, PrintOptions.Orientation.REVERSE_LANDSCAPE).contains(pxlOpts.getOrientation());
if (pxlOpts.getSize() != null) {
if (!renderFromHeight) {
pageWidth = pxlOpts.getSize().getWidth() * convertFactor;
} else {
pageWidth = pxlOpts.getSize().getHeight() * convertFactor;
}
} else if (options.getDefaultOptions().getPageSize() != null) {
if (!renderFromHeight) {
pageWidth = options.getDefaultOptions().getPageSize().getWidth();
} else {
pageWidth = options.getDefaultOptions().getPageSize().getHeight();
}
}
if (pxlOpts.getMargins() != null) {
PrintOptions.Margins margins = pxlOpts.getMargins();
if (!renderFromHeight || pxlOpts.isRasterize()) {
pageWidth -= (margins.left() + margins.right()) * convertFactor;
} else {
// due to vector margin matching
pageWidth -= (margins.top() + margins.bottom()) * convertFactor;
}
}
if (!data.isNull("options")) {
JSONObject dataOpt = data.getJSONObject("options");
if (!dataOpt.isNull("pageWidth") && dataOpt.optDouble("pageWidth") > 0) {
pageWidth = dataOpt.optDouble("pageWidth") * convertFactor;
}
if (!dataOpt.isNull("pageHeight") && dataOpt.optDouble("pageHeight") > 0) {
pageHeight = dataOpt.optDouble("pageHeight") * convertFactor;
}
}
models.add(new WebAppModel(source, (flavor != PrintingUtilities.Flavor.FILE), pageWidth, pageHeight, pxlOpts.isScaleContent(), pageZoom));
}
log.debug("Parsed {} html records", models.size());
} catch (IOException e) {
throw new UnsupportedOperationException("Unable to start JavaFX service", e);
} catch (NoClassDefFoundError e) {
throw new UnsupportedOperationException("JavaFX libraries not found", e);
}
}
use of qz.printer.PrintOptions in project tray by qzind.
the class PrintPDF method print.
@Override
public void print(PrintOutput output, PrintOptions options) throws PrinterException {
if (printables.isEmpty()) {
log.warn("Nothing to print");
return;
}
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintService(output.getPrintService());
PrintOptions.Pixel pxlOpts = options.getPixelOptions();
Scaling scale = (pxlOpts.isScaleContent() ? Scaling.SCALE_TO_FIT : Scaling.ACTUAL_SIZE);
PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, job.getPageFormat(null), (Media[]) output.getPrintService().getSupportedAttributeValues(Media.class, null, null));
// Disable attributes per https://github.com/qzind/tray/issues/174
if (SystemUtilities.isMac() && Constants.JAVA_VERSION.compareWithBuildsTo(Version.valueOf("1.8.0+202")) < 0) {
log.warn("MacOS and Java < 1.8.0u202 cannot use attributes with PDF prints, disabling");
attributes.clear();
}
RenderingHints hints = new RenderingHints(buildRenderingHints(pxlOpts.getDithering(), pxlOpts.getInterpolation()));
double useDensity = pxlOpts.getDensity();
if (!pxlOpts.isRasterize()) {
if (pxlOpts.getDensity() > 0) {
// clear density for vector prints (applied via print attributes instead)
useDensity = 0;
} else if (SystemUtilities.isMac() && Constants.JAVA_VERSION.compareWithBuildsTo(Version.valueOf("1.8.0+121")) < 0) {
log.warn("OSX systems cannot print vector PDF's, forcing raster to prevent crash.");
useDensity = options.getDefaultOptions().getDensity();
}
}
BookBundle bundle = new BookBundle();
for (PDDocument doc : printables) {
PageFormat page = job.getPageFormat(null);
applyDefaultSettings(pxlOpts, page, output.getSupportedMedia());
// trick pdfbox into an alternate doc size if specified
if (docWidth > 0 || docHeight > 0) {
Paper paper = page.getPaper();
if (docWidth <= 0) {
docWidth = page.getImageableWidth();
}
if (docHeight <= 0) {
docHeight = page.getImageableHeight();
}
paper.setImageableArea(paper.getImageableX(), paper.getImageableY(), docWidth, docHeight);
page.setPaper(paper);
// to get custom size we need to force scaling
scale = Scaling.SCALE_TO_FIT;
// pdf uses imageable area from Paper, so this can be safely removed
attributes.remove(MediaPrintableArea.class);
}
for (PDPage pd : doc.getPages()) {
if (pxlOpts.getRotation() % 360 != 0) {
rotatePage(doc, pd, pxlOpts.getRotation());
}
if (pxlOpts.getOrientation() == null) {
PDRectangle bounds = pd.getBBox();
if ((page.getImageableHeight() > page.getImageableWidth() && bounds.getWidth() > bounds.getHeight()) ^ (pd.getRotation() / 90) % 2 == 1) {
log.info("Adjusting orientation to print landscape PDF source");
page.setOrientation(PrintOptions.Orientation.LANDSCAPE.getAsOrientFormat());
}
} else if (pxlOpts.getOrientation() != PrintOptions.Orientation.PORTRAIT) {
// flip imageable area dimensions when in landscape
Paper repap = page.getPaper();
repap.setImageableArea(repap.getImageableX(), repap.getImageableY(), repap.getImageableHeight(), repap.getImageableWidth());
page.setPaper(repap);
// reverse fix for OSX
if (SystemUtilities.isMac() && pxlOpts.getOrientation() == PrintOptions.Orientation.REVERSE_LANDSCAPE) {
pd.setRotation(pd.getRotation() + 180);
}
}
}
bundle.append(new PDFWrapper(doc, scale, false, (float) (useDensity * pxlOpts.getUnits().as1Inch()), false, pxlOpts.getOrientation(), hints), page, doc.getNumberOfPages());
}
if (pxlOpts.getSpoolSize() > 0 && bundle.getNumberOfPages() > pxlOpts.getSpoolSize()) {
int jobNum = 1;
int offset = 0;
while (offset < bundle.getNumberOfPages()) {
job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT) + "-" + jobNum++);
job.setPageable(bundle.wrapAndPresent(offset, pxlOpts.getSpoolSize()));
printCopies(output, pxlOpts, job, attributes);
offset += pxlOpts.getSpoolSize();
}
} else {
job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT));
job.setPageable(bundle.wrapAndPresent());
printCopies(output, pxlOpts, job, attributes);
}
}
Aggregations