Html to pdf in java struts.
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.html. simpleparser.HTMLWorker;
import com.lowagie.text.html. simpleparser.StyleSheet;
import com.lowagie.text.pdf. PdfWriter;
import com.lowagie.text.pdf.codec. Base64;
import java.util.ArrayList;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http. HttpServletRequest;
import javax.servlet.http. HttpServletResponse;
import javax.servlet.http. HttpServlet;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http. HttpSession;
import org.apache.struts.action. ActionForm;
import org.apache.struts.action. ActionMapping;
import org.apache.struts.action. ActionForward;
import org.apache.struts.validator. DynaValidatorForm;
import org.hibernate. HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import plugins. HibernateSessionFactory;
String contextName = request.getContextPath(). substring(1);
HttpSession hs = request.getSession();
String path = hs.getServletContext(). getRealPath("/" + contextName);
char pathSep = new File(path).separatorChar;
path = path.substring(0, path.lastIndexOf(pathSep + contextName));
path = path + pathSep + "MyForms";
Document pdfDoc = new Document(PageSize.A4, 50, 50, 1, 50);
Reader htmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(path+"\\ newRegistrn.html")));
ByteArrayOutputStream byteAOS = new ByteArrayOutputStream();
PdfWriter.getInstance(pdfDoc, byteAOS);
pdfDoc.open();
StyleSheet stylez = new StyleSheet();
stylez.loadTagStyle("body", "font", "Tahoma");
ArrayList arrayElementList = HTMLWorker.parseToList( htmlReader, stylez);
for (int i = 0; i < arrayElementList.size(); ++i) {
Element e = (Element) arrayElementList.get(i);
pdfDoc.add(e);
}
pdfDoc.close();
DateFormat dff = new SimpleDateFormat("dd_MMMM_ yyyy");
DateFormat tf = new SimpleDateFormat("hhmmss");
String dateString = dff.format(new java.util.Date());
String timeString = tf.format(new java.util.Date());
String pdfStoredFileName = "\\newRegistrn_" + dateString + timeString + "__" + ".pdf";
byte[] byt = byteAOS.toByteArray();
String pdfBase64 = Base64.encodeBytes(byt);
File pdfFile = new File(path+pdfStoredFileName);
FileOutputStream out = new FileOutputStream(pdfFile);
out.write(byt);
out.close();
htmlReader.close();
String fileToDownload =pdfStoredFileName;
String disHeader = "Attachment;Filename=" + fileToDownload;
response.setHeader("Content- Disposition", disHeader);
File desktopFile = new File(path+pdfStoredFileName);
PrintWriter pw = response.getWriter();
FileInputStream fileInputStream = new FileInputStream(desktopFile);
int j;
pw.flush();
while ((j = fileInputStream.read()) != -1) {
pw.write(j);
}
fileInputStream.close();
response.flushBuffer();
pw.flush();
pw.close();
File fileToDel = new File(path+pdfStoredFileName);
if(fileToDel.delete()){
System.out.println("\n "+fileToDel.getName() + " is deleted!");
} else {
System.out.println("\n Delete operation is failed.");
}
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.html.
import com.lowagie.text.html.
import com.lowagie.text.pdf.
import com.lowagie.text.pdf.codec.
import java.util.ArrayList;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.
import javax.servlet.http.
import javax.servlet.http.
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.
import org.apache.struts.action.
import org.apache.struts.action.
import org.apache.struts.action.
import org.apache.struts.validator.
import org.hibernate.
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import plugins.
String contextName = request.getContextPath().
HttpSession hs = request.getSession();
String path = hs.getServletContext().
char pathSep = new File(path).separatorChar;
path = path.substring(0, path.lastIndexOf(pathSep + contextName));
path = path + pathSep + "MyForms";
Document pdfDoc = new Document(PageSize.A4, 50, 50, 1, 50);
Reader htmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(path+"\\
ByteArrayOutputStream byteAOS = new ByteArrayOutputStream();
PdfWriter.getInstance(pdfDoc, byteAOS);
pdfDoc.open();
StyleSheet stylez = new StyleSheet();
stylez.loadTagStyle("body", "font", "Tahoma");
ArrayList arrayElementList = HTMLWorker.parseToList(
for (int i = 0; i < arrayElementList.size(); ++i) {
Element e = (Element) arrayElementList.get(i);
pdfDoc.add(e);
}
pdfDoc.close();
DateFormat dff = new SimpleDateFormat("dd_MMMM_
DateFormat tf = new SimpleDateFormat("hhmmss");
String dateString = dff.format(new java.util.Date());
String timeString = tf.format(new java.util.Date());
String pdfStoredFileName = "\\newRegistrn_" + dateString + timeString + "__" + ".pdf";
byte[] byt = byteAOS.toByteArray();
String pdfBase64 = Base64.encodeBytes(byt);
File pdfFile = new File(path+pdfStoredFileName);
FileOutputStream out = new FileOutputStream(pdfFile);
out.write(byt);
out.close();
htmlReader.close();
String fileToDownload =pdfStoredFileName;
String disHeader = "Attachment;Filename=" + fileToDownload;
response.setHeader("Content-
File desktopFile = new File(path+pdfStoredFileName);
PrintWriter pw = response.getWriter();
FileInputStream fileInputStream = new FileInputStream(desktopFile);
int j;
pw.flush();
while ((j = fileInputStream.read()) != -1) {
pw.write(j);
}
fileInputStream.close();
response.flushBuffer();
pw.flush();
pw.close();
File fileToDel = new File(path+pdfStoredFileName);
if(fileToDel.delete()){
System.out.println("\n "+fileToDel.getName() + " is deleted!");
} else {
System.out.println("\n Delete operation is failed.");
}
0 comments:
Post a Comment