




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第如何利用java实现生成PDF文件目录1.PDF文件简介2.生成PDF2.1基于freemarker框架实现HTML转PDF2.1.1引入jar包依赖:2.1.2创建html模板test_template:2.1.3获取HTML内容2.1.4生成PDF文档总结
1.PDF文件简介
PDF是可移植文档格式,是一种电子文件格式,具有许多其他电子文档格式无法相比的优点。PDF文件格式可以将文字、字型、格式、颜色及独立于设备和分辨率的图形图像等封装在一个文件中。该格式文件还可以包含超文本链接、声音和动态影像等电子信息,支持特长文件,集成度和安全可靠性都较高。在系统开发中通常用来生成比较正式的报告或者合同类的电子文档。
2.生成PDF
2.1基于freemarker框架实现HTML转PDF
2.1.1引入jar包依赖:
dependency
groupIdjectlombok/groupId
artifactIdlombok/artifactId
version1.18.20/version
/dependency
!--/artifact/com.itextpdf/html2pdf--
dependency
groupIdcom.itextpdf/groupId
artifactIdhtml2pdf/artifactId
version4.0.3/version
/dependency
!--springboot项目请添加此依赖--
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-freemarker/artifactId
/dependency
!--非springboot项目请添加此依赖--
dependency
groupIdorg.freemarker/groupId
artifactIdfreemarker/artifactId
version2.3.30/version
/dependency
2.1.2创建html模板test_template:
!DOCTYPEhtml
htmllang="en"
head
metacharset="UTF-8"/
titleTitle/title
style
body{font-family:SimSun;}
.title{align-content:center;text-align:center;}
.signature{float:right}
/style
/head
body
div
h1标题/h1
h4副标题/h4
span当前时间:${date_time}/span
div日期:${date}/div
/div
/body
/html
2.1.3获取HTML内容
当HTML模板存放在系统文件夹
StringtemplateDirectory="D:\\";//系统文件夹路径如:D:\
当HTML模板存放在项目resources/templates目录
ClassLoaderclassLoader=PdfUtilTest.class.getClassLoader();
URLresource=classLoader.getResource("templates");
StringtemplateDirectory=resource.toURI().getPath();
示例代码:
importcom.itextpdf.html2pdf.ConverterProperties;
importcom.itextpdf.html2pdf.HtmlConverter;
importcom.itextpdf.layout.font.FontProvider;
importfreemarker.template.Configuration;
importfreemarker.template.Template;
importlombok.extern.slf4j.Slf4j;
importjava.io.*;
import.URL;
importjava.time.LocalDateTime;
importjava.time.format.DateTimeFormatter;
importjava.util.HashMap;
importjava.util.Map;
@Slf4j
publicclassPdfUtilTest{
*获取模板内容
*@paramtemplateDirectory模板文件夹
*@paramtemplateName模板文件名
*@paramparamMap模板参数
*@return
*@throwsException
privatestaticStringgetTemplateContent(StringtemplateDirectory,StringtemplateName,MapString,ObjectparamMap)throwsException{
Configurationconfiguration=newConfiguration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
try{
configuration.setDirectoryForTemplateLoading(newFile(templateDirectory));
}catch(Exceptione){
System.out.println("--exception--");
Writerout=newStringWriter();
Templatetemplate=configuration.getTemplate(templateName,"UTF-8");
cess(paramMap,out);
out.flush();
out.close();
returnout.toString();
publicstaticvoidmain(String[]args)throwsException{
MapString,ObjectparamMap=newHashMap();
DateTimeFormatterdateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss");
paramMap.put("date_time",dateTimeFormatter.format(LocalDateTime.now()));
paramMap.put("date",dateTimeFormatter.format(LocalDateTime.now()).substring(0,10));
ClassLoaderclassLoader=PdfUtilTest.class.getClassLoader();
URLresource=classLoader.getResource("templates");
StringtemplateDirectory=resource.toURI().getPath();
StringtemplateContent=PdfUtilTest.getTemplateContent(templateDirectory,"test_template.html",paramMap);
System.out.println(templateContent);
2.1.4生成PDF文档
示例代码:
/**
*HTML转PDF
*@paramcontenthtml内容
*@paramoutPath输出pdf路径
*@return是否创建成功
publicstaticbooleanhtml2Pdf(Stringcontent,StringoutPath){
try{
ConverterPropertiesconverterProperties=newConverterProperties();
converterProperties.setCharset("UTF-8");
FontProviderfontProvider=newFontProvider();
fontProvider.addSystemFonts();
converterProperties.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(content,newFileOutputStream(outPath),converterProperties);
}catch(Exceptione){
log.error("生成模板内容失败,{}",e);
returnfalse;
returntrue;
*HTML转PDF
*@paramcontenthtml内容
*@returnPDF字节数组
publicstaticbyte[]html2Pdf(Stringcontent){
ByteArrayOutputStreamoutputStream=newByteArrayOutputStream();;
try{
ConverterPropertiesconverterProperties=newConverterProperties();
converterProperties.setCharset("UTF-8");
FontProviderfontProvider=newFontProvider();
fontProvider.addSystemFonts();
converterProperties.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(content,outputStream,converterProperties);
}catch(Exceptione){
log.error("生成PDF失败,{}",e);
returnoutputStream.toByteArray();
publicstaticvoidmain(String[]args)throwsException{
MapString,ObjectparamMap=newHashMap();
DateTimeFormatterdateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss");
paramMap.put("date_time",dateTimeFormatter.format(LocalDateTime.now()));
paramMap.put("date",dateTimeFormatter.format(LocalDate
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 软件设计师考试全方位试题及答案分析
- 病房火灾的应急预案演练脚本(3篇)
- 烤房火灾应急预案(3篇)
- 网络架构设计对业务支持的影响试题及答案
- 软件设计师考试顺序安排试题及答案详情
- 2025年软件设计师考试考点解析试题及答案
- 2025年软件设计师考试高效阅读材料试题及答案
- 计算机二级VB考试核心试题及答案
- 2025年程序员考试的复习策略试题及答案
- 实践指导2025年软件设计师试题及答案
- 2025年医疗行业反垄断监管政策变化与合规经营关键指引报告
- 矿产资源开采与销售协议
- 《支气管镜检查技术》课件
- 育肥猪考试试题及答案
- 写作技巧知识培训课件
- 顺丰公司外包协议合同书
- 2025年度教师资格证考试综合素质必考250个重点知识汇编
- 2025年中考数学三轮冲刺训练一次函数中几何压轴题综合训练
- 中考英语词汇电子版单选题100道及答案
- 2025年中考政治总复习必考重点知识复习提纲
- 河南省安阳市(百师联盟)2023-2024学年高一下学期5月大联考数学试题(人教版)(解析版)
评论
0/150
提交评论