Printing PDF file from a web application
803221Dec 7 2010 — edited Dec 8 2010I have integrated PDFRender API via the a servlet called PDFRendererServlet.
I am running tomcat (6.x) as the app server. I use eclipse ide installed on macbook pro. When I run the server and test locally it works fine (say, http://localhost:8080/pdfWeb/PDFRendererServlet).
While the server is running on the macbook, and test the print capability via a IE from the windows XP desktop computer (which is in network), I see the print dialog box on the macbook rather than on the windows XP desktop computer.
Appreciate if someone could help me out what am I doing wrong or any other fix or code modifications needed. Following is the code:
PDFRendererServlet.java_
public class PDFRendererServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String EXISTING_FILE = "/eclipse/workspace/pdf/demofiles/PUSD_Calendar.pdf";
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
FileInputStream fis = new FileInputStream(EXISTING_FILE);
PrintPdf printPDFFile;
try {
printPDFFile = new PrintPdf(fis, "Test Print PDF");
printPDFFile.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
PrintPdf.java_
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFRenderer;
/**
* Converts the PDF content into printable format
* Source: https://pdf-renderer.dev.java.net/
* http://webmoli.com/2008/11/03/java-print-pdf/
*/
public class PrintPdf {
private PrinterJob pjob = null;
private static final String EXISTING_FILE = "/eclipse/workspace/pdf/demofiles/demopage.pdf";
public static void main(String[] args) throws IOException, PrinterException {
// Create a PDFFile from a File reference
FileInputStream fis = new FileInputStream(EXISTING_FILE);
PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF");
printPDFFile.print();
}
/**
* Constructs the print job based on the input stream
*
* @param inputStream
* @param jobName
* @throws IOException
* @throws PrinterException
*/
public PrintPdf(InputStream inputStream, String jobName) throws IOException, PrinterException {
byte[] pdfContent = new byte[inputStream.available()];
inputStream.read(pdfContent, 0, inputStream.available());
initialize(pdfContent, jobName);
}
/**
* Constructs the print job based on the byte array content
*
* @param content
* @param jobName
* @throws IOException
* @throws PrinterException
*/
public PrintPdf(byte[] content, String jobName) throws IOException, PrinterException {
initialize(content, jobName);
}
/**
* Initializes the job
*
* @param pdfContent
* @param jobName
* @throws IOException
* @throws PrinterException
*/
private void initialize(byte[] pdfContent, String jobName) throws IOException, PrinterException {
ByteBuffer bb = ByteBuffer.wrap(pdfContent);
// Create PDF Print Page
PDFFile pdfFile = new PDFFile(bb);
PDFPrintPage pages = new PDFPrintPage(pdfFile);
// Create Print Job
pjob = PrinterJob.getPrinterJob();
PageFormat pf = PrinterJob.getPrinterJob().defaultPage();
pjob.setJobName(jobName);
Book book = new Book();
book.append(pages, pf, pdfFile.getNumPages());
pjob.setPageable(book);
// to remove margins
Paper paper = new Paper();
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
pf.setPaper(paper);
}
public void print() throws PrinterException {
// Send print job to default printer
pjob.printDialog();
pjob.print();
}
}
/**
* Class that actually converts the PDF file into Printable format
*/
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;
if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
Graphics2D g2 = (Graphics2D) g;
PDFPage page = file.getPage(pagenum);
// fit the PDFPage into the printing area
Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
(int) format.getImageableWidth(), (int) format.getImageableHeight());
g2.translate(0, 0);
PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
try {
page.waitForFinish();
pgs.run();
} catch (InterruptedException ie) {
// nothing to do
}
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
}
demopdf.jsp_
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>PDF file - Print Demo</h1>
<a href="<%=request.getContextPath() %>/print">here</a> to open the file (demo by PDFRendererServlet)
</body>
</html>
web.xml configuration_
<servlet>
<servlet-name>print</servlet-name>
<servlet-class>pdf.test.servlet.PDFRendererServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>print</servlet-name>
<url-pattern>/print</url-pattern>
</servlet-mapping>
Test URL_
http://localhost:8080/pdfweb/PrintRendererServlet