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.