1.添加依赖
<!--解决中文显示问题-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--生成html代码-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
<!--生成pdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!--xml模板-->
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.13</version>
</dependency>
2.生成pdf在页面预览
@RequestMapping("/showPdf")
public void showPdf(HttpServletResponse response){
try {
response.setContentType("application/pdf");
//inline设置是强制浏览器显示,attachment设置时强制浏览器下载
response.setHeader("Content-Disposition", "inline; filename= file");
// 1.新建document
Document document = new Document();
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
//创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
// 3.打开文档
document.open();
//要解析的html
//html转换成普通文字,方法如下:
org.jsoup.nodes.Document contentDoc = Jsoup.parseBodyFragment("<h1 align=\"center\">" + "测试主题"+ "</h1>");
org.jsoup.nodes.Document.OutputSettings outputSettings = new org.jsoup.nodes.Document.OutputSettings();
outputSettings.syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
contentDoc.outputSettings(outputSettings);
String parsedHtml = contentDoc.outerHtml();
System.out.println(parsedHtml);
//这儿的font-family不支持汉字,{font-family:仿宋} 是不可以的。
InputStream cssIs = new ByteArrayInputStream("* {font-family: PingFang-SC-Medium.otf;}".getBytes("UTF-8"));
//第四个参数是html中的css文件的输入流
//第五个参数是字体提供者,使用系统默认支持的字体时,可以不传。
Charset charset = Charset.defaultCharset();
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(parsedHtml.getBytes()), null,charset,new FontProviderUtil());
// XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(parsedHtml.getBytes()));
// 5.关闭文档
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3.中文显示问题
重写XMLWorkerFontProvider类
public class FontProviderUtil extends XMLWorkerFontProvider {
@Override
public Font getFont(final String fontname, final String encoding,
final boolean embedded, final float size, final int style,
final BaseColor color) {
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
}
4.替换字符串中的${xxx}特殊字符
重写mybatis替换占位符的方法去实现
// 重写mybatis中解决占位符替换问题
public class VariableTokenHandler implements TokenHandler {
private Map<String, String> variables = new HashMap<>();
public VariableTokenHandler(Map<String, String> variables) {
this.variables = variables;
}
@Override
public String handleToken(String content) {
String value = variables.get(content);
return value ;
}
}
5.调用
Map<String,String> params = new HashMap<>() ;
params.put("name","zzp") ;
params.put("age","25") ;
String sqlTemplate = "123insert into user(username,age,sequence,${age}) values(${name},${age},${name}.nextval)" ;
VariableTokenHandler handler = new VariableTokenHandler(params);
// 替换sql模板中的${xxx}占位符
GenericTokenParser parser = new GenericTokenParser("${", "}", handler);
String retContent = parser.parse(sqlTemplate);
System.out.println(retContent);
6.问题
生成的富文本xml中,出现替换不了的占位符,可能是占位符不对,如:${name}变成了${name<u></u>}
网友评论