• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in printing a pdf file using pdfrenderer

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search these forums for "headless", there's a property you need to set.
 
Rohit Nagamalla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the CATALINA_OPTS variable in Tomcat's bin/catalina.sh script.
 
Rohit Nagamalla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey i have tried it but no luck.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you told us what you've tried we might be in a position to help.
 
Rohit Nagamalla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what do you suggest me to do??
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought my second post was plenty clear, but apparently not. Have you yet examined the script in order to understand how it works?
 
reply
    Bookmark Topic Watch Topic
  • New Topic