0

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

vikifor
  • 3,426
  • 4
  • 45
  • 75

1 Answers1

1

FS takes XHTML, this means its very picky about the input of the HTML file.

Heres 2 things to try:

  1. Put your <!DOCTYPE html> at the very top of the page.
  2. put an end / on your meta tag.
  3. use <c:out value="${message}" /> instead of ${message} to ensure no illegal characters are being placed in your HTML causing the parser to break.

If that fails heres a standard template I use for my FS jsp pages, the doc type declaration is optional and you can use the standard <!DOCTYPE html> but I find that a custom declaration greatly increases speed. It does however mean that you have to use decimal encoding the escape characters.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE doctypeName [
   <!ENTITY nbsp "&#160;">
   <!ENTITY amp "&#38;">
]> 
<%-- other jsp stuff here --%>
<%@include file="/WEB-INF/jsp/taglib.inc"%>
<html>
....
</html>


It should also be mentioned that if you dont want to bother with all this junk you can use JSoup or another HTML cleaner to cleanup your HTML and make sure its 100% good for FS. There is a good fork of the FS project going on that is being built out by danfickle on github that helps with that integration. He has also added in much more CSS3 support.
ug_
  • 11,267
  • 2
  • 35
  • 52