項目中有一功能是導出歷史記錄,可以導出pdf和excel,這里先說導出pdf。在網上查可以用那些方式導出pdf,用itext比較多廣泛。
導出pdf可以使用兩種方式,一是可以根據已有的pdf模板,進行生成文檔。二是直接用代碼生成pdf
一、使用模板生成pdf
1、添加依賴
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2、創建word,創建需要的樣式,例如,保存為pdf格式,
3、使用Adobe Acrobat 打開,打開內容編輯,選擇編輯域,編輯域的名稱與代碼的數據屬性名對應。
4、java代碼
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import com.itextpdf.text.Document;
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;
/**
* @Title: CreatePdf.java
* @Description: TODO
* @author zhangjunhong
* @date 2018年10月22日
*/
public class CreatePdf {
public static void fillTemplate() throws Exception {
//讀取的模板
String templatePath = "D:/mypdf1.pdf";
//生成的pdf存儲的路徑
String targetPath = "D:/test1.pdf";
PdfReader reader;
FileOutputStream outputStream;
ByteArrayOutputStream bos;
PdfStamper stamper;
reader = new PdfReader(templatePath);
outputStream = new FileOutputStream(targetPath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
//取得模板表單對應的域
AcroFields from = stamper.getAcroFields();
String[] strings = { "12222", "zahang", "男", "1992-09-12" };
int i = 0;
Iterator<String> iterator = from.getFields().keySet().iterator();
while (iterator.hasNext()) {
String name = iterator.next().toString();
System.err.println(name);
from.setField(name, strings[i++]);
}
stamper.setFormFlattening(true);
stamper.close();
Document document = new Document();
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
PdfImportedPage importedPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importedPage);
document.close();
}
public static void main(String[] args) throws Exception{
fillTemplate();
}
}
5、結果
二、根據數據生成pdf並導出,這個好像挺簡單的,直接代碼一波,看注釋,也可以生成表格之類的
@RequestMapping("/export/pdf")
public void exPdf(HttpServletResponse response){
OutputStream os=null;
try {
// 指定解析器
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
String filename = "大面積延誤歷史記錄詳情.pdf";
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"attachment;fileName=" + URLEncoder.encode(filename, "UTF-8"));
os = new BufferedOutputStream(response.getOutputStream());
//生成pdf
Document document=new Document();
PdfWriter writer=PdfWriter.getInstance(document, os);
// 頁面大小
Rectangle rectangle = new Rectangle(PageSize.A4);
// 頁面背景顏色
rectangle.setBackgroundColor(BaseColor.WHITE);
document.setPageSize(rectangle);
// 頁邊距 左,右,上,下
document.setMargins(20, 20, 20, 20);
document.open();
//中文字體 ----不然中文會亂碼
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//設置字體
Font font = new Font(bf, 14, Font.BOLD, BaseColor.BLACK);
Paragraph p=new Paragraph("設置了字體樣式的標題哈哈哈哈哈今天比較閑嚶嚶嚶", font);
document.add(p);
document.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
結果:訪問路徑http://127.0.0.1:8080/export/pdf,下載下來的pdf文檔
還有很多對pdf的操作,如添加page,表格等,可查看itext的官方文檔,
或者看這個博客:https://blog.csdn.net/weixin_36380516/article/details/76984283
最后是不是貼上我花了幾天寫的pdf導出,這個技術是簡單了,尼瑪項目業務邏輯賊復雜。周五任務完成截止日期,昨天寫完啦,等前端對接接口,,就寫寫博客,算了,先不貼代碼。我再整理整理