Skip to content
Permalink
Browse files
7179006: [macosx] Print-to-file doesn't work: printing to the default…
… printer instead

Reviewed-by: clanger
Backport-of: ea7c47c
  • Loading branch information
TheRealMDoerr committed Sep 2, 2021
1 parent d41b37f commit 9a5ba0b995e25ebcb0e196c9df4412040df36b2c
Showing 4 changed files with 192 additions and 68 deletions.
@@ -30,13 +30,15 @@
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import java.net.URI;
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.print.*;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Destination;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
@@ -259,6 +261,21 @@ private void setPrintToFile(boolean printToFile) {
isPrintToFile = printToFile;
}

private void setDestinationFile(String dest) {
if (attributes != null && dest != null) {
try {
URI destURI = new URI(dest);
attributes.add(new Destination(destURI));
destinationAttr = "" + destURI.getSchemeSpecificPart();
} catch (Exception e) {
}
}
}

private String getDestinationFile() {
return destinationAttr;
}

@Override
public void print(PrintRequestAttributeSet attributes) throws PrinterException {
// NOTE: Some of this code is copied from RasterPrinterJob.
@@ -359,12 +359,7 @@ static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject d
DECLARE_METHOD(jm_setCollated, sjc_CPrinterJob, "setCollated", "(Z)V");
DECLARE_METHOD(jm_setPageRangeAttribute, sjc_CPrinterJob, "setPageRangeAttribute", "(IIZ)V");
DECLARE_METHOD(jm_setPrintToFile, sjc_CPrinterJob, "setPrintToFile", "(Z)V");

if (src.jobDisposition == NSPrintSaveJob) {
(*env)->CallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, true);
} else {
(*env)->CallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, false);
}
DECLARE_METHOD(jm_setDestinationFile, sjc_CPrinterJob, "setDestinationFile", "(Ljava/lang/String;)V");

// get the selected printer's name, and set the appropriate PrintService on the Java side
NSString *name = [[src printer] name];
@@ -374,6 +369,19 @@ static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject d

NSMutableDictionary* printingDictionary = [src dictionary];

if (src.jobDisposition == NSPrintSaveJob) {
(*env)->CallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, true);
CHECK_EXCEPTION();
NSURL *url = [printingDictionary objectForKey:NSPrintJobSavingURL];
NSString *nsStr = [url absoluteString];
jstring str = NSStringToJavaString(env, nsStr);
(*env)->CallVoidMethod(env, dstPrinterJob, jm_setDestinationFile, str);
CHECK_EXCEPTION();
} else {
(*env)->CallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, false);
CHECK_EXCEPTION();
}

NSNumber* nsCopies = [printingDictionary objectForKey:NSPrintCopies];
if ([nsCopies respondsToSelector:@selector(integerValue)])
{
@@ -429,6 +437,7 @@ static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobj
DECLARE_METHOD(jm_getSelectAttrib, sjc_CPrinterJob, "getSelectAttrib", "()I");
DECLARE_METHOD(jm_getNumberOfPages, jc_Pageable, "getNumberOfPages", "()I");
DECLARE_METHOD(jm_getPageFormat, sjc_CPrinterJob, "getPageFormatFromAttributes", "()Ljava/awt/print/PageFormat;");
DECLARE_METHOD(jm_getDestinationFile, sjc_CPrinterJob, "getDestinationFile", "()Ljava/lang/String;");

NSMutableDictionary* printingDictionary = [dst dictionary];

@@ -476,6 +485,17 @@ static void javaPrinterJobToNSPrintInfo(JNIEnv* env, jobject srcPrinterJob, jobj
if (page != NULL) {
javaPageFormatToNSPrintInfo(env, NULL, page, dst);
}

jstring dest = (*env)->CallObjectMethod(env, srcPrinterJob, jm_getDestinationFile);
CHECK_EXCEPTION();
if (dest != NULL) {
[dst setJobDisposition:NSPrintSaveJob];
NSString *nsDestStr = JavaStringToNSString(env, dest);
NSURL *nsURL = [NSURL fileURLWithPath:nsDestStr isDirectory:NO];
[printingDictionary setObject:nsURL forKey:NSPrintJobSavingURL];
} else {
[dst setJobDisposition:NSPrintSpoolJob];
}
}

/*
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,76 +23,86 @@

/*
@test
@bug 4865976 7158366
@summary Pass if it program exits.
@key printer
@bug 4865976 7158366 7179006
@summary Pass if program exits.
@run main/manual PrintDlgApp
*/
import java.awt.*;
import java.awt.print.*;
import javax.print.attribute.*;

import java.io.File;
import java.net.URI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterException;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Destination;
import java.util.Locale;

import javax.print.*;
public class PrintDlgApp implements Printable {

class PrintDlgApp implements Printable {
/**
* Constructor
*/
public PrintDlgApp() {
super();
}
/**
* Starts the application.
*/
public static void main(java.lang.String[] args) {
PrintDlgApp pd = new PrintDlgApp();
PrinterJob pj = PrinterJob.getPrinterJob();
System.out.println(pj);
PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet();
pSet.add(new Copies(1));
//PageFormat pf = pj.pageDialog(pSet);
PageFormat pf = new PageFormat();
System.out.println("Setting Printable...pf = "+pf);
if (pf == null) {
return;
}
pj.setPrintable(pd,pf);
public PrintDlgApp() {}

//try { pj.setPrintService(services[0]); } catch(Exception e) { e.printStackTrace(); }
pSet.add(new Destination(new java.io.File("./out.prn").toURI()));
System.out.println("open PrintDialog..");
for (int i=0; i<2; i++) {
if (pj.printDialog(pSet)) {
try {
System.out.println("About to print the data ...");
pj.print(pSet);
System.out.println("Printed");
}
catch (PrinterException pe) {
pe.printStackTrace();
}
}
}
public static void main(String[] args) {
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.getPrintService() == null) {
System.out.println("No printers installed. Skipping test");
return;
}

}
PrintDlgApp pd = new PrintDlgApp();
PageFormat pf = new PageFormat();
pj.setPrintable(pd, pf);

PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet();
pSet.add(new Copies(1));

Destination dest = null;
for (int i=0; i<2; i++) {
File file = new File("./out"+i+".prn");
dest = new Destination(file.toURI());
pSet.add(dest);
System.out.println("open PrintDialog.");
if (pj.printDialog(pSet)) {
// In case tester changes the destination :
dest = (Destination)pSet.get(Destination.class);
System.out.println("DEST="+dest);
if (dest != null) {
URI uri = dest.getURI();
file = new File(uri.getSchemeSpecificPart());
System.out.println("will be checking for file " + file);
}
try {
System.out.println("About to print the data ...");
pj.print(pSet);
System.out.println("Printed.");
}
catch (PrinterException pe) {
pe.printStackTrace();
}
}
if (dest != null && !file.exists()) {
throw new RuntimeException("No file created");
}
}
}

//printable interface
public int print(Graphics g, PageFormat pf, int pi) throws
PrinterException {
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {

if (pi > 0) {
System.out.println("pi is greater than 0");
return Printable.NO_SUCH_PAGE;
}
// Simply draw two rectangles
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawRect(1,1,200,300);
g2.drawRect(1,1,25,25);
System.out.println("print method called "+pi);
return Printable.PAGE_EXISTS;
if (pi > 0) {
return Printable.NO_SUCH_PAGE;
}
// Simply draw two rectangles
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawRect(1,1,200,300);
g2.drawRect(1,1,25,25);
System.out.println("print method called "+pi);
return Printable.PAGE_EXISTS;
}
}
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @key printer
* @bug 7179006
* @summary Confirm printing to file works.
*/

import java.io.File;
import java.net.URI;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PrinterException;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Destination;

public class PrintToFileTest implements Printable {

public PrintToFileTest() {}

public static void main(String[] args) throws Exception {
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.getPrintService() == null) {
System.out.println("No printers installed. Skipping test.");
return;
}
pj.setPrintable(new PrintToFileTest(), new PageFormat());
PrintRequestAttributeSet pSet = new HashPrintRequestAttributeSet();
File file = new File("./out.prn");
pSet.add(new Destination(file.toURI()));
pj.print(pSet);
if (!file.exists()) {
throw new RuntimeException("No file created");
}
}

public int print(Graphics g, PageFormat pf, int pi) throws
PrinterException {

if (pi > 0) {
return Printable.NO_SUCH_PAGE;
}
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.drawRect(1,1,200,300);
g2.drawRect(1,1,25,25);
return Printable.PAGE_EXISTS;
}
}

1 comment on commit 9a5ba0b

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 9a5ba0b Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.