You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Dominic Eubel <do...@plentymarkets.com> on 2015/10/26 11:54:05 UTC

Problems with scaling and orientation for small pages (pdfbox 2.0.0)

Hey guys,
I hope, it's the right way for my problem :)

We need to print pdfs on very small pages. Like on 40mm x 26mm. (It's a 
Zebra thermal printer)
For testing, I want to print a A4-testpage on this little label.

If I try it with native dialog the a4 page is scaled correctly to 40x26. 
(equal if landscape or portrait with keeping aspect ratio).

Now, if I try to print with Java/pdfbox the page is not scaling correctly.
But if I try to print the a4 page with pdfbox on a a4-printer (landscape 
and portrait), it is scaling fine.

Some information for you:
- latest pdfbox 2.0.0
- MacOsx 10.10/10.11 and Windows
- Java 1.8

Here is source-code :

private static void printOne(PDDocument doc) throws IOException, 
PrinterException { PrinterJob job= PrinterJob.getPrinterJob(); PrintService[] printService= PrinterJob.lookupPrintServices(); for(PrintService printer: printService) { System.out.println("Printer: " + printer.getName()); if(printer.getName().equals("ZEBRA")) { try{ job.setPrintService(printer); break; } catch (PrinterException pe) { pe.printStackTrace(); } } } Paper paper= new Paper(); paper.setSize(40 / (25.4 / 72), 26 / (25.4 / 72)); // It's working with a4 (210x297) 
double pWidth= paper.getWidth(); double pHeight= paper.getHeight(); paper.setImageableArea(0, 0, pWidth, pHeight); PageFormat pageFormat= job.defaultPage(); pageFormat.setPaper(paper); pageFormat.setOrientation(PageFormat.PORTRAIT); // Or PageFormat.LANDSCAPE Book book= new Book(); book.append(new PDFPrintable(doc, Scaling.SCALE_TO_FIT), pageFormat, doc.getNumberOfPages()); job.setPageable(book); job.print(); }

I hope, you can help me :)

Re: Problems with scaling and orientation for small pages (pdfbox 2.0.0)

Posted by John Hewson <jo...@jahewson.com>.
On 26 Oct 2015, at 03:54, Dominic Eubel <do...@plentymarkets.com> wrote:
> 
> Hey guys,
> I hope, it's the right way for my problem :)
> 
> We need to print pdfs on very small pages. Like on 40mm x 26mm. (It's a Zebra thermal printer)
> For testing, I want to print a A4-testpage on this little label.
> 
> If I try it with native dialog the a4 page is scaled correctly to 40x26. (equal if landscape or portrait with keeping aspect ratio).
> 
> Now, if I try to print with Java/pdfbox the page is not scaling correctly.
> But if I try to print the a4 page with pdfbox on a a4-printer (landscape and portrait), it is scaling fine.
> 
> Some information for you:
> - latest pdfbox 2.0.0
> - MacOsx 10.10/10.11 and Windows
> - Java 1.8
> 
> Here is source-code :
> 
> private static void printOne(PDDocument doc) throws IOException, PrinterException { PrinterJob job= PrinterJob.getPrinterJob(); PrintService[] printService= PrinterJob.lookupPrintServices(); for(PrintService printer: printService) { System.out.println("Printer: " + printer.getName()); if(printer.getName().equals("ZEBRA")) { try{ job.setPrintService(printer); break; } catch (PrinterException pe) { pe.printStackTrace(); } } } Paper paper= new Paper(); paper.setSize(40 / (25.4 / 72), 26 / (25.4 / 72)); // It's working with a4 (210x297) double pWidth= paper.getWidth(); double pHeight= paper.getHeight(); paper.setImageableArea(0, 0, pWidth, pHeight); PageFormat pageFormat= job.defaultPage(); pageFormat.setPaper(paper); pageFormat.setOrientation(PageFormat.PORTRAIT); // Or PageFormat.LANDSCAPE Book book= new Book(); book.append(new PDFPrintable(doc, Scaling.SCALE_TO_FIT), pageFormat, doc.getNumberOfPages()); job.setPageable(book); job.print(); }

You shouldn’t be using job.defaultPage(). Follow the printWithPaper example in our Printing example
which can be found in org.apache.pdfbox.examples.printing.

— John

> I hope, you can help me :)


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org