JAVA生成PDF文件(根据模板和html 显示中文)
程序员文章站
2024-03-21 14:36:52
...
一、引入包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.6</version>
</dependency>
二、代码
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class PDFUtils {
private static Logger log = LoggerFactory.getLogger(PDFUtils.class);
/**
* 生成PDF(模板)
* @param templatePath 模板路径
* @param newPDFPath 生成的新文件路径
* @param map 模板字段和对应的赋值
* @return
*/
public boolean makePDF(String templatePath,String newPDFPath,Map<String,String> map)
{
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
for (Entry<String, String> entry : map.entrySet()) {
form.setField(entry.getKey(),entry.getValue());
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
return true;
} catch (IOException e) {
log.error("生成PDF异常:"+e.toString());
return false;
} catch (DocumentException e) {
log.error("生成PDF异常:"+e.toString());
return false;
} catch (Exception e) {
log.error("生成PDF异常:"+e.toString());
return false;
}
}
/**
* 生成PDF(html内容)
* @param newPDFPath 生成的新文件路径
* @param content pdf内容
* @return
*/
public boolean makePDF(String newPDFPath,String content)
{
Document document = new Document();
String html ="<html><body style='font-size:20px;font-family:SimSun;'>" +content + "</body></html>" ;
try {
OutputStream os = new FileOutputStream(newPDFPath);
PdfWriter writer = PdfWriter.getInstance(document,os);
document.open();
XMLWorkerHelper helper = XMLWorkerHelper.getInstance();
helper.parseXHtml(writer,document,
new ByteArrayInputStream(html.getBytes("UTF-8")),
null,
Charset.forName("UTF-8"),new AsianFontProvider());
document.close();
return true;
} catch (DocumentException e) {
document.close();
e.printStackTrace();
log.error("生成PDF异常1:"+e.toString());
return false;
} catch (FileNotFoundException e) {
document.close();
e.printStackTrace();
log.error("生成PDF异常1:"+e.toString());
return false;
}catch (Exception e) {
document.close();
e.printStackTrace();
log.error("生成PDF异常1:"+e.toString());
return false;
}
}
}
import com.itextpdf.text.Font;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
public class AsianFontProvider extends XMLWorkerFontProvider {
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) {
String fntname = fontname;
if(fntname == null)
fntname = "宋体"; //设置字体:解决不显示中文的问题
return super.getFont(fntname, encoding, size, style);
}
}