java用HTML加载动态数据生成PDF下载(demo含jar) - Go语言中文社区

java用HTML加载动态数据生成PDF下载(demo含jar)


最近工作需要,项目中部分页面需要转化成pdf进行下载,我们的页面用的是jsp,但是jsp不能直接转pdf,需要用html,所以部分页面又重做了一份,做成了html样式,因为这个东西用的不多,太深的东西没有去看,此处仅给出html转pdf的demo,让你拿到即可用,heml需要用ajax动态渲染数据,我这里写的是静态的数据。

代码是我从网上找的,然后自己补充了部分jar和类的引用等东西,拿到即可用

所需的jar包

jar包引入这一个就行,会关联下载别的
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>core-renderer</artifactId>
            <version>R8</version>
        </dependency>

pdf做文字文字转化的时候需要用到字体,这里用的是 simsun.ttc
下载路径是

http://www.font5.com.cn/font_download.php?id=150&part=1237886897

下载后在文件的位置是
这里写图片描述

PdfUtil.java

package com.jp.controller;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

//import com.itextpdf.text.pdf.BaseFont;
import com.lowagie.text.pdf.BaseFont;

@Controller
@RequestMapping("/ces")
public class PdfUtil {

    @RequestMapping("/ces.do")
    public void cess() throws Exception{
        System.out.println("开始了=====");
        htmlToPdf2("d:/pdf/11111.pdf","http://127.0.0.1:8080/shopuu_mvc/ces/ce.do");
        System.out.println("结束了=====");
    }

        @RequestMapping("/ce.do")
    public ModelAndView ces(){
        Map<String, Object> map =new HashMap<String, Object>();
        map.put("payResponse", "ljp");
        map.put("op_date", new Date());
    //request.setAttribute("option", "pdf  download");
        return new ModelAndView("first", map);
    }


    /**
     * 把URL转换为PDF
     * @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="/simsun.ttc";
        // 解决中文支持问题
        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;
    }

}

在上述 java里面 我访问的是 http://127.0.0.1:8081/ljp_mvc/first.jsp 实际里面写的是html标签

first.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>页面打印</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type='text/css'>
.tab td {
    border-bottom: 1 solid #000000;
    border-left: 1 solid #000000;
    border-right: 0 solid #ffffff;
    border-top: 0 solid #ffffff;
}

.tab {
    border-color: #000000 #000000 #000000 #000000;
    border-style: solid;
    border-top-width: 2px;
    border-right-width: 2px;
    border-bottom-width: 1px;
    border-left-width: 1px;
}

.hr {
    font-family: '宋体';
    font-size: 9pt;
}
</style>
</head>
 <body bgcolor='white' style='font-family:SimSun; height:100%;' screen_capture_injected='true' ryt11773='1'> 

    <table cellspacing='0' cellpadding='0' width='100%' align='center'>
        <tr>
            <td align='center' colspan='4' style='font-size: 24px'><b
                id='pdf_text'>授信历史信息下载</b></td>
        </tr>
    </table>        


    <table class='tab' cellSpacing='0' cellPadding='0' width='100%' border='1'>
        <tr align='center' height='23'>
            <td width='10%' height='23'>姓名</td>
            <td width='10%' height='23'>${payResponse}</td>
            <td width='10%' height='23'>身份证号</td>
            <td width='10%' height='23'>${payResponse}</td>
        </tr>
        <tr align='center' height='23'>
            <td width='10%' height='23'>授信人</td>
            <td width='10%' height='23'>${payResponse}</td>
            <td width='10%' height='23'>授信日期</td>
            <td width='10%' height='23'><fmt:formatDate value="${op_date}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
        </tr>
    </table>

</body>
</html> 

这个页面不在 mvc项目映射的view路径下面,first.jsp 在webapp下,个web-inf同级

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/xuxie13/article/details/81298872
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 10:33:10
  • 阅读 ( 1115 )
  • 分类:前端

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢