You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Timo Rumland <ti...@digitalys.de> on 2016/04/27 12:22:35 UTC

Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4

Hello,

after migrating from PDFBox 1.8.2 to 2.0.0, I have to use
'MediaSizeName.ISO_A4' in combination with 'java.awt.print.PrinterJob' to
print a PDF document correctly in DIN A4.

Please, see the code below:

----

PDDocument pdDocument = PDDocument.load( new FileInputStream(
"pdf_din-a4.pdf" ) );

PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setJobName( "PDFBox is a great library" );
printerJob.setPrintService( PrintServiceLookup.lookupDefaultPrintService()
);
printerJob.setPrintable( new PDFPrintable( pdDocument ) );
printerJob.print();

----

 - In my case, the source PDF file (loaded by 'PDDocument.load(...)') is in
DIN A4 format.
 - Furthermore, the default printer on my dev system (Window 7 64 Bit) in
PDFCreator.


Using PDFBox 1.8.2, the output PDF file has the correct size of DIN A4.

Using PDFBox 2.0.0, the output PDF file is in DIN A4, but the contents of
the PDF is smaller, probably DIN A5 (so, the contents is scaled down and the
resulting PDF file has large white areas around the contents).

To get the same result as with PDFBox 1.8.2 using PDFBox 2.0.0, I have to
specify the media size like this:


----

// Code like above without 'printerJob.print();'

PrintRequestAttributeSet printRequestAttributeSet = new
HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 );
printerJob.print( printRequestAttributeSet );

----


Now, while this works for me, there are several questions like:

 - Can this be considered a bug in 2.0.0, or maybe there was a bug in 1.8.2?
 - Should this be mentioned in the migration guide?
 - Or am I doing something wrong here?


Thanks a lot!


Best Regards,
Timo




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


AW: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4

Posted by Timo Rumland <ti...@digitalys.de>.
Hi John,

> Yes, there’s a two-argument constructor for PDFPageable which allows 
> the Scaling to be specified.What you’re after is ACTUAL_SIZE. The 
> relvant JavaDoc is here:
> [...]

great, thanks again!



Best Regards,
Timo



-----Ursprüngliche Nachricht-----
Von: John Hewson [mailto:john@jahewson.com] 
Gesendet: Donnerstag, 28. April 2016 19:48
An: users@pdfbox.apache.org
Betreff: Re: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4


> On 28 Apr 2016, at 01:07, Timo Rumland <ti...@digitalys.de> wrote:
> 
> Hello John,
> 
> thank you for the explanation!
> 
>> By default PDFBox 2.0 scales down PDFs to fit the printable area of 
>> the PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 
>> which tried to do something like this but actually ended up setting 
>> the printable area of the Paper, possibly even to a size large than 
>> the physical paper (oops).
> 
> 
> is this behavior configurable (I would like to be able to choose no scaling, even if that means some areas of the output are truncated)? Maybe I overlooked something in the PDFBox documentation.

Yes, there’s a two-argument constructor for PDFPageable which allows the Scaling to be specified.What you’re after is ACTUAL_SIZE. The relvant JavaDoc is here:

https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/printing/PDFPrintable.html#PDFPrintable(org.apache.pdfbox.pdmodel.PDDocument,%20org.apache.pdfbox.printing.Scaling)

— John

> 
> Thanks
> 
> 
> Best Regards,
> Timo
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: John Hewson [mailto:john@jahewson.com]
> Gesendet: Donnerstag, 28. April 2016 00:03
> An: users@pdfbox.apache.org
> Betreff: Re: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 
> must be set to print in DIN A4
> 
> 
>> On 27 Apr 2016, at 03:22, Timo Rumland <ti...@digitalys.de> wrote:
>> 
>> Hello,
>> 
>> after migrating from PDFBox 1.8.2 to 2.0.0, I have to use 
>> 'MediaSizeName.ISO_A4' in combination with 'java.awt.print.PrinterJob'
>> to print a PDF document correctly in DIN A4.
>> 
>> Please, see the code below:
>> 
>> ----
>> 
>> PDDocument pdDocument = PDDocument.load( new FileInputStream( 
>> "pdf_din-a4.pdf" ) );
>> 
>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>> printerJob.setJobName( "PDFBox is a great library" ); 
>> printerJob.setPrintService(
>> PrintServiceLookup.lookupDefaultPrintService()
>> );
>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>> printerJob.print();
>> 
>> ----
>> 
>> - In my case, the source PDF file (loaded by 'PDDocument.load(...)') 
>> is in DIN A4 format.
>> - Furthermore, the default printer on my dev system (Window 7 64 Bit) 
>> in PDFCreator.
>> 
>> 
>> Using PDFBox 1.8.2, the output PDF file has the correct size of DIN A4.
>> 
>> Using PDFBox 2.0.0, the output PDF file is in DIN A4, but the 
>> contents of the PDF is smaller, probably DIN A5 (so, the contents is 
>> scaled down and the resulting PDF file has large white areas around the contents).
> 
> By default PDFBox 2.0 scales down PDFs to fit the printable area of the PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 which tried to do something like this but actually ended up setting the printable area of the Paper, possibly even to a size large than the physical paper (oops).
> 
> So PDFBox 2.0 will scale down the PDF to match the imageable area of the job’s paper. Take a look at:
> 
> printerJob.defaultPage()
> 
> getWidth() and getHeight() will tell you about the physical paper, while the getImageableXYZ() methods will tell you about the margins. Do these values match what you expect? It’s possible that your default paper is not A4, or that MediaSizeName.ISO_A4 somehow results in different margins.
> 
>> 
>> To get the same result as with PDFBox 1.8.2 using PDFBox 2.0.0, I 
>> have to specify the media size like this:
>> 
>> 
>> ----
>> 
>> // Code like above without 'printerJob.print();'
>> 
>> PrintRequestAttributeSet printRequestAttributeSet = new 
>> HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 ); 
>> printerJob.print( printRequestAttributeSet );
>> 
>> ----
>> 
>> 
>> Now, while this works for me, there are several questions like:
>> 
>> - Can this be considered a bug in 2.0.0, or maybe there was a bug in 1.8.2?
> 
> 1.8.2 was definitely buggy. 2.0.0 could be, but it’s usually just Java => print driver interaction.
> 
>> - Should this be mentioned in the migration guide?
> 
> Probably, once we get to the bottom of it.
> 
>> - Or am I doing something wrong here?
> 
> Maybe, but the Java printing API is just horrible.
> 
> — John
> 
>> Thanks a lot!
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


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



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


Re: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4

Posted by John Hewson <jo...@jahewson.com>.
> On 28 Apr 2016, at 01:07, Timo Rumland <ti...@digitalys.de> wrote:
> 
> Hello John,
> 
> thank you for the explanation!
> 
>> By default PDFBox 2.0 scales down PDFs to fit the printable area of 
>> the PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 
>> which tried to do something like this but actually ended up setting 
>> the printable area of the Paper, possibly even to a size large than 
>> the physical paper (oops).
> 
> 
> is this behavior configurable (I would like to be able to choose no scaling, even if that means some areas of the output are truncated)? Maybe I overlooked something in the PDFBox documentation.

Yes, there’s a two-argument constructor for PDFPageable which allows the Scaling to be specified.What you’re after is ACTUAL_SIZE. The relvant JavaDoc is here:

https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/printing/PDFPrintable.html#PDFPrintable(org.apache.pdfbox.pdmodel.PDDocument,%20org.apache.pdfbox.printing.Scaling)

— John

> 
> Thanks
> 
> 
> Best Regards,
> Timo
> 
> 
> -----Ursprüngliche Nachricht-----
> Von: John Hewson [mailto:john@jahewson.com] 
> Gesendet: Donnerstag, 28. April 2016 00:03
> An: users@pdfbox.apache.org
> Betreff: Re: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4
> 
> 
>> On 27 Apr 2016, at 03:22, Timo Rumland <ti...@digitalys.de> wrote:
>> 
>> Hello,
>> 
>> after migrating from PDFBox 1.8.2 to 2.0.0, I have to use 
>> 'MediaSizeName.ISO_A4' in combination with 'java.awt.print.PrinterJob' 
>> to print a PDF document correctly in DIN A4.
>> 
>> Please, see the code below:
>> 
>> ----
>> 
>> PDDocument pdDocument = PDDocument.load( new FileInputStream( 
>> "pdf_din-a4.pdf" ) );
>> 
>> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
>> printerJob.setJobName( "PDFBox is a great library" ); 
>> printerJob.setPrintService( 
>> PrintServiceLookup.lookupDefaultPrintService()
>> );
>> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
>> printerJob.print();
>> 
>> ----
>> 
>> - In my case, the source PDF file (loaded by 'PDDocument.load(...)') 
>> is in DIN A4 format.
>> - Furthermore, the default printer on my dev system (Window 7 64 Bit) 
>> in PDFCreator.
>> 
>> 
>> Using PDFBox 1.8.2, the output PDF file has the correct size of DIN A4.
>> 
>> Using PDFBox 2.0.0, the output PDF file is in DIN A4, but the contents 
>> of the PDF is smaller, probably DIN A5 (so, the contents is scaled 
>> down and the resulting PDF file has large white areas around the contents).
> 
> By default PDFBox 2.0 scales down PDFs to fit the printable area of the PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 which tried to do something like this but actually ended up setting the printable area of the Paper, possibly even to a size large than the physical paper (oops).
> 
> So PDFBox 2.0 will scale down the PDF to match the imageable area of the job’s paper. Take a look at:
> 
> printerJob.defaultPage()
> 
> getWidth() and getHeight() will tell you about the physical paper, while the getImageableXYZ() methods will tell you about the margins. Do these values match what you expect? It’s possible that your default paper is not A4, or that MediaSizeName.ISO_A4 somehow results in different margins.
> 
>> 
>> To get the same result as with PDFBox 1.8.2 using PDFBox 2.0.0, I have 
>> to specify the media size like this:
>> 
>> 
>> ----
>> 
>> // Code like above without 'printerJob.print();'
>> 
>> PrintRequestAttributeSet printRequestAttributeSet = new 
>> HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 ); 
>> printerJob.print( printRequestAttributeSet );
>> 
>> ----
>> 
>> 
>> Now, while this works for me, there are several questions like:
>> 
>> - Can this be considered a bug in 2.0.0, or maybe there was a bug in 1.8.2?
> 
> 1.8.2 was definitely buggy. 2.0.0 could be, but it’s usually just Java => print driver interaction.
> 
>> - Should this be mentioned in the migration guide?
> 
> Probably, once we get to the bottom of it.
> 
>> - Or am I doing something wrong here?
> 
> Maybe, but the Java printing API is just horrible.
> 
> — John
> 
>> Thanks a lot!
>> 
>> 
>> Best Regards,
>> Timo
>> 
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


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


AW: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4

Posted by Timo Rumland <ti...@digitalys.de>.
Hello John,

thank you for the explanation!

> By default PDFBox 2.0 scales down PDFs to fit the printable area of 
> the PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 
> which tried to do something like this but actually ended up setting 
> the printable area of the Paper, possibly even to a size large than 
> the physical paper (oops).


is this behavior configurable (I would like to be able to choose no scaling, even if that means some areas of the output are truncated)? Maybe I overlooked something in the PDFBox documentation.


Thanks


Best Regards,
Timo


-----Ursprüngliche Nachricht-----
Von: John Hewson [mailto:john@jahewson.com] 
Gesendet: Donnerstag, 28. April 2016 00:03
An: users@pdfbox.apache.org
Betreff: Re: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4


> On 27 Apr 2016, at 03:22, Timo Rumland <ti...@digitalys.de> wrote:
> 
> Hello,
> 
> after migrating from PDFBox 1.8.2 to 2.0.0, I have to use 
> 'MediaSizeName.ISO_A4' in combination with 'java.awt.print.PrinterJob' 
> to print a PDF document correctly in DIN A4.
> 
> Please, see the code below:
> 
> ----
> 
> PDDocument pdDocument = PDDocument.load( new FileInputStream( 
> "pdf_din-a4.pdf" ) );
> 
> PrinterJob printerJob = PrinterJob.getPrinterJob(); 
> printerJob.setJobName( "PDFBox is a great library" ); 
> printerJob.setPrintService( 
> PrintServiceLookup.lookupDefaultPrintService()
> );
> printerJob.setPrintable( new PDFPrintable( pdDocument ) ); 
> printerJob.print();
> 
> ----
> 
> - In my case, the source PDF file (loaded by 'PDDocument.load(...)') 
> is in DIN A4 format.
> - Furthermore, the default printer on my dev system (Window 7 64 Bit) 
> in PDFCreator.
> 
> 
> Using PDFBox 1.8.2, the output PDF file has the correct size of DIN A4.
> 
> Using PDFBox 2.0.0, the output PDF file is in DIN A4, but the contents 
> of the PDF is smaller, probably DIN A5 (so, the contents is scaled 
> down and the resulting PDF file has large white areas around the contents).

By default PDFBox 2.0 scales down PDFs to fit the printable area of the PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 which tried to do something like this but actually ended up setting the printable area of the Paper, possibly even to a size large than the physical paper (oops).

So PDFBox 2.0 will scale down the PDF to match the imageable area of the job’s paper. Take a look at:

printerJob.defaultPage()

getWidth() and getHeight() will tell you about the physical paper, while the getImageableXYZ() methods will tell you about the margins. Do these values match what you expect? It’s possible that your default paper is not A4, or that MediaSizeName.ISO_A4 somehow results in different margins.

> 
> To get the same result as with PDFBox 1.8.2 using PDFBox 2.0.0, I have 
> to specify the media size like this:
> 
> 
> ----
> 
> // Code like above without 'printerJob.print();'
> 
> PrintRequestAttributeSet printRequestAttributeSet = new 
> HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 ); 
> printerJob.print( printRequestAttributeSet );
> 
> ----
> 
> 
> Now, while this works for me, there are several questions like:
> 
> - Can this be considered a bug in 2.0.0, or maybe there was a bug in 1.8.2?

1.8.2 was definitely buggy. 2.0.0 could be, but it’s usually just Java => print driver interaction.

> - Should this be mentioned in the migration guide?

Probably, once we get to the bottom of it.

> - Or am I doing something wrong here?

Maybe, but the Java printing API is just horrible.

— John

> Thanks a lot!
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


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



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


Re: Using PDFBox 2.0.0 with PrinterJob: MediaSizeName.ISO_A4 must be set to print in DIN A4

Posted by John Hewson <jo...@jahewson.com>.
> On 27 Apr 2016, at 03:22, Timo Rumland <ti...@digitalys.de> wrote:
> 
> Hello,
> 
> after migrating from PDFBox 1.8.2 to 2.0.0, I have to use
> 'MediaSizeName.ISO_A4' in combination with 'java.awt.print.PrinterJob' to
> print a PDF document correctly in DIN A4.
> 
> Please, see the code below:
> 
> ----
> 
> PDDocument pdDocument = PDDocument.load( new FileInputStream(
> "pdf_din-a4.pdf" ) );
> 
> PrinterJob printerJob = PrinterJob.getPrinterJob();
> printerJob.setJobName( "PDFBox is a great library" );
> printerJob.setPrintService( PrintServiceLookup.lookupDefaultPrintService()
> );
> printerJob.setPrintable( new PDFPrintable( pdDocument ) );
> printerJob.print();
> 
> ----
> 
> - In my case, the source PDF file (loaded by 'PDDocument.load(...)') is in
> DIN A4 format.
> - Furthermore, the default printer on my dev system (Window 7 64 Bit) in
> PDFCreator.
> 
> 
> Using PDFBox 1.8.2, the output PDF file has the correct size of DIN A4.
> 
> Using PDFBox 2.0.0, the output PDF file is in DIN A4, but the contents of
> the PDF is smaller, probably DIN A5 (so, the contents is scaled down and the
> resulting PDF file has large white areas around the contents).

By default PDFBox 2.0 scales down PDFs to fit the printable area of the
PrinterJob’s Paper. This differs form the behaviour of PDFBox 1.8 which tried
to do something like this but actually ended up setting the printable area
of the Paper, possibly even to a size large than the physical paper (oops).

So PDFBox 2.0 will scale down the PDF to match the imageable area of
the job’s paper. Take a look at:

printerJob.defaultPage()

getWidth() and getHeight() will tell you about the physical paper, while
the getImageableXYZ() methods will tell you about the margins. Do these
values match what you expect? It’s possible that your default paper is not
A4, or that MediaSizeName.ISO_A4 somehow results in different margins.

> 
> To get the same result as with PDFBox 1.8.2 using PDFBox 2.0.0, I have to
> specify the media size like this:
> 
> 
> ----
> 
> // Code like above without 'printerJob.print();'
> 
> PrintRequestAttributeSet printRequestAttributeSet = new
> HashPrintRequestAttributeSet( MediaSizeName.ISO_A4 );
> printerJob.print( printRequestAttributeSet );
> 
> ----
> 
> 
> Now, while this works for me, there are several questions like:
> 
> - Can this be considered a bug in 2.0.0, or maybe there was a bug in 1.8.2?

1.8.2 was definitely buggy. 2.0.0 could be, but it’s usually just Java => print driver interaction.

> - Should this be mentioned in the migration guide?

Probably, once we get to the bottom of it.

> - Or am I doing something wrong here?

Maybe, but the Java printing API is just horrible.

— John

> Thanks a lot!
> 
> 
> Best Regards,
> Timo
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


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