Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8276027

Native print dialog's “Open in Preview” option now prints instead of previewing

XMLWordPrintable

    • 2d
    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      macOS 11.6 (Intel)
      openjdk 17 2021-09-14
      OpenJDK Runtime Environment Temurin-17+35 (build 17+35)
      OpenJDK 64-Bit Server VM Temurin-17+35 (build 17+35, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Starting with Git commit ea7c47c1bb51d079aff44b88f94b3fe5c76fbce5 [1], when using `java.awt.print.PrinterJob` to print on macOS, the native print dialog's “Open in Preview”, “Send in Mail”, “Save to iCloud Drive”, and “Save to Web Receipts” options no longer do what they say. Instead, choosing any of these options sends a print job to the actual printer. “Save as PDF” and “Save as PostScript” still work correctly. The “Open in Preview” option does work correctly in previous versions of OpenJDK.

      I suspect this is happening because of line 440 of `src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m` [2], which resets the `jobDisposition` of the `NSPrintInfo` to `NSPrintSpoolJob`, overriding the user's prior selection (which might be `NSPrintPreviewJob` or some other value).

      [1]: https://github.com/openjdk/jdk/commit/ea7c47c1bb51d079aff44b88f94b3fe5c76fbce5
      [2]: https://github.com/openjdk/jdk/blob/ea7c47c1bb51d079aff44b88f94b3fe5c76fbce5/src/java.desktop/macosx/native/libawt_lwawt/awt/CPrinterJob.m#L440

      REGRESSION : Last worked in version 15

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Use a Mac with a configured printer.
      2. Save the attached source code as `PrintBugDemo.java`.
      3. `javac PrintBugDemo.java`
      4. `java -cp . PrintBugDemo`
      5. In the Print dialog that appears, click “PDF”, then “Open in Preview”.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The Preview application opens, showing a test page.
      ACTUAL -
      In JDK 15 and earlier: The Preview application opens, as expected.
      In JDK 16 and later: The printer prints the test page instead.

      ---------- BEGIN SOURCE ----------
      import javax.print.attribute.HashPrintRequestAttributeSet;
      import javax.print.attribute.standard.DialogTypeSelection;
      import java.awt.EventQueue;
      import java.awt.print.Book;
      import java.awt.print.Printable;
      import java.awt.print.PrinterJob;
      import static java.lang.Math.ceil;

      public class PrintBugDemo {
      public static void main(String[] args) throws Throwable {
      EventQueue.invokeAndWait(() -> {
      try {
      var job = PrinterJob.getPrinterJob();

      var attrs = new HashPrintRequestAttributeSet();
      attrs.add(DialogTypeSelection.NATIVE);

      if (!job.printDialog(attrs))
      return;

      var book = new Book();
      book.append(
      (g, pageFormat, pageIndex) -> {
      g.drawString(
      "Hello, world!",
      (int) ceil(pageFormat.getImageableX()),
      (int) ceil(pageFormat.getImageableY() + g.getFont().getSize2D())
      );
      return Printable.PAGE_EXISTS;
      },
      job.getPageFormat(attrs)
      );
      job.setPageable(book);

      job.print(attrs);
      }
      catch (Exception e) {
      e.printStackTrace();
      }
      });
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


        1. PrintBugDemo.java
          1.0 kB
          Praveen Narayanaswamy

            prr Philip Race
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: