将jsp链接转为pdf文件

提供两个jsp/html转pdf的方法:

 /**
     * 把URL转换为PDF
     * @param outputFile , 示例:E:/11111.pdf
     * @param url,示例:http://localhost:8080/xxx/yyy.do?method=xxx
     * @return
     * @throws Exception
     */
    public static boolean htmlToPdf(String outputFile, String url)throws Exception {
        File outFile = new File(outputFile);
        if (!outFile.exists()) {
            outFile.getParentFile().mkdirs();
        }


        OutputStream os = new FileOutputStream(outputFile);
        
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        String content=getUrlContent(url);
        ITextRenderer renderer = new ITextRenderer();
        Document doc = builder.parse(new ByteArrayInputStream(url.getBytes("utf-8")));
        renderer.setDocument(doc, null);
//        renderer.setDocumentFromString(content);
        String fontPath = PdfUtil.class.getClassLoader().getResource("").toString().replaceAll("file:/", "")+ "simsun.ttc";
        System.out.println(fontPath);


        // 解决中文支持问题
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        renderer.layout();
        renderer.createPDF(os);
        os.flush();
        os.close();


        System.out.println("文件转换成功");
        return true;
    }


/**
     * 把URL转换为PDF
     * @param outputFile , 示例:E:/11111.pdf
     * @param url,示例:http://localhost:8080/xxx/yyy.do?method=xxx
     * @return
     * @throws Exception
     */
    public static boolean htmlToPdf2(String outputFile, String url)
            throws Exception {
        File outFile = new File(outputFile);
        if (!outFile.exists()) {
            outFile.getParentFile().mkdirs();
        }
        OutputStream os = new FileOutputStream(outputFile);
        ITextRenderer renderer = new ITextRenderer();

        renderer.setDocument(url);
        String fontPath = PdfUtil.class.getClassLoader().getResource("").toString().replaceAll("file:/", "")+ "simsun.ttc";
        System.out.println(fontPath);

        // 解决中文支持问题
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        renderer.layout();
        renderer.createPDF(os);
        os.flush();
        os.close();
        System.out.println("文件转换成功");
        return true;
    }

使用方法如下:

htmlToPdf2("E:/11111.pdf","http://localhost:8080/xxx/ccc/aaa.do?method=xxx");

htmlToPdf("E:/11111.pdf","http://localhost:8080/xxx/ccc/aaa.do?method=xxx");

需要的依赖是:

 <dependency>
     <groupId>org.xhtmlrenderer</groupId>
     <artifactId>core-renderer</artifactId>
     <version>R8</version>
 </dependency>

可能遇到的问题:

1. NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

解决: documentBuilderFactory.setNamespaceAware(true); 大多数情况下,此异常与使用的旧版本的 Xalan 有关。有条件的可以升级 到 2.7.0。

2. [Fatal Error] :1:1: Content is not allowed in prolog. 

解决: 检查各种标签是否是<div></div>这种完整的标签, 尤其注意</br>这种是不行的,必须是<br />

3. The entity name must immediately follow the '&' in the entity reference

解决:&必须要转义 用&amp;来替代&, 尤其注意&&要换成&amp;&amp;

4.The reference to entity "w" must end with the ';' delimiter.

解决: 字面意思,但我没找到我项目中的问题在哪...............九命!!!

5. org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.

解决: 大多数情况下,此异常与使用的旧版本的 Xalan 有关。确保升级 到 2.7.0。

博主已经崩溃了,大家可以一起交流=,=

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值