[英]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种方法可以尝试:
<!DOCTYPE html>
at the very top of the page. <!DOCTYPE html>
放在页面的顶部。 /
on your meta tag. /
在你的元标记。 <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 " ">
<!ENTITY amp "&">
]>
<%-- other jsp stuff here --%>
<%@include file="/WEB-INF/jsp/taglib.inc"%>
<html>
....
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.