如何利用java实现生成PDF文件_第1页
如何利用java实现生成PDF文件_第2页
如何利用java实现生成PDF文件_第3页
如何利用java实现生成PDF文件_第4页
如何利用java实现生成PDF文件_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第如何利用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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论