java生成中文pdf,java生成PDF-Go语言中文社区

不废话,直接上代码:

依赖jar包

com.itextpdf

itextpdf

5.5.11

com.itextpdf.tool

xmlworker

5.5.12

org.freemarker

freemarker

2.3.23

PdfUtils类:

package com.brank.practice.brankyang.utils;

import com.itextpdf.text.Document;

import com.itextpdf.text.pdf.PdfWriter;

import com.itextpdf.tool.xml.XMLWorkerFontProvider;

import com.itextpdf.tool.xml.XMLWorkerHelper;

import freemarker.template.Configuration;

import freemarker.template.Template;

import java.io.*;

import java.nio.charset.Charset;

import java.util.HashMap;

import java.util.Map;

/**

* @author yangbo

* @version 1.0

* Created 2019/2/22 17:45

**/

public class PdfUtils {

private static final String FONT = "/sim-hei.ttf";//黑体字

private static final String HTML = "/test.html";//模板html文件位置

private static final String PDF="target/result.pdf";//目标pdf文件

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) throws Exception{

Map data=new HashMap();

StringBuffer message=new StringBuffer();

message.append("输入你想写的文字");

data.put("message",message.toString());//

String content=freeMarkerRender(data,HTML);//替换html中的参数

Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDF));

document.open();

XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);

fontImp.register(FONT);

XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"), fontImp);

document.close();

}

private static String freeMarkerRender(Map 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;

}

}

PathUtil类:

package com.brank.practice.brankyang.utils;

import java.io.File;

/**

* @author yangbo

* @version 1.0

* Created 2019/2/22 16:26

**/

public class PathUtil {

public static String getCurrentPath() {

Class> caller = getCaller();

if (caller == null) {

caller = PathUtil.class;

}

return getCurrentPath(caller);

}

private static Class> getCaller() {

StackTraceElement[] stack = (new Throwable()).getStackTrace();

if (stack.length < 3) {

return PathUtil.class;

}

String className = stack[2].getClassName();

try {

return Class.forName(className);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

return null;

}

/**

* 获取当前路径

* @param cls

* @return

*/

public static String getCurrentPath(Class> cls) {

String path = cls.getProtectionDomain().getCodeSource().getLocation().getPath();

path = path.replaceFirst("file:/", "");

path = path.replaceAll("!/", "");

if (path.lastIndexOf(File.separator) >= 0) {

path = path.substring(0, path.lastIndexOf(File.separator));

}

if ("/".equalsIgnoreCase(path.substring(0, 1))) {

String osName = System.getProperty("os.name").toLowerCase();

if (osName.contains("window")) {

path = path.substring(1);

}

}

return path;

}

}

test.html:

Title

body{

font-family:SimHei;

}

.blue{

color: black;

}

.pos{

position:absolute;

left:100px;

top:150px

}

文件标题

你想说点啥吗: ${message}

文件结尾。

文件存放位置截图:

9e3972259181e1847b73b36284063c5c.png

生成文件位置:

a61292a4eeb900a17a33214d6e3de7cf.png

效果图:

468395a4efe1fe8b5c9627c064148b5a.png

sim-hei.ttf文件见我的附件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值