JDK-8167102 : [macosx] PrintRequestAttributeSet breaks page size set using PageFormat
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 8u102,9
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • Submitted: 2016-10-04
  • Updated: 2018-02-08
  • Resolved: 2017-03-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10 JDK 8 JDK 9
10Fixed 8u131Fixed 9 b166Fixed
Related Reports
Relates :  
Relates :  
JDK 8u102 b14

OS X 10.11

On OS X transferring an instance of "javax.print.attribute.PrintRequestAttributeSet" with at least one any attribute "javax.print.attribute.PrintRequestAttribute" to the method "java.awt.print.PrinterJob.print(PrintRequestAttributeSet)" leads to printing of a page with an incorrect paper size which is different from the expected paper size that was originally set to the printer job via "java.awt.print.PageFormat" instance by the call "java.awt.print.PrinterJob.setPrintable(Printable, PageFormat)". For example, in the test case listed below the printed page size should be exactly 4" x 6", while adding a single "javax.print.attribute.PrintRequestAttribute" leads to a much bigger size of the printed page.

1. Configure a default printer for the system to use either a virtual printer, for example "PDF Printer Lite", "PDFwriter for Mac", or a printer capable of printing a page with 4" x 6" size.
2. Compile and run the listed below test case leaving the line "attributes.add(Chromaticity.MONOCHROME);" commented out.
3. Notice that 4" x 6" page is properly printed.
4. Uncomment the line "attributes.add(Chromaticity.MONOCHROME);", recompile and run the test case.
5. Notice that the printed page size is different from the expected 4'' x 6'' and corresponds to the printer's default page size.

---------- BEGIN SOURCE ----------
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Chromaticity;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class PageSizeBug implements Printable {
    public static void main(String[] args) throws Exception{
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        // Uncomment this next line to break the page's printed size and
        // default to printer's default (usually A4 or US Letter)
        // Re-comment this next line to allow page size to be printed correctly
        // This is not limited to Chromaticity, any new PrintRequestAttribute
        // will break the page's printed size

        //test data
        Paper paper = new Paper();
        paper.setSize(4 * 72, 6 * 72); //4x6in
        paper.setImageableArea(0, 0, 4 * 72, 6 * 72);
        PageFormat pf = new PageFormat();

        PageSizeBug printable = new PageSizeBug();
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(printable, pf);

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
        g.drawRect((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY(),
                (int)pageFormat.getImageableWidth() -1, (int)pageFormat.getImageableHeight() -1);
        return pageIndex == 0? PAGE_EXISTS:NO_SUCH_PAGE;
---------- END SOURCE ----------
Approved for JDK9 as an escalated bug.

Fix Request: - Fix for Release : JDK 9 - Justification : Fix for an OS X specific issue in a basic printing aspect as the paper size occurring in not a rare scenario of Java Print Service API usage. - Risk Analysis : low (affects only OS X platform) - Webrev : http://cr.openjdk.java.net/~alitvinov/8167102/jdk9/webrev.02 - Testing (done/to-be-done) : Manual regression test which is part of the fix "jdk/test/java/awt/print/PageFormat/WrongPaperPrintingTest.java". - Back port (done/to-be-done) : It is necessary to port the fix to JDK 8. - Reviewers : psadhukhan, prr

Debugging showed that the method "RasterPrinterJob.getPageFormatFromAttributes()" returns a new "PageFormat" always, if the provided by the user "PrintRequestAttributeSet" is not empty, and the default values are set to particular instance variables of this "PageFormat", if "PrintRequestAttributeSet" does not contain the searched "OrientationRequested", "MediaSizeName", "MediaPrintableArea" attributes. Although it is clearly stated in Java Platform SE 8 API Specification (documentation of the method "PrinterJob.print(PrintRequestAttributeSet)") Specification URL: http://docs.oracle.com/javase/8/docs/api/java/awt/print/PrinterJob.html#print-javax.print.attribute.PrintRequestAttributeSet- that media size, orientation and imageable area attributes specified in "PrintRequestAttributeSet" supplied to the method "PrinterJob.print(PrintRequestAttributeSet)" will be used for construction of a new "PageFormat", which will be passed to "Printable" object, instead of the original "PageFormat" instance set through "PrinterJob.setPrintable" method, the observed in this issue behavior is a bug, because in the bug test case neither media size, nor orientation, nor imageable area attributes are specified in "PrintRequestAttributeSet". THE POSSIBLE SOLUTION: To change the method "sun.print.RasterPrinterJob.getPageFormatFromAttributes()" in a way which would make it use corresponding values from "PageFormat" instance originally set through "PrinterJob" API during construction of the new "PageFormat", when it is found out that "OrientationRequested", or "MediaSizeName", or "MediaPrintableArea" attribute is not specified in "PrintRequestAttributeSet" supplied to "PrinterJob.print(PrintRequestAttributeSet)" method.

Two PDF documents, which show results of 2 executions of the test case on OS X 10.9.5 with the virtual printer "PDF Printer Lite" demonstrating the correct and incorrect size of the printed page, were attached to the bug report as the next files: 1. "PrintedPageWithCorrectSize.pdf" 2. "PrintedPageWithIncorrectSize.pdf" It was defined that a cause of printing a page of a size different from a size stored in "java.awt.print.PageFormat" instance originally set to printer job through the method "java.awt.print.PrinterJob.setPrintable(Printable, PageFormat)", when nonempty instance of "javax.print.attribute.PrintRequestAttributeSet" is supplied to "PrinterJob.print(PrintRequestAttributeSet)" method, is the fact that in the subsequent calls of the method "PrinterJob.print" the call to "sun.print.RasterPrinterJob.getPageFormatFromAttributes()" returns not "null" value, but a new dynamically created "PageFormat" with a new paper size. Tried to implement 2 workarounds based on information from Java Platform SE 8 API Specification, where the 1st is based on implementing the interface "java.awt.print.Pageable" and the 2nd is based on supplying the required paper size and imageable area through the appropriate instances of "javax.print.attribute.standard.MediaSizeName", "javax.print.attribute.standard.MediaPrintableArea" print service attributes. However, it was practically proven that both workarounds are not workable.

Approved by component triage team to defer

SQE: JDK9: OK to defer

does it affect JDK 8 b132?

Hello Victor. I was able to find only JDK 8 b132 as the latest build of JDK 8 release. And it was defined that the bug does not affect JDK 8 b132, thus it is a regression in some JDK 8 update release.

Hello Victor, Yes, the issue affects JDK 9 also.The bug was reproduced using the virtual printer "PDF Printer Lite" on OS X 10.9.5 with JDK 9 b146, JDK 8u112 b16, JDK 8u102 b14. Thank you, Anton

Does it affect 9?