html转化成PDF

本文介绍了将HTML转换为PDF的步骤,包括准备jar包、字体资源,提供了转换代码示例,并提到了使用的PDF工具。适合有开发经验者阅读,文中资源已上传至优快云下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html转化成PDF


本文主要介绍了html转化PDF的方法以及代码,文章有点长请耐心阅读。

  1. jar 包准备
	 core-renderer.jar
	 httpmime-4.5.jar
	 itext-2.0.8.jar
	 itextpdf-5.0.6.jar
	 zxing-core.jar
  1. 字体准备
	arialuni.ttf
	msyh.ttc
	msyhbd.ttc
	simfang.ttf
	simhei.ttf
	simsun.ttf
  1. 上代码
 @RequestMapping(value = "/xyxx/reportFileGenerate.jspx", method = RequestMethod.GET)
    @ResponseBody
    public RestResult reportFileGenerate1(String id, String code, String name, HttpServletRequest request, HttpServletResponse response, @RequestParam Map<String, Object> parameterMap, ModelMap model) throws Exception {
        String tempPath = Global.getConfig("temp.path");
        //报告下载数量(需要查询业务,此处我随便写的)
        int reportDownloadCount = 10;
        Date nowDate = new Date();
        //报告编号
        String reportNum = DateUtils.formatDate(nowDate, "yyyyMMddHHmmss") + String.format("%06d", reportDownloadCount + 1);
        //报告水印背景的字
        String waterText = "我是报告";
        File dfile = new File(tempPath);
        if (!dfile.exists()) {
            dfile.mkdirs();
        }
        //报告生成存放得临时本地目录
        String path = tempPath + "/" + reportNum + "_1.pdf";
        String path_sy = tempPath + "/" + reportNum + ".pdf";
        File file = null;
        Map<String, String> map = new HashMap<>();
        //报告中的业务数据
        //begain
        map.put("siteName", "生成报告");
        map.put("siteUrl", "www.xxxx.com");
        map.put("reportNum", reportNum);
        map.put("downloadTime", DateUtils.formatDate(nowDate, "yyyy年MM月dd日 HH:mm:ss"));
        //end
        //生成报告
        genPerviewReport(id, code, name, "L", reportNum, "/xyxx/creditReport.jspx", path, path_sy, request, waterText, map);
        file = new File(path_sy); //要下载的文件绝对路径
        String filePath="";
        if (file.exists()) {
            //此处我把报告上传到文件服务器去(根据自己实际情况)
            filePath = FileUpLoadUtils.uploadFileAndGetPath("creditReport", file, reportNum);
        }
      //写入下载记录(根据自身情况)
        ReportDownloadRecord reportDownloadRecord = new ReportDownloadRecord();
        reportDownloadRecord.setFilePath(filePath);
        reportDownloadRecord.setSiteName("站点1");
        reportDownloadRecord.setCode(reportNum);
        insertReportDownload(reportDownloadRecord);
        //生成后把报告编号信息返回前端 (原则上只要能返回可以查到报告的数据即可)
        return RestResult.success().setData(reportNum);
    }
/**
     * 生成pdf
     *
     * @param id  业务数据id
     * @param m010101  业务数据
     * @param name  业务数据
     * @param baseCredit  业务数据
     * @param reportNum  报告编号
     * @param reportUlr   报告地址(html页面)
     * @param path     报告存放路径
     * @param path_sy  报告存放路径
     * @param request
     * @param waterText  水印字
     * @param map  报告生成需要的数据
     * @return
     */
    public boolean genPerviewReport(String id, String m010101, String name, String baseCredit, String reportNum, String reportUlr, String path, String path_sy, HttpServletRequest request, String waterText, Map<String, String> map) {
        String tempPath = Global.getConfig("temp.path");
        File destPdf = new File(path);
        if (destPdf.exists()) {
            destPdf.delete();
        }
        File waterPdf = new File(path_sy);
        if (waterPdf.exists()) {
            waterPdf.delete();
        }
        try {
            destPdf.createNewFile();
            waterPdf.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String rootPath = tempPath + "/";
        //背景图片地址
        String imgpath = this.getClass().getResource("/") + "charts/backimg1.jpg";
        //获取html 地址
        String url = this.getReportHtmlUrl(request, reportUlr, id, m010101, name, baseCredit, reportNum);
        //生成PDF文件 信用报告
        this.genReport(rootPath, url, destPdf, map);
        //给pdf添加图片水印
        PdfExportNew.setWaterMarkForPDF(path, path_sy, imgpath, waterText);
        destPdf.delete();
        return true;
    }
  /**
     * 获取html的url
     *
     * @param request
     * @param reportUlr
     * @param m010101
     * @return
     */
    public String getReportHtmlUrl(HttpServletRequest request, String reportUlr, String id, String m010101, String name, String baseCredit, String reportNum) {
        return Global.getConfig("whSiteDomain") + "/" + reportUlr + "?baseCredit=" + baseCredit + "&id=" + id + "&code=" + m010101 + "&reportNum=" + reportNum + "&name=" + name;
    }
 /**
     * 生成报告
     * @param rootPath
     * @param url
     * @param outFile
     * @param map
     */
    public void genReport(String rootPath, String url, File outFile, Map<String, String> map) {
        String html = ReportUtils2.getHtml(url, "UTF-8");
        //生成pdf
        ReportUtils2.genPdf(rootPath, outFile, html);
        //添加页码
        pdfExportNew.addPage(rootPath, outFile, outFile, map);

    }

PDF 工具

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileInputStream;
import java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值