1.把html转pdf,首先必须要解决中文显示问题,CSS样式问题以及可能的JS问题,先上例子,自己去体会。

2.先去下载simsun.ttc字体;

2.demo.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF­8"/>
<title>Title</title>
<style>
.color {
color: green;
} .pos {
position: absolute;
left: 200px;
top: 5px;
width: 200px;
font­size: 10px;
}
</style>
</head>
<body style="font-family: SimSun">
<img src="logo.png" width="600px" />
<div class="color pos">
hello,${name};
</div>
</body>
</html>

3.java实现转换代码

package com.ra.truck.createpdf;

import com.itextpdf.text.pdf.BaseFont;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer; import java.awt.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map; /**
* @Auther: lanhao
* @Date: 2018/8/7 11:46
* @Description:
*/
public class JavaToPdfHtmlFreeMarkerFS {
private static final String DEST ="../HelloWorld_CN_HTML_FREEMARKER_FS_index.pdf";
private static final String HTML = "demo.html";
private static final String FONT = "simsun.ttc";private static final String LOGO_PATH ="file:/"+PathUtil.getCurrentPath()+"/"+"demo448"+".png";
private static Configuration freemarkerCfg=null;
static {
freemarkerCfg =new Configuration();
//freemarker的模板目录
try {
freemarkerCfg.setDirectoryForTemplateLoading(new File(PathUtil.getCurrentPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
String os=System.getProperty("os.name");
System.out.println(os);
Map<String,Object> data = new HashMap<>();
data.put("name","lan浩");
String content =freeMarkerRender(data,HTML);
createPdf(content,DEST);
} /**
* freemarker渲染html
*/
public static String freeMarkerRender(Map<String, Object> data,String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF­8");
// 合并数据模型与模板
template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
} private static void createPdf(String content,String dest) {
try {
ITextRenderer render = new ITextRenderer();
//解决中文不显式问题
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONT,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
//解决图片相对路径的问题
render.getSharedContext().setBaseURL(LOGO_PATH);
render.layout();
render.createPDF(new FileOutputStream(dest));
}catch (Exception e) {
e.printStackTrace();
}
}
}

注:引用的文件全部放在项目的resource根目录下

使用freemarker和itext把html转pdf的更多相关文章

  1. iText框架(生成pdf文档)

    1.创建一个itext的简单示例 a.导包(pom.xml文件) <dependencies> <dependency> <groupId>com.lowagie& ...

  2. 用itext合并多个pdf文件【转】【补】

    java代码 package c; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arra ...

  3. Java使用IText(VM模版)导出PDF

    Java使用IText(VM模版)导出PDF: public String createPDF(ProjectManageBase projectManageBase) { Map map = new ...

  4. wkhtmltopdf+itext实现html生成pdf文件的打印下载(适用于linux及windows)

    目中遇到个根据html转Java的功能,在java中我们itext可以快速的实现pdf打印下载的功能,在itext中我们一般有以下三中方式实现 配置pdf模板,通过Adobe Acrobat 来设置域 ...

  5. 【转】Java通过IText导出word和pdf

    原帖地址:http://blog.csdn.net/zwx19921215/article/details/34439851 最近做的项目中需要用到把Highcharts图表导出word和pdf的功能 ...

  6. 利用itext将html转为pdf

    亲测代码没有问题,需要注意细节已经标注:需要jar包:iText-2.0.8.jar:core-renderer-R8.jar: core-renderer-R8.jar下载地址:http://cen ...

  7. itext转html为pdf遇到的问题

    记录一下使用itext将html文件转为pdf文件遇到的一些问题 1.中文不显示 原因:itext默认不支持中文 解决方法:引入中文字体 需要注意的是在java代码中设置好中文字体后,还需要在html ...

  8. Java使用IText(VM模版)导出PDF,IText导出word(二)

    ===============action=========================== //退款导出word    public void exportWordTk() throws IOE ...

  9. 在spring boot 中使用itext和itextrender生成pdf文件

    转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...

随机推荐

  1. dubbo系列一:dubbo介绍、dubbo架构、dubbo的官网入门使用demo

    一.dubbo介绍 Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成.简单地说,dubbo是一个基于Spri ...

  2. Stack堆栈的数据结构

    1.

  3. 使用Python启动一个简单的服务器

    在 Linux 服务器上或安装了 Python 的机器上,Python自带了一个WEB服务器 SimpleHTTPServer,我们可以很简单的使用  python -m SimpleHTTPServ ...

  4. [Ubuntu] Git可视化比较工具 P4Merge 的安装/配置及使用

    1 下载 下载地址. 链接到上面的下载页后,先找到左边导航的 Clients ,如下图 1 所示. 接着找到 P4Merge: Visual Merge Tool , 如下图 2 所示. 最后,选择好 ...

  5. 转载>>C# Invoke和BeginInvoke区别和使用场景

    转载>>C# Invoke和BeginInvoke区别和使用场景 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是dotnet程 ...

  6. 解决Pycharm更新package出现的问题:AttributeError:module 'pip' has no attribute 'main'

    很久一段时间没有更新Pycharm当中的package了,今天打开Pycharm点击package更新,发生了错误,AttributeError:module 'pip' has no attribu ...

  7. Excel带条件求和——SUMIF函数

    老婆求帮忙,问Excel中怎么跨Sheet带条件求和,就是关于sheet2中筛选出来的数据自动合计在sheet3中 . 比如有个sheet2表中的数据如下: 现在要在sheet3中求合计, 通过分析可 ...

  8. N - Asteroids

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  9. archlinux 下使用 aria2+uget 作为下载工具

    1.创建配置文件 sudo vim /etc/aria2/aria2.conf ## /etc/aria2/aria2.conf### '#'开头为注释内容, 选项都有相应的注释说明, 根据需要修改 ...

  10. python API whoami

    import getpass print getpass.getuser() See the documentation of the getpass module. getpass.getuser( ...