I have a jsp file which I like to convert it to PDF using flying saucer. Here is the jsp file:
<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="display" uri="http://displaytag.sf.net/el" %>
<!DOCTYPE html>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="testDBForm" action="<%=basePath%>/TestDatabase" method="post" onsubmit="return true">
<input type="submit" id="btnInsert" value="btnInsert" name="btnInsert" text="INSERT"/>
<input type="submit" id="btnSelect" value="btnSelect" name="btnSelect" text="SELECT"/>
<input type="submit" id="btnDelete" value="btnDelete" name="btnDelete" text="DELETE"/>
<input type="submit" id="btnUpdate" value="btnUpdate" name="btnUpdate" text="UPDATE"/>
</form>
<c:if test="${not empty message}">
<h1>${message}</h1>
</c:if>
<c:if test="${not empty insert}">
<h1>Insert: ${message}</h1>
</c:if>
<c:if test="${not empty select}">
<h1>Select: ${message}</h1>
</c:if>
<c:if test="${not empty update}">
<h1>Update: ${message}</h1>
</c:if>
<c:if test="${not empty delete}">
<h1>Delete: ${message}</h1>
</c:if>
</body>
</html>
Here is the servlet code that I am using for parsing html to pdf:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/pdf");
String inputFile = "D:\\03072014\\src\\main\\webapp\\includes\\testDatabase.jsp";
String url="";
try {
url = new File(inputFile).toURI().toURL().toString();
} catch (MalformedURLException ex) {
Logger.getLogger(HtmlToPdfTaxCardConvertor.class.getName()).log(Level.SEVERE, null, ex);
}
OutputStream os=null;
try {
os = response.getOutputStream();
} catch (IOException ex) {
Logger.getLogger(HtmlToPdfTaxCardConvertor.class.getName()).log(Level.SEVERE, null, ex);
}
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
try {
renderer.createPDF(os);
os.close();
} catch (DocumentException ex) {
Logger.getLogger(HtmlToPdfTaxCardConvertor.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(HtmlToPdfTaxCardConvertor.class.getName()).log(Level.SEVERE, null, ex);
}
} }
I got exception that
javax.xml.transform.TransformerException: org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.
Could somebody help me and is it possible to create pdf from this kind of html page