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

[macos] print to file doesn't work

XMLWordPrintable

    • 2d
    • x86_64
    • os_x

      FULL PRODUCT VERSION :
      1.8.161 also seen in 1.9.0.4

      ADDITIONAL OS VERSION INFORMATION :
      MACOS 10.12, also seen in earlier versions

      A DESCRIPTION OF THE PROBLEM :
      Print to file does not work, just does regular print.
      works on Windows

      If Print to File is not going to be implemented soon or ever, then the "Print to File" checkbox option should not be shown. The java.awt.print.PrinterJob class method printDialog() does not allow the programmer to customize the Print dlog to hide the "Print to File" option.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run attached program
      click print button
      in the Print dlog, check the "Print to File" checkbox
      click "Print" button
      in Print to File dlog, click "save" button
      In my warning dlog, click "Yes, try anyway" button


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      print to the chosen output file
      ACTUAL -
      prints to the printer

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Graphics;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import java.awt.print.PageFormat;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      import javax.print.attribute.HashPrintRequestAttributeSet;
      import javax.print.attribute.PrintRequestAttributeSet;
      import javax.print.attribute.standard.Destination;
      import javax.swing.*;

      public class testPrint extends JFrame implements ActionListener {

      public testPrint() {
              super("testPrint");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(null);
      setSize(200, 200);

      JButton printButton = new JButton("Print");
      printButton.setLocation(70, 130);
      printButton.setSize(printButton.getPreferredSize());
      printButton.setVisible(true);
      printButton.addActionListener(this);
      add(printButton);

      String JavaVers = "Java version = " + System.getProperty("java.version");
      JTextArea versText = new JTextArea(JavaVers);
      versText.setSize(versText.getPreferredSize());
      versText.setLocation(10, 10);
      add(versText);

      setVisible(true);
          }
       

          public static void main(String[] args) {
              //Schedule a job for the event dispatch thread:
              //creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  @SuppressWarnings("unused")
      public void run() {
                   new testPrint();
                  }
              });
          }

      private static PrinterJob printjob = PrinterJob.getPrinterJob();
      private static PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
      private myPrinter printer = new myPrinter();

      class myPrinter implements Printable {
      public int print(Graphics g, PageFormat pf, int page) {
      if (page > 0)
      return NO_SUCH_PAGE;

      testPrint.this.printAll(g);
      return PAGE_EXISTS;
      }
      } // end class myPrinter

      public void actionPerformed(ActionEvent arg0) {
      if (!printjob.printDialog(attrs))
      return;
      if (attrs.get(Destination.class) != null) {
      // print to file doesn't work on Mac
      Object[] options = {"Yes, try anyway", "No, cancel printing"};
      int n = JOptionPane.showOptionDialog(this,
      "Printing to a file is not supported on the Mac.\nWould you like to try anyway?",
      "Print to file not supported", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
      null, options, options[1]);
      if (n != JOptionPane.YES_OPTION)
      return;
      }
      printjob.setPrintable(printer);
      try {
      printjob.print(attrs);
      } catch (PrinterException pe) {
      JOptionPane.showMessageDialog(this, "Print error" + '\n' + pe.getMessage(),
      "Print error", JOptionPane.ERROR_MESSAGE);
      }
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      My source code has a workaround of warning the user that "Print to File" will fail, but this should not be needed. It would be better if the Print dlog didn't show the "Print to File" option.

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

              Created:
              Updated:
              Resolved: