package main.java.com.example.pdf.util;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import main.com.example.pdf.util.B64ImgReplacedElementFactory;
import main.java.com.example.pdf.domain.SchedulePlan;
import org.xhtmlrenderer.pdf.ITextRenderer;
import sun.misc.BASE64Encoder;
import com.lowagie.text.DocumentException;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;
public class PdfUtils {
public static void main(String[] args) {
try {
Map<Object, Object> o=new HashMap<Object, Object>();
//存入一个集合
List<SchedulePlan> list = new ArrayList<SchedulePlan>();
for(int i=0;i<10;i++){
SchedulePlan schedulePlan=new SchedulePlan();
schedulePlan.setTitle("信息局" + i);
schedulePlan.setPlace("信息技术局1");
schedulePlan.setHotelName("名族大道哦");
schedulePlan.setArriveTiime("2018-05-08");
list.add(schedulePlan);
}
o.put("name", "https://www.baidu.com/img/bd_logo1.png?where=super");
o.put("nameList", list);
String path=PdfHelper.getPath();
generateToFile(path, "main/resources/templates/test.ftl",path+"pdf/", o, "D:\\pdfDemo.pdf");
System.out.println("生成成功");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成PDF到文件
* @param ftlPath 模板文件路径(不含文件名)
* @param ftlName 模板文件吗(不含路径)
* @param imageDiskPath 图片的磁盘路径
* @param data 数据
* @param outputFile 目标文件(全路径名称)
* @throws Exception
*/
public static void generateToFile(String ftlPath,String ftlName,String imageDiskPath,Object data,String outputFile) throws Exception {
String html=PdfHelper.getPdfContent(ftlPath, ftlName, data);
OutputStream out = null;
ITextRenderer render = null;
out = new FileOutputStream(outputFile);
render = PdfHelper.getRender();
render.setDocumentFromString(html);
if(imageDiskPath!=null&&!imageDiskPath.equals("")){
//html中如果有图片,图片的路径则使用这里设置的路径的相对路径,这个是作为根路径
render.getSharedContext().setBaseURL("file:/"+imageDiskPath);
//图片可直接使用地址返回,我这里是使用base64编码格式来显示图片
render.getSharedContext().setReplacedElementFactory(new B64ImgReplacedElementFactory());
}
render.layout();
render.createPDF(out);
render.finishPDF();
render = null;
out.close();
}
/**
* 生成PDF到输出流中(ServletOutputStream用于下载PDF)
* @param ftlPath ftl模板文件的路径(不含文件名)
* @param ftlName ftl模板文件的名称(不含路径)
* @param imageDiskPath 如果PDF中要求图片,那么需要传入图片所在位置的磁盘路径
* @param data 输入到FTL中的数据
* @param response HttpServletResponse
* @return
* @throws TemplateNotFoundException
* @throws MalformedTemplateNameException
* @throws ParseException
* @throws IOException
* @throws TemplateException
* @throws DocumentException
*/
public static OutputStream generateToServletOutputStream(String ftlPath,String ftlName,String imageDiskPath,Object data,HttpServletResponse response) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException, DocumentException{
String html=PdfHelper.getPdfContent(ftlPath, ftlName, data);
OutputStream out = null;
ITextRenderer render = null;
out = response.getOutputStream();
render = PdfHelper.getRender();
render.setDocumentFromString(html);
if(imageDiskPath!=null&&!imageDiskPath.equals("")){
//html中如果有图片,图片的路径则使用这里设置的路径的相对路径,这个是作为根路径
render.getSharedContext().setBaseURL("file:/"+imageDiskPath);
}
render.layout();
render.createPDF(out);
render.finishPDF();
render = null;
return out;
}
public static String BASE64Encoder(String path){
InputStream in;
byte[] picdata = null;
try {
in = new FileInputStream(path);
picdata = new byte[in.available()];
in.read(picdata);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
String img = encoder.encode(picdata);
return img;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java根据特定模板生成pdf
共47个文件
jar:6个
ttf:6个
java:6个
4星 · 超过85%的资源 需积分: 50 81 下载量 143 浏览量
2018-05-09
11:05:01
上传
评论
收藏 93.78MB RAR 举报
温馨提示
是java普通项目,直接导入MyEclipse即可运行即可使用(已包含jar包,没有数据库),代码注释很清楚,网上很多都是maven项目,且bug很多,现已进行修复。main方法执行即可生成。
资源推荐
资源详情
资源评论
收起资源包目录
pdf.rar (47个子文件)
pdf
.project 2KB
.settings
.jsdtscope 500B
org.eclipse.core.resources.prefs 234B
org.eclipse.wst.common.component 447B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 282B
WebRoot
WEB-INF
web.xml 404B
lib
freemarker-2.3.22.jar 1.24MB
core-renderer-R8-0604.jar 1.12MB
lib.rar 2.33MB
iText-2.0.8.jar .jar 1.16MB
iTextAsian.jar 322KB
lib1.rar 5.23MB
core-renderer.jar.jar 1.12MB
bouncycastle.jar 1.1MB
classes
test
java
com
example
pdf
main
resources
templates
msyhbd.ttc 15.49MB
msyh.ttf 14.37MB
msyhl.ttc 11.45MB
test.ftl 4KB
simsun.ttf 10.01MB
arialuni.ttf 21.87MB
application.properties 0B
java
com
example
pdf
domain
SchedulePlan.class 1KB
util
PdfHelper.class 5KB
PdfUtils.class 5KB
com
example
pdf
util
B64ImgReplacedElementFactory.class 3KB
index.jsp 834B
META-INF
MANIFEST.MF 36B
.myeclipse
src
test
java
com
example
pdf
Demo1ApplicationTests.java 335B
main
resources
static
templates
msyhbd.ttc 15.49MB
msyh.ttf 14.37MB
msyhl.ttc 11.45MB
test.ftl 4KB
simsun.ttf 10.01MB
arialuni.ttf 21.87MB
application.properties 0B
java
com
example
pdf
Demo1Application.java 319B
domain
SchedulePlan.java 747B
util
PdfHelper.java 4KB
PdfUtils.java 5KB
com
example
pdf
util
B64ImgReplacedElementFactory.java 4KB
.classpath 604B
demo-pdf
.project 2KB
bin
.project 2KB
共 47 条
- 1
资源评论
- hu11399320952019-08-06ERROR: '对实体 "id" 的引用必须以 ';' 分隔符结尾。'这错误是什么意思a
我叫满家镓
- 粉丝: 0
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功