This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Tomcat and the fly likes Problem in printing a pdf file using pdfrenderer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Tomcat
Bookmark "Problem in printing a pdf file using pdfrenderer" Watch "Problem in printing a pdf file using pdfrenderer" New topic
Author

Problem in printing a pdf file using pdfrenderer

Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
Hi,

I have written a piece of code using PDFrenderer.jar api to print a pdf file, it worked perfectly on a windows system but it is failing in linux. I am using java 1.6 and tomcat 6.0. I am attaching the log so can anyone please help me out on this?


90a1517b 06:55:27 [http-8080-5] ERROR org.openbravo.localization.us.checkprinting.ad_forms.CheckPrintingCategory - Error captured:
java.awt.HeadlessException
at sun.print.PSPrinterJob.printDialog(PSPrinterJob.java:401)
at org.openbravo.localization.us.checkprinting.ad_forms.PrinterTesting.print(PrinterTesting.java:109)
at org.openbravo.localization.us.checkprinting.ad_forms.PrinterTesting.print(PrinterTesting.java:37)
at org.openbravo.localization.us.checkprinting.ad_forms.CheckPrintingCategory.doPost(CheckPrintingCategory.java:233)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.openbravo.base.HttpBaseServlet.serviceInitialized(HttpBaseServlet.java:225)
at org.openbravo.base.secureApp.HttpSecureAppServlet.service(HttpSecureAppServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at sun.reflect.GeneratedMethodAccessor137.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:162)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:283)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
at org.openbravo.utils.SessionExpirationFilter.doFilter(SessionExpirationFilter.java:66)
at sun.reflect.GeneratedMethodAccessor136.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:218)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
at org.openbravo.utils.CharsetFilter.doFilter(CharsetFilter.java:35)
at sun.reflect.GeneratedMethodAccessor135.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:218)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
at org.openbravo.dal.core.DalRequestFilter$1.doAction(DalRequestFilter.java:81)
at org.openbravo.dal.core.ThreadHandler.run(ThreadHandler.java:46)
at org.openbravo.dal.core.DalRequestFilter.doFilter(DalRequestFilter.java:99)
at sun.reflect.GeneratedMethodAccessor134.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:244)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:276)
at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:218)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:56)
at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:185)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Search these forums for "headless", there's a property you need to set.
Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
Hey thanks for the reply i have gone through those forums but how to set that property(-Djava.awt.headless=true) in LINUX environment?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
In the CATALINA_OPTS variable in Tomcat's bin/catalina.sh script.
Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
Hey i have tried it but no luck.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
If you told us what you've tried we might be in a position to help.
Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
We are using the following java files to print a pdf. When the user clicks on print button the application should open an print dialog which will automatically print the PDF without opening the file on linux environment.

As suggested, we have set the environment CATALINA_OPTS and JAVA_OPTS variables to -Djava.awt.headless=true and tried to execute the program again but there still I get java.awt.headlessException on the console.

Please suggest.


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFRenderer;

class PDFPrintPage implements Printable {
private PDFFile file;

PDFPrintPage(PDFFile file) {
this.file =file;
}

public int print(Graphics g, PageFormat format, int index)
throws PrinterException {
int pagenum = index + 1;

// don't bother if the page number is out of range.
if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
// fit the PDFPage into the printing area
Graphics2D g2 = (Graphics2D)g;
PDFPage page = file.getPage(pagenum);
double pwidth = format.getImageableWidth();
double pheight = format.getImageableHeight();

double aspect = page.getAspectRatio();
double paperaspect = pwidth / pheight;

Rectangle imgbounds;

if (aspect>paperaspect) {
// paper is too tall / pdfpage is too wide
int height= (int)(pwidth / aspect);
imgbounds= new Rectangle(
(int)format.getImageableX(),
(int)(format.getImageableY() + ((pheight - height) / 2)),
(int)pwidth,
height
);
} else {
// paper is too wide / pdfpage is too tall
int width = (int)(pheight * aspect);
imgbounds = new Rectangle(
(int)(format.getImageableX() + ((pwidth - width) / 2)),
(int)format.getImageableY(),
width,
(int)pheight
);
}

// render the page
PDFRenderer pgs = new PDFRenderer(page, g2, imgbounds, null, null);
try {
page.waitForFinish();
pgs.run();
} catch (InterruptedException ie) {}

return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
}








import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.sun.pdfview.PDFFile;

public class PrinterTesting implements Printable {

// public static void main(String[] args) {
// PrinterTesting p = new PrinterTesting();
// p.print("E:\\ITextTest.pdf");
// System.exit(0);
// }

public String print(String fn) {
PrinterTesting p = new PrinterTesting();
FileReader ifile = null;
String strReturn="";
try {
ifile = new FileReader(fn);
} catch (FileNotFoundException fnf) {
System.err.println("File not found!");
}
strReturn=p.print(ifile,fn);
System.out.println("Print a file by name.command......"+strReturn);
// p.print(ifile,fn);
return strReturn;
}


/**
* Method: print
* <p>
*
* This class is responsible for rendering a page using the provided
* parameters. The result will be a grid where each cell will be half an
* inch by half an inch.
*
* @param g
* a value of type Graphics
* @param pageFormat
* a value of type PageFormat
* @param page
* a value of type int
* @return a value of type int
*/
public int print(Graphics g, PageFormat pageFormat, int page) {
System.out.println("INSIDE PRINT METHOD");
File f = new File("E:\\ITextTest1.pdf");
PrinterJob pjob = PrinterJob.getPrinterJob();
try{
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
PDFPrintPage pages = new PDFPrintPage(pdfFile);

// Create Print Job

PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

pjob.setJobName(f.getName());


Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);

// Send print job to default printer
pjob.print();

}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
g.dispose(); // flush page
// total end of print job.
pjob = null;

return (PAGE_EXISTS);

}

public String print(Reader ifile,String fileName) {
System.out.println("Inside print testing file");
String strReturn="OK";
BufferedReader is = new BufferedReader(ifile);
System.out.println("Buffer reader file name ..."+fileName);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
// GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// System.out.println("Headless mode: " + ge.isHeadless());
// if(!ge.isHeadless()){
// System.setProperty("java.awt.headless", "false");
// }
//System.setProperty("java.awt.headless", "true");
if (!printJob.printDialog()) { // User cancelled??
System.out.println("This is cancel ...");
strReturn ="CANCEL";
return strReturn;
}
try{
File f = new File(fileName);
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
PDFFile pdfFile = new PDFFile(bb); // Create PDF Print Page
PDFPrintPage pages = new PDFPrintPage(pdfFile);
// Create Print Job
//PrinterJob pjob = PrinterJob.getPrinterJob();
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

printJob.setJobName(f.getName());
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
printJob.setPageable(book);
// Send print job to default printer
printJob.print();
printJob = null; // avoid redundant calls to pjob.end()

}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

return strReturn;
}

}




Thanks,
Rohit.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
As suggested, we have set the environment CATALINA_OPTS and JAVA_OPTS variables to -Djava.awt.headless=true

How did you do that? Post a relevant excerpt from the altered catalina.sh script, and tell us how it's now different from the original script.
Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
earlier CATALINA_OPTS property was -server -Xms768M -Xmx1024M now we added -Djava.awt.headless=true

something like this
CATALINA_OPTS=-Djava.awt.headless=true -server -Xms768M -Xmx1024M

Find the environment variables set on my machine.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
You needn't expect anybody to read all that unformatted code. Use the edit button to add code tags, and post an SSCCE which people will actually be able to read the whole of.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Are you saying that you are not using the catalina.sh script? If not, what is the command you're using to run the server?
Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
We are using catalina.sh script.

The commands to stop & start the server.

/etc/init.d/tomcat6 stop
/etc/init.d/tomcat6 start


Thanks,
Rohit
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
If you're using the script then that's where you need to alter CATALINA_OPTS. Whatever you do outside of it is overridden by the variables in the script.
Rohit Nagamalla
Greenhorn

Joined: Jul 28, 2010
Posts: 7
so what do you suggest me to do??
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I thought my second post was plenty clear, but apparently not. Have you yet examined the script in order to understand how it works?
 
GeeCON Prague 2014
 
subject: Problem in printing a pdf file using pdfrenderer