Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7179006: [macosx] Print-to-file doesn't work: printing to the default printer instead. #339

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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;
@@ -251,6 +253,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();

This comment has been minimized.

@mrserb

mrserb Sep 25, 2020
Member

This destinationAttr in the RasterPrinterJob is usually assigned to:
destinationAttr = "" + new File(destination.getURI().
getSchemeSpecificPart());
Do we need to do the same here?

This comment has been minimized.

@prrace

prrace Sep 25, 2020
Author Contributor

I see that in WPrinterJob.java we do the same as I am doing here.
I think the difference in RPJ may bethat we want to get a full path name which is what the new File did.
Here macOS gives us a full path name already. So I don't think it is needed.
It gave the required value for sure.

} 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.
@@ -316,21 +316,25 @@ static void nsPrintInfoToJavaPrinterJob(JNIEnv* env, NSPrintInfo* src, jobject d
static JNF_MEMBER_CACHE(jm_setCollated, sjc_CPrinterJob, "setCollated", "(Z)V");
static JNF_MEMBER_CACHE(jm_setPageRangeAttribute, sjc_CPrinterJob, "setPageRangeAttribute", "(IIZ)V");
static JNF_MEMBER_CACHE(jm_setPrintToFile, sjc_CPrinterJob, "setPrintToFile", "(Z)V");

if (src.jobDisposition == NSPrintSaveJob) {
JNFCallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, true);
} else {
JNFCallVoidMethod(env, dstPrinterJob, jm_setPrintToFile, false);
}
static JNF_MEMBER_CACHE(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];
jstring printerName = JNFNSToJavaString(env, name);
JNFCallVoidMethod(env, dstPrinterJob, jm_setService, printerName);


NSMutableDictionary* printingDictionary = [src dictionary];

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

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

NSMutableDictionary* printingDictionary = [dst dictionary];

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

jstring dest = JNFCallObjectMethod(env, srcPrinterJob, jm_getDestinationFile);
if (dest != NULL) {
[dst setJobDisposition:NSPrintSaveJob];
NSString *nsDestStr = JNFJavaToNSString(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");
}

This comment has been minimized.

@prsadhuk

prsadhuk Sep 25, 2020
Contributor

I guess we need to delete the file in finally block, if created.

This comment has been minimized.

@prrace

prrace Sep 25, 2020
Author Contributor

I meant to leave it. So we can look at it if we need to.

}

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;
}
}