java中html网页转化成pdf(itext)

Java 实现 HTML 页面转 PDF 解决方案

一、添加 maven 依赖

<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itext-asian</artifactId>
  <version>5.2.0</version>
</dependency>
<dependency>
  <groupId>org.xhtmlrenderer</groupId>
  <artifactId>core-renderer</artifactId>
  <version>R8</version>
</dependency>

二、java代码实现

@ResponseBody
    @RequestMapping(value = "/saveNotification.do",method=RequestMethod.POST)
    public void saveNotification(HttpServletRequest request, HttpServletResponse response){
        JSONObject resultsObj = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(new Date());
        String targetPath = request.getServletContext().getRealPath(File.separator);   //项目根路径
        String htmlFile = targetPath + "/html/custormer_notification.html";    //目标html网页的地址
        String pdfPath = targetPath + "/pdf/";//pdf储存位置
        String pdfFile = pdfPath + date + ".pdf";//储存的pdf文件路径
        File filePath = new File(pdfPath);
        //判断此路径文件夹是否存在
        if (!filePath.exists()) {
            //如果不存在 ,则创建文件夹/目录
            filePath.mkdirs();
        }
        /*创建 内存中的File对象*/
        File file = new File(pdfFile);
        //如果有重名文件存在则删除文件,对象对应的硬盘必须删不能存在,如果已经存在 则会抛出IOException异常
        if (file.exists()) {
            file.delete();
        }
        try {
            OutputStream os = null;
            ITextRenderer renderer = new ITextRenderer();
            /*第一种*/
//            renderer.setDocument(new File(htmlFile).toURI().toString());
            /*第二种*/
            htmlFile = htmlFile.replace("\\", "/");
            File html_file = new File(htmlFile);
            FileInputStream inputStream = new FileInputStream(html_file);
            //流转换成字符串
            StringBuffer out = new StringBuffer();
            byte[] b = new byte[4096];
            for (int n; (n = inputStream.read(b)) != -1;) {
                out.append(new String(b, 0, n));
            }
            String html = out.toString();
            renderer.setDocumentFromString(html);
            //解决中文支持
            ITextFontResolver fontResolver = renderer.getFontResolver();
            if("linux".equals(getCurrentOperatingSystem())){
                fontResolver.addFont("/usr/share/fonts/chiness/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            }else{
                fontResolver.addFont("c:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            }
            os = new FileOutputStream(pdfFile);
            renderer.layout();
            renderer.createPDF(os);
            renderer.finishPDF();
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public String getCurrentOperatingSystem(){
    String os = System.getProperty("os.name").toLowerCase();
    System.out.println("---------当前操作系统是-----------" + os);
    return os;
}

三、注意事项:

1、输入的 HTML 页面必须是标准的 XHTML 页面。页面的顶上必须是这样的格式:

<!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">

2、语法必须是标准的 HTML 语法:所有标签都必须闭合

3、CSS 中必须指定字体,然后在 Java 代码中设置相应的字体

body { font-family: SimSun;}//此字体对应Java代码中的simsun.ttc,如果需要其他字体,则需要查询相关资料去
/*设定纸张大小  A4纸*/
@page{ size: a4; }
table标签添加 style="table-layout:fixed;word-break:break-strict;"

四、本人踩过的坑:

1、建议自己先编写一个简单的标准html网页进行测试,因为复杂的网页中有的css样式不支持此方法

2、在项目实际应用中会遇到pdf文档右边中文显示不全的问题,原因是itext源码问题,老外做的东西,没有考虑到中文问题。默认提供的包里,中文不会换行,有人修改了源代码,解决了这个问题。下载地址: 【介绍一个PDF的生成方案讨论第6页: - Java - ITeye论坛】  需要注意的是,在官网提供的jar包里,有两个包,一个是core-renderer.jar,另一个是core-renderer-minimal.jar。引用时,只需引用前者就行。

五、解决中文换行问题:

1、在resources目录下新建lib文件夹,将上面下载的jar包放在此文件夹下

2、在pom.xml文件中引入此本地jar包

<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>core-renderer</artifactId>
    <version>0.0.1</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/core-renderer.jar</systemPath>
</dependency>
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.0.8</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/src/main/resources/lib/itext-2.0.8.jar</systemPath>
</dependency>

3、添加maven打包资源位置

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <compilerArguments>
                    <extdirs>${project.basedir}/src/main/resources/lib</extdirs>
                </compilerArguments>
            </configuration>
        </plugin>
    </plugins>
</build>

五、解决pdf文件在windows系统和Linux系统在文字显示问题:

windows系统下,此处c:/Windows/Fonts/simsun.ttc默认自带字体,但是linux系统下,此处/usr/share/fonts/chiness/simsun.ttc是不存在字体样式的,而我们的项目需要部署在linux服务器上,所有我的解决办法是:

将windows系统中的simsun.ttc文件copy在项目中,如图所示:

然后在代码中引用项目中的字体路径,如图:

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值