Search in sources :

Example 1 with Scaling

use of org.apache.pdfbox.printing.Scaling 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();
    PrintRequestAttributeSet attributes = applyDefaultSettings(pxlOpts, job.getPageFormat(null));
    // Disable attributes per https://github.com/qzind/tray/issues/174
    if (SystemUtilities.isMac() && Constants.JAVA_VERSION.lessThan(Version.valueOf("1.8.0-162"))) {
        log.warn("MacOS and Java < 8u162 cannot use attributes with PDF prints, disabling");
        attributes.clear();
    }
    Scaling scale = (pxlOpts.isScaleContent() ? Scaling.SCALE_TO_FIT : Scaling.ACTUAL_SIZE);
    BookBundle bundle = new BookBundle();
    for (PDDocument doc : printables) {
        PageFormat page = job.getPageFormat(null);
        applyDefaultSettings(pxlOpts, page);
        for (PDPage pd : doc.getPages()) {
            if (pxlOpts.getRotation() % 360 != 0) {
                rotatePage(doc, pd, pxlOpts.getRotation());
            }
            if (pxlOpts.getOrientation() == null) {
                PDRectangle bounds = pd.getBBox();
                if (bounds.getWidth() > bounds.getHeight() || (pd.getRotation() / 90) % 2 == 1) {
                    log.info("Adjusting orientation to print landscape PDF source");
                    page.setOrientation(PrintOptions.Orientation.LANDSCAPE.getAsFormat());
                }
            } 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) (pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch()), false, pxlOpts.getOrientation()), page, doc.getNumberOfPages());
    }
    job.setJobName(pxlOpts.getJobName(Constants.PDF_PRINT));
    job.setPageable(bundle.wrapAndPresent());
    printCopies(output, pxlOpts, job, attributes);
}
Also used : PageFormat(java.awt.print.PageFormat) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDFWrapper(qz.printer.PDFWrapper) BookBundle(qz.printer.BookBundle) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PrintOptions(qz.printer.PrintOptions) Scaling(org.apache.pdfbox.printing.Scaling) Paper(java.awt.print.Paper) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PrinterJob(java.awt.print.PrinterJob) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet)

Aggregations

PageFormat (java.awt.print.PageFormat)1 Paper (java.awt.print.Paper)1 PrinterJob (java.awt.print.PrinterJob)1 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 Scaling (org.apache.pdfbox.printing.Scaling)1 BookBundle (qz.printer.BookBundle)1 PDFWrapper (qz.printer.PDFWrapper)1 PrintOptions (qz.printer.PrintOptions)1