国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java根據模板動態生成PDF實例

java根據模板動態生成PDF實例

2020-09-20 14:16Enlightenment Java教程

本篇文章主要介紹了java根據模板動態生成PDF實例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

一、需求說明:

根據業務需要,需要在服務器端生成可動態配置的PDF文檔,方便數據可視化查看。

二、解決方案:

iText+FreeMarker+JFreeChart生成可動態配置的PDF文檔

iText有很強大的PDF處理能力,但是樣式和排版不好控制,直接寫PDF文檔,數據的動態渲染很麻煩。

FreeMarker能配置動態的html模板,正好解決了樣式、動態渲染和排版問題。

JFreeChart有這方便的畫圖API,能畫出簡單的折線、柱狀和餅圖,基本能滿足需要。

三、實現功能:

1、能動態配置PDF文檔內容

2、能動態配置中文字體顯示

3、設置自定義的頁眉頁腳信息

4、能動態生成業務圖片

5、完成PDF的分頁和圖片的嵌入

四、主要代碼結構說明:

1、component包:PDF生成的組件 對外提供的是PDFKit工具類和HeaderFooterBuilder接口,其中PDFKit負責PDF的生成,HeaderFooterBuilder負責自定義頁眉頁腳信息。

2、builder包:負責PDF模板之外的額外信息填寫,這里主要是頁眉頁腳的定制。

3、chart包:JFreeChart的畫圖工具包,目前只有一個線形圖。

4、test包:測試工具類

5、util包:FreeMarker等工具類。

五、關鍵代碼說明:

1、模板配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  <meta http-equiv="Content-Style-Type" content="text/css"/>
  <title></title>
  <style type="text/css">
    body {
      font-family: pingfang sc light;
    }
    .center{
      text-align: center;
      width: 100%;
    }
  </style>
</head>
<body>
<!--第一頁開始-->
<div class="page" >
  <div class="center"><p>${templateName}</p></div>
  <div><p>iText官網:${ITEXTUrl}</p></div>
  <div><p>FreeMarker官網:${freeMarkerUrl}</p></div>
  <div><p>JFreeChart教程:${JFreeChartUrl}</p></div>
  <div>列表值:</div>
  <div>
    <#list scores as item>
      <div><p>${item}</p></div>
    </#list>
  </div>
</div>
<!--第一頁結束-->
<!---分頁標記-->
<span style="page-break-after:always;"></span>
<!--第二頁開始-->
<div class="page">
  <div>第二頁開始了</div>
  <!--外部鏈接-->
  <p>百度圖標</p>
  <div>
    <img src="${imageUrl}" alt="百度圖標" width="270" height="129"/>
  </div>
  <!--動態生成的圖片-->
  <p>氣溫變化對比圖</p>
  <div>
    <img src="${picUrl}" alt="我的圖片" width="500" height="270"/>
  </div>
</div>
 
 
<!--第二頁結束-->
</body>
</html>

2、獲取模板內容并填充數據

java" id="highlighter_367478">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * @description 獲取模板
 */
public static String getContent(String fileName,Object data){
 
  String templatePath=getPDFTemplatePath(fileName);//根據PDF名稱查找對應的模板名稱
  String templateFileName=getTemplateName(templatePath);
  String templateFilePath=getTemplatePath(templatePath);
  if(StringUtils.isEmpty(templatePath)){
    throw new FreeMarkerException("templatePath can not be empty!");
  }
  try{
    Configuration config = new Configuration(Configuration.VERSION_2_3_25);//FreeMarker配置
    config.setDefaultEncoding("UTF-8");
    config.setDirectoryForTemplateLoading(new File(templateFilePath));//注意這里是模板所在文件夾,不是文件
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    config.setLogTemplateExceptions(false);
    Template template = config.getTemplate(templateFileName);//根據模板名稱 獲取對應模板
    StringWriter writer = new StringWriter();
    template.process(data, writer);//模板和數據的匹配
    writer.flush();
    String html = writer.toString();
    return html;
  }catch (Exception ex){
    throw new FreeMarkerException("FreeMarkerUtil process fail",ex);
  }
}

3、導出模板到PDF文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
   * @description   導出pdf到文件
   * @param fileName 輸出PDF文件名
   * @param data   模板所需要的數據
   *
   */
public String exportToFile(String fileName,Object data){
   String htmlData= FreeMarkerUtil.getContent(fileName, data);//獲取FreeMarker的模板數據
  if(StringUtils.isEmpty(saveFilePath)){
    saveFilePath=getDefaultSavePath(fileName);//設置PDF文件輸出路徑
  }
  File file=new File(saveFilePath);
  if(!file.getParentFile().exists()){
    file.getParentFile().mkdirs();
  }
  FileOutputStream outputStream=null;
  try{
    //設置輸出路徑
    outputStream=new FileOutputStream(saveFilePath);
    //設置文檔大小
    Document document = new Document(PageSize.A4);//IText新建PDF文檔
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);//設置文檔和輸出流的關系
 
    //設置頁眉頁腳
    PDFBuilder builder = new PDFBuilder(headerFooterBuilder,data);
    builder.setPresentFontSize(10);
    writer.setPageEvent(builder);
 
    //輸出為PDF文件
    convertToPDF(writer,document,htmlData);
  }catch(Exception ex){
    throw new PDFException("PDF export to File fail",ex);
  }finally{
    IOUtils.closeQuietly(outputStream);
  }
  return saveFilePath;
 
}

4、測試工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public String createPDF(Object data, String fileName){
     //pdf保存路徑
     try {
       //設置自定義PDF頁眉頁腳工具類
       PDFHeaderFooter headerFooter=new PDFHeaderFooter();
       PDFKit kit=new PDFKit();
       kit.setHeaderFooterBuilder(headerFooter);
       //設置輸出路徑
       kit.setSaveFilePath("/Users/fgm/Desktop/pdf/hello.pdf”);//設置出書路徑
       String saveFilePath=kit.exportToFile(fileName,data);
       return saveFilePath;
     } catch (Exception e) {
       log.error("PDF生成失敗{}", ExceptionUtils.getFullStackTrace(e));
       return null;
     }
   
   }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void main(String[] args) {
    ReportKit360 kit=new ReportKit360();
       TemplateBO templateBO=new TemplateBO();//配置模板數據
       templateBO.setTemplateName("Hello iText! Hello freemarker! Hello jFreeChart!");
       templateBO.setFreeMarkerUrl("http://www.zheng-hang.com/chm/freemarker2_3_24/ref_directive_if.html");
       templateBO.setITEXTUrl("http://developers.itextpdf.com/examples-itext5");
 
 templateBO.setJFreeChartUrl("http://www.yiibai.com/jfreechart/jfreechart_referenced_apis.html");
   templateBO.setImageUrl("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
   
   
      List<String> scores=new ArrayList<String>();
       scores.add("90");
       scores.add("95");
       scores.add("98");
       templateBO.setScores(scores);
       List<Line> lineList=getTemperatureLineList();
       TemperatureLineChart lineChart=new TemperatureLineChart();
       String picUrl=lineChart.draw(lineList,0);//自定義的數據畫圖
       templateBO.setPicUrl(picUrl);
       String path= kit.createPDF(templateBO,"hello.pdf");
     System.out.println(path);
   
   }

六、生成效果圖:

java根據模板動態生成PDF實例

七、項目完整代碼

1、github地址:https://github.com/superad/pdf-kit

八、遇到的坑:

1、FreeMarker配置模板文件樣式,在實際PDF生成過程中,可能會出現一些不一致的情形,目前解決方法,就是換種方式調整樣式。

2、字體文件放在resource下,在打包時會報錯,運行mvn -X compile 會看到詳細錯誤:

這是字體文件是二進制的,而maven項目中配置了資源文件的過濾,不能識別二進制文件導致的,plugins中增加下面這個配置就好了:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
  <!--增加的配置,過濾ttf文件的匹配-->
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.7</version>
      <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
          <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
      </configuration>
    </plugin>
  </plugins>
</build>

3、PDF分頁配置:

在ftl文件中,增加分頁標簽: <span style="page-break-after:always;"></span>

九、 完整maven配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!--pdf生成 itext-->
 
 <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.4.2</version>
   </dependency>
 <dependency>
  <groupId>com.itextpdf.tool</groupId>
  <artifactId>xmlworker</artifactId>
  <version>5.4.1</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itext-asian</artifactId>
  <version>5.2.0</version>
</dependency>
<dependency>
  <groupId>org.xhtmlrenderer</groupId>
  <artifactId>flying-saucer-pdf</artifactId>
  <version>9.0.3</version>
</dependency>
<!--freemarker-->
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.26-incubating</version>
</dependency>
<!--jfreechart-->
<dependency>
  <groupId>jfreechart</groupId>
  <artifactId>jfreechart</artifactId>
  <version>1.0.0</version>
</dependency>
<!--log-->
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-core</artifactId>
  <version>1.0.13</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.0.13</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-access</artifactId>
  <version>1.0.13</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.5</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>log4j-over-slf4j</artifactId>
  <version>1.7.21</version>
</dependency>
<!--util-->
<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>20.0</version>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.14.8</version>
</dependency>
<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-io</artifactId>
  <version>1.3.2</version>
</dependency>
<dependency>
  <groupId>commons-lang</groupId>
  <artifactId>commons-lang</artifactId>
  <version>2.6</version>
</dependency>
<!--servlet-->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
</dependency>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000009160184

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 色吊丝在线永久观看最新版本 | 91九色在线 | 天天干天天操 | 久久精品夜夜夜夜夜久久 | 四虎永久免费影视 | 欧美日韩久久久 | 亚洲视频1 | 亚洲精品永久视频 | 国产免费一区二区三区 | 免费成人黄色大片 | 亚洲视频一区二区三区在线观看 | 日本 欧美 国产 | 一级毛片免费网站 | 337p日本粉嫩噜噜噜 | 久久99精品久久久久蜜臀 | 成人在线视频免费 | 天天爽天天操 | 中日韩免费视频 | 国产大片在线观看 | 综州合另类 | 亚洲精品久久久久久下一站 | 国产精品免费久久久久久久久久中文 | 蜜桃av噜噜一区二区三区小说 | 99综合| 巴西性猛交xxxx免费看久久久 | 99热在线播放 | 亚洲精品在线观看av | 日韩精品久久久 | 中文字幕视频在线 | 日韩精品一区二区三区中文字幕 | 欧洲一区二区三区 | 日本1区2区 | 综合激情网 | 成人看的免费视频 | 日韩一区二区在线观看 | 色在线看| 亚洲欧美视频在线观看 | 久久亚洲一区二区三区四区 | 韩日一区| 成人av电影在线观看 | 久久久性色精品国产免费观看 |