简体   繁体   English

使用飞碟将JSP转换为PDF

[英]Convert jsp to PDF using flying saucer

I have a jsp file which I like to convert it to PDF using flying saucer. 我有一个jsp文件,我想使用飞碟将其转换为PDF。 Here is the jsp file: 这是jsp文件:

<%@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: 这是我用于将html解析为pdf的servlet代码:

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 有人可以帮我吗,是否可以从这种html页面创建pdf

FS takes XHTML , this means its very picky about the input of the HTML file. FS使用XHTML ,这意味着它对HTML文件的输入非常挑剔。

Heres 2 things to try: 这里有2种方法可以尝试:

  1. Put your <!DOCTYPE html> at the very top of the page. <!DOCTYPE html>放在页面的顶部。
  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. 请使用<c:out value="${message}" />而不是${message}来确保HTML中没有放置任何非法字符,从而导致解析器损坏。

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. 如果失败了,这是我用于FS jsp页面的标准模板,则doc类型声明是可选的,您可以使用标准的<!DOCTYPE html>但是我发现自定义声明大大提高了速度。 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. 还应该提到的是,如果您不想打扰所有这些垃圾,可以使用JSoup或其他HTML清理程序来清理HTML,并确保其100%适用于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. 正在进行的FS项目的一个好分支danfickle在github上构建的,有助于实现该集成。 He has also added in much more CSS3 support. 他还添加了更多的CSS3支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM