1.PDF 模板设计神器
软件名称:Adobe Acrobat DC
下载地址:可以自己下载,需要的私聊站长。
使用方式:
1.使用 Adobe Acrobat DC 打开需要设计的 PDF 模板文件(可编辑)
2.打开【工具】选项,如下图所示:
3.点击【准备表单】进入表单设计页面
4.进入表单设计页面,可以看到相关表单元素
5.设计相关表单
注意:此处的表单的 key 为类似 fill_3 这样的名称,这个 key 是程序设计
需要使用的。因此,建议自行按照相关程序定义进行编写,方便下一步的处理。
2.Java 操作 PDF 文件神器
1.引入依赖
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.1</version>
<type>pom</type>
</dependency>
</dependencies>
2.实现模板文件的标准化输出示例
public static void main(String[] args) throws Exception {
String REGULAR = "fonts/STSONG.TTF"; //src/resources
//模板文件路径
String filePath = "c:/template.pdf";
//标准化输出文件路径
String toPath = "c:/template-output.pdf";
PdfDocument pdfDoc = new PdfDocument(new PdfReader(filePath), new PdfWriter(toPath));
PdfAcroForm pdfAcroForm = PdfAcroForm.getAcroForm(pdfDoc, true);
//原始数据(待填充到PDF文件中的数据)
Map<String, String> formDatas = new HashMap<>();
//获取模板文件中的全量字段
Map<String, PdfFormField> formFields = pdfAcroForm.getFormFields();
formFields.forEach((key, value) -> {
try {
//处理中文乱码(支持Linux系统)
FontProgram fontProgram = FontProgramFactory.createFont(REGULAR,false);
PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.IDENTITY_H, false);
//获取到模板中的某个字段
PdfFormField agreementId = formFields.get(key);
agreementId.setFont(font);
//为模板中的某个字段设置值
agreementId.setValue(formDatas.get(key));
} catch (Exception e) {
e.printStackTrace();
}
});
//设置生成表单不可编辑,注意itext的版本,在7.0.0版本报错
pdfAcroForm.flattenFields();
pdfDoc.close();
}
3.扩展学习
3.1 PDF 转图片 Base64
引入依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
/**
* pdf 文件转图片
* @throws Exception
*/
public static void pdfToImg() throws Exception{
//原始文件路径
String pdfFilePath="c:/template.pdf";
float dpi=130;
File file = new File(pdfFilePath);
PDDocument pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
//获取PDF页码
int pages = pdDocument.getNumberOfPages();
for (int i = 0; i < pages; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image,"png", outputStream);
System.out.println(getImageStr(outputStream));
};
}
/**
* 图片转化成base64字符串
* @param outputStream
* @return
*/
public static String getImageStr( ByteArrayOutputStream outputStream) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
// 返回Base64编码过的字节数组字符串
String encode = null;
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
byte[] outByteArray = outputStream.toByteArray();
encode = encoder.encode(outByteArray);
return "data:image/png;base64,"+encode;
}
3.2 PDF 精准设置 Image 域(使用 DC 设计模板)
/**
* PDF image域设置值
* @param pdfFormField image类型字段
* @param filePath 文件路径
*/
public static void pdfAddImg(PdfFormField pdfFormField,String filePath){
try{
//方法一
PdfButtonFormField button = (PdfButtonFormField)pdfFormField;
InputStream is = new FileInputStream(new File("d:/timg.jpg"));
String str = Base64.encodeBytes(StreamUtil.inputStreamToArray(is));
button.setValue(str);
//知道文件路径的也可以直接使用下面的方法,下面方法的底层和方法一一样
// button.setImage("d:/timg.jpg");
}catch (Exception e){
e.printStackTrace();
}
}
3.3 HTML转PDF (基于Itext7)
引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.2</version>
</dependency>
源码实现:
/**
* HTML转PDF
* @param html HTML 源码
* @throws IOException
*/
public void html2Pdf(String html) throws IOException {
String font = "./fonts/STSONG.TTF";
ConverterProperties props = new ConverterProperties();
DefaultFontProvider defaultFontProvider = new DefaultFontProvider(false, false, false);
defaultFontProvider.addFont(font);
props.setFontProvider(defaultFontProvider);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
File f=new File("d:/html2pdf.pdf");
PdfWriter writer = new PdfWriter(f);
PdfDocument pdf = new PdfDocument(writer);
Document document = HtmlConverter.convertToDocument(html, pdf, props);
EndPosition endPosition = new EndPosition();
LineSeparator separator = new LineSeparator(endPosition);
document.add(separator);
document.getRenderer().close();
document.close();
}
/**
* 定义操作区域
*/
class EndPosition implements ILineDrawer {
// y坐标
protected float y;
/**
* @Description: 获取y坐标
* @return
*/
public float getY() {
return y;
}
/**
* @Description: 操作画布特定区域
* @param pdfCanvas:操作画布
* @param rect:操作区域
*/
@Override
public void draw(PdfCanvas pdfCanvas, Rectangle rect) {
this.y = rect.getY();
}
/**
* @Description: 获取行颜色
* @return
*/
@Override
public Color getColor() {
return null;
}
/**
* @Description: 获取行宽
* @return
*/
@Override
public float getLineWidth() {
return 0;
}
/**
* @Description: 设置行颜色
* @param color
*/
@Override
public void setColor(Color color) {
}
/**
* @Description: 设置行宽
* @param lineWidth:宽度
*/
@Override
public void setLineWidth(float lineWidth) {
}
}
说明:
HTML源码中的Img标签src值建议使用图片的base64格式
样式文件建议写在HTML源码中
标题:Java实现发票信息输出为PDF文件(模板化)
作者:TravelEngineers
地址:https://www.mycitymemory.com/articles/2020/01/07/1578382222537.html
版权声明:转载请注明博文地址,尊重作者劳动成果。
作者简介:坐标魔都,一枚爱旅行爱摄影的攻城狮。愿攻城拔寨的路上,你不用996,也不再孤单,加油。