Hi,

We have an web application in that, We are generating an PDF report upon cliking the "Print" Button, using Oracle Application Server - Business Intelligent service. Upon generation , the system should pop-up the print dialog box on the client side to print the PDF report..

I have tried by using a tool(xpdf) mentioned by one of the gentelmen in one of the print forum, which basically converts PDF document to Postscript document (ps) and that could be read using the normal Print API of the JSDK1.4 and be printed. The specific tool in the xpdf package is pdftops.exe.

The tool could be downloaded from the following URL :
http://www.foolabs.com/xpdf/download.html

I used the pdftops.exe file to convert the pdf document into PS and printed it, using the Runtime java API class.

Here is complete source code.

package javaprint;

import javax.print.*;
import javax.print.attribute.*;

import java.io.*;
import java.util.*;

/**
* Title: PrintPdf.java
* Description: This class converts a PDF format document into PostScript (ps) and prints the same.
* Copyright: Copyright (c) 2003
* Company: Misys Healthcare Systems
* @author Madhusudan M Krishnamurthy
* @e-mail madhusudan dot krishnamurthy at misyshealthcare dot com
* @version 1.0
*/

public class PrintPdf
{
/* Input PDF file name */
private String inputFileName;

/* Autogenerated output PS file name */
private String outputFileName;

/* Path from where the pdftops tool can be accessed */
private static final String PDF_TO_PS_TOOL_PATH = "C:
pdftops";

/* Thread class which clean up accumulates .ps file every 1 hour. */
private CleanUpThread cleanUpThread;

/**
* Default Constructor.
*/
public PrintPdf()
{
cleanUpThread = new CleanUpThread();
}

/**
* Constructor, the PDF file to be printed should be passed as parameter to this.
*/
public PrintPdf(String inputFileName)
{
this.inputFileName = inputFileName;
cleanUpThread = new CleanUpThread();
}

/**
* Sets the inputFileName property value
* @param inputFileName
*/
public void setInputFileName(String inputFileName)
{
this.inputFileName = inputFileName;
}

/**
* Accessor method to obtain the value of inputFileName property value.
* @return java.lang.String (inputFileName).
*/
public String getInputFileName()
{
return this.inputFileName;
}

/**
* Sets the outputFileName property value.
* This method sets the generated PostScript file name that is generated for the input PDF file name.
* @param outputFileName
*/
private void setOuputFileName(String outputFileName)
{
this.outputFileName = outputFileName;
}


/**
* Accessor method to obtain the outputFileName name.
* @return outputFileName
*/
public String getOutputFileName()
{
return this.outputFileName;
}

/**
* Core logic to print the input document.
*/
public void printDocument()
{
try
{
generateOuputFileName();
convertPdfToPostScript();

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService printPrintService = null;
PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras);
if (service != null)
{
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(getOutputFileName());
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* This method generates the post script output file name. The method obtains the path and PDF fileName and generates
* the post script file name with the same name as PDF file under the same path.
*/
protected void generateOuputFileName()
{
try
{
File file = new File(getInputFileName()); //Create a File Object from the give PDF file.
String filename = file.getName();
String parent = file.getParent();
StringTokenizer fileNameTokenizer = new StringTokenizer(filename, ".");
filename = fileNameTokenizer.nextToken(); //Extrace the file name except the extension. ex: if file name is 'xxx.pdf' this code will return 'xxx'

// this will result in a value as, "C:\" + "\" + "xxx" + "1028393030" + ".ps"; The file name generated is concatinated with current date
// time to make it unique, may be there will be situation where two clients may be generating the print output to the same PDF file.
filename = parent + filename + ((new Date()).getTime()) + ".ps";
setOuputFileName(filename);
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* This method convets the given PDF file to PS file
* This method uses the pdftopdf.exe tool to pass the input pdf file name and output ps file name to
* generate the Postscript version of the input PDF file.
*/
protected void convertPdfToPostScript()
{
try
{
Runtime runTime = Runtime.getRuntime();
Process process = null;
String command = PDF_TO_PS_TOOL_PATH + " " + getInputFileName() + " " + getOutputFileName();
process = runTime.exec(command);
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* This method cleans up the generated post script document so that all the generated PS documents
* clog the hard drive space.
*/
protected synchronized void cleanUpPSDocument()
{
try
{
File file = new File(getOutputFileName());
file = new File(file.getParent());
File[] fileList = file.listFiles(
new FilenameFilter ()
{
public boolean accept(File dir, String name)
{
if (name.endsWith(".ps"))
return true;
else
return false;
}
}
);
if (fileList != null)
{
for (int i = 0; i < fileList.length; i++)
{
long lastModified = fileList.lastModified();
if ((lastModified / (60 * 60 * 1000)) > (60 * 60 * 1000)) // If the file is more than ONE hour old then delete it.
fileList.delete();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

/**
* Title:
* Description: This private inner class is used to clean up the converted ps file. Over a period of time <br>
* the files PS file gets accumulated and clogs the hard disk space hence every one hour clean the files.
* Copyright: Copyright (c) 2003
* Company: Misys Healthcare Systems
* @author Madhusudan M Krishnamurthy
* @e-mail madhusudan dot krishnamurthy at misyshealthcare dot com
* @version 1.0
*/
private class CleanUpThread extends Thread
{
public CleanUpThread()
{
start();
}

public void run()
{
while(true)
{
cleanUpPSDocument();
try
{
sleep(60 * 60 * 1000); //Sleep for ONE hour
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}

/**
* Test Harness
*/
public static void main(String args[]) //throws Exception
{
PrintPdf printPdf = new PrintPdf("C:
IBMJavaPrint.pdf");
printPdf.printDocument();
}
}


This code pop-up the print dialog box, but its printing the postscript instead of PDF itself. i think this will only work for Postscript-enabled printers.


Could any one tell me the way to print the PDF directly or by some other methods....

regards,
MohdJaleel.