Skip to main content

convert image file to PDF

3 replies [Last post]
vempatia4u
Offline
Joined: 2009-01-13

index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

">

My JSP 'index.jsp' starting page

-->

please upload the image file

web.xml:
<?xml version="1.0" encoding="UTF-8"?>

This is the description of my J2EE component
This is the display name of my J2EE component
ImageToPDF
com.prime.PDFImage.ImageToPDF

ImageToPDF
/ImageToPDF.htm

index.jsp

servlet code:

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfWriter;

public class ImageToPDF extends HttpServlet {

/**
* Constructor of the object.
*/
public ImageToPDF() {
super();
}

/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("");
out.println("");
out.println(" A Servlet");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" ");
out.println("");
out.flush();
out.close();
}

/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream sout;
System.out.println("in DOPOST of Servlet");

response.setContentType("application/pdf");
sout = response.getOutputStream();

//PrintWriter out = response.getWriter();
// to get the content type information from JSP Request Header
String contentType = request.getContentType();
System.out.println("1");
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
System.out.println("2");
DataInputStream in = new DataInputStream(request.getInputStream());
// we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
// this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}

String file = new String(dataBytes);
// for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")
+ 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,
contentType.length());
int pos;
// extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)) .getBytes()).length;

String pdfFile=saveFile.substring(0,saveFile.length()-4);

// creating a new file with the same name and writing the
FileOutputStream fileOut = new FileOutputStream("D:/sureshv/"+saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
System.out.println(saveFile);
fileOut.close();
ImagesPDF imagPdf=new ImagesPDF();
//FileOutputStream fileOutPdf=new FileOutputStream("D:/sureshv/"+pdfFile+".pdf");

Document document = new Document();
System.out.println("Document created");
try {
PdfWriter.getInstance(document, sout);
document.open();
Image image = Image.getInstance("D:/sureshv/"+saveFile);
document.add(image);
document.close();
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response=configureResponse(response, pdfFile);
sout.close();

}
}

/**
* Initialization of the servlet.
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
private HttpServletResponse configureResponse(HttpServletResponse response, String fileName) {

System.out.println("file name is:" + fileName);
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.pdf");
response.addHeader("Content-disposition", "attachment; filename=\"" + fileName + ".pdf\"");

return response;
}

}

jar used :iText-2.1.4.jar
http://sourceforge.net/project/showfiles.php?group_id=15255&package_id=1...

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vempatia4u
Offline
Joined: 2009-01-13

SERVLET POST METHOD:

ServletOutputStream sout;
System.out.println("in DOPOST of Servlet");

response.setContentType("application/pdf");
sout = response.getOutputStream();

//PrintWriter out = response.getWriter();
// to get the content type information from JSP Request Header
String contentType = request.getContentType();
System.out.println("1");
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
System.out.println("2");
DataInputStream in = new DataInputStream(request.getInputStream());
// we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
// this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}

String file = new String(dataBytes);
// for saving the file name
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")
+ 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,
contentType.length());
int pos;
// extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)) .getBytes()).length;

String pdfFile=saveFile.substring(0,saveFile.length()-4);

// creating a new file with the same name and writing the
FileOutputStream fileOut = new FileOutputStream("D:/sureshv/"+saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
System.out.println(saveFile);
fileOut.close();
ImagesPDF imagPdf=new ImagesPDF();
//FileOutputStream fileOutPdf=new FileOutputStream("D:/sureshv/"+pdfFile+".pdf");

Document document = new Document();
System.out.println("Document created");
try {
PdfWriter.getInstance(document, sout);
document.open();
Image image = Image.getInstance("D:/sureshv/"+saveFile);
document.add(image);
document.close();
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response=configureResponse(response, pdfFile);
sout.close();

}
}

/**
* Initialization of the servlet.

*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
private HttpServletResponse configureResponse(HttpServletResponse response, String fileName) {

System.out.println("file name is:" + fileName);
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.pdf");
response.addHeader("Content-disposition", "attachment; filename=\"" + fileName + ".pdf\"");

return response;
}

vempatia4u
Offline
Joined: 2009-01-13

continue to above:

String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")
+ 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,
contentType.length());
int pos;
// extracting the index of file
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)) .getBytes()).length;

String pdfFile=saveFile.substring(0,saveFile.length()-4);

// creating a new file with the same name and writing the
FileOutputStream fileOut = new FileOutputStream("D:/sureshv/"+saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
System.out.println(saveFile);
fileOut.close();
ImagesPDF imagPdf=new ImagesPDF();
//FileOutputStream fileOutPdf=new FileOutputStream("D:/sureshv/"+pdfFile+".pdf");

Document document = new Document();
System.out.println("Document created");
try {
PdfWriter.getInstance(document, sout);
document.open();
Image image = Image.getInstance("D:/sureshv/"+saveFile);
document.add(image);
document.close();
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response=configureResponse(response, pdfFile);
sout.close();

vempatia4u
Offline
Joined: 2009-01-13

continue toa bove:

private HttpServletResponse configureResponse(HttpServletResponse response, String fileName) {

System.out.println("file name is:" + fileName);
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.pdf");
response.addHeader("Content-disposition", "attachment; filename=\"" + fileName + ".pdf\"");

return response;
}