首页 新闻 会员 周边 捐助

html转pdf问题(导入css样式,不支持<%@ page...>等标签)

0
[已关闭问题] 关闭于 2016-12-21 18:34

实习进公司第一个接到的问题就是,网页有个“导出总揽”按钮,点击后将页面的导出成pdf格式。想法是:在前端用ajax将整个页面.html()形式发送到controller层,然后将String类型的字符串转为html,再将生成的html利用itext或者flyingsaucer生成pdf格式,实验了好几天,发现了个问题,就是从前端传值的时候,有些符号自动变了,比如“<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>”还有<link..../>,后面的“/>”变成了上面的“>”,然后就报错报错报错,转不了。后来将这些字符串在后台拼接,生成的html文件确实能完成转化为pdf格式的功能,但是里面的<link..>无法生成css,也就完成不了排版,然后生成的pdf完全是从上到下式的,如果使用flyingsaucer,发现不支持“<%@ page”“<%@ taglib”"<jsp:include"标签,直接导致后面的就不支持"<c:set"标签。下面贴上我的前端代码跟java实验代码,求各位大神解救我,不要百度其他人的代码复制给我,谢谢谢谢

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
 


public static boolean createPdf(String file) throws IOException, DocumentException {
        //jsp来源 
        String HTML = "C:\\Users\\Administrator\\Desktop\\test.html";
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            document.open();
            XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                    new FileInputStream(HTML), Charset.forName("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            document.close();
        }
        return true;
    }

    /**
     * Main method
     */
    public static void main(String[] args) throws IOException, DocumentException {
        String DEST = "C:\\Users\\Administrator\\Desktop\\我的总揽.pdf";
        if(createPdf(DEST)){
            createPdf(DEST);
            File file = new File(DEST);
            file.getParentFile().mkdirs();
            System.out.println("创建成功");
        }
    }
<%--这个是插入的jsp代码/include/new_public.jsp--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:set var="ctx" value="${pageContext.request.contextPath}" scope="application"></c:set> <%request.setAttribute("webRoot", request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/"); %> <link type="image/x-icon" rel="shortcut icon" href="<c:url value='/resources/images/favicon.ico'/>"></link> <script type="text/javascript"> var SITE = "${webRoot}"; </script> <link rel="stylesheet" type="text/css" href="${ctx }/resources/theme/static/common/css/base.css" ></link> <link rel="stylesheet" type="text/css" href="${ctx }/resources/theme/static/statistical/css/statistical.css" ></link> <link rel="stylesheet" type="text/css" href="${ctx }/resources/css/my.css" ></link> <!--公用js类库 start --> <script type="text/javascript" src="${ctx }/resources/js/jquery/jquery-1.10.2.js"></script> <script type="text/javascript" src="${ctx }/resources/js/jquery/jquery-migrate-1.1.1.js"></script> <script type="text/javascript" src="${ctx }/resources/js/jquery/jquery-json.js"></script> <script type="text/javascript" src="${ctx }/resources/js/Highstock-4.2.6/js/highstock.js"></script> <script type="text/javascript" src="${ctx }/resources/js/login/sso.js"></script> <script type="text/javascript" src="${ctx}/resources/js/WdatePicker/WdatePicker.js"></script>

 

<!--这个是前端代码-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <head> <c:set var="ctx" value="${pageContext.request.contextPath}" scope="application"></c:set> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>我的教学总览新</title> <jsp:include page="../include/new_public.jsp"></jsp:include> <script type="text/javascript" src="${ctx }/resources/js/common/pieCharts.js"></script> <script type="text/javascript" src="${ctx }/resources/js/overview/overview.js"></script> <script type="text/javascript"> $(function(){ $("#btn-export-fr-myview").click(function(){ var inputString="<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"; inputString+=$(":root").html(); inputString+="</html>"; //inputString=htmlEncode(htmlEncode(inputString)); alert(inputString); $.ajax({ type:"post", url:SITE+"/common/exportPdf.action", data:{"inputString":inputString}, error:function(){ alert("error"); } }); }); }); var SITE = "${ctx}"; Highcharts.setOptions({ global : { useUTC : false } }); $(function() { overview.prepareClass(); overview.workDistributeChart(); }); </script> <style> body{ font-family:SimSun; } </style> </head> <body> ... </body> </html>
SummerSummerHot的主页 SummerSummerHot | 菜鸟二级 | 园豆:202
提问于:2016-12-21 15:03
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册