itextpdf結合jfinal模版生成pdf文件


jar使用的是紅色部分兩個,其餘的是其餘的加強jar,暫時沒有用到  

<!--生成pdf--> <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.11</version> </dependency> <!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker iText-Html渲染--> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.5.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker 5.iText-Html-Freemarker渲染--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.19</version> </dependency> <!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf 6.Flying Saucer-CSS高級特性支持--> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.1.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 6.Flying Saucer-CSS高級特性支持--> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf-itext5</artifactId> <version>9.1.5</version> </dependency> <!--pdf轉jpg --> <!-- https://mvnrepository.com/artifact/org.jpedal/jpedal-lgpl --> <!--<dependency> <groupId>org.jpedal</groupId> <artifactId>jpedal-lgpl</artifactId> <version>4.74b27</version> </dependency>--> <!--<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>--> <!-- https://mvnrepository.com/artifact/com.lowagie/itext --> <!--<dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency>--> <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>1.7</version> </dependency> <!--生成pdf-->

 

工具的結構html

 

這個結構能夠適用於不一樣的使用方式,這裏我寫了三種,後續有,還能夠繼承抽象類就能夠了,下邊把代碼貼出來,能夠參考看一下java

1.接口apache

import java.io.OutputStream; import java.util.Map; /** * 生成pdf的接口 * 生成pdf都須要把一個字符串寫成pdf文件輸出到指定的地方 */
public interface PDF { boolean writeToPDF(Map<String, Object> data, String htmlTmp, OutputStream out); }

2.抽象類ide

import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerFontProvider; import com.itextpdf.tool.xml.XMLWorkerHelper; import com.jfinal.kit.PathKit; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Map; public abstract class BasePDF implements PDF { //字體
    private static String FONT = PathKit.getWebRootPath() + "/js/font/simhei.ttf"; /** * 字體這裏設置了默認的,若是使用其餘字體須要倒入字體,而後經過這個方法設定字體 * @param FONT */
    public static void setFont(String FONT) { BasePDF.FONT = FONT; } @Override public boolean writeToPDF(Map<String, Object> data, String htmlTmp, OutputStream out) { String content = templateToString(data, htmlTmp); return write(content, out); } /** * 準備生成pdf */
    public boolean write(String content, OutputStream out) { // step 1
        Document document = new Document(); // step 2
        PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, out); } catch (DocumentException e) { e.printStackTrace(); } // step 3
 document.open(); // step 4
        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); fontImp.register(FONT); try { XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(content.getBytes()), null, Charset.forName("UTF-8"), fontImp); } catch (IOException e) { e.printStackTrace(); return false; } // step 5
 document.close(); return true; } /** * 要輸出的字符串模板 */
    public abstract String templateToString(Map<String, Object> data, String htmlTmp); }

3.jfinal實現類工具

import com.jfinal.template.Engine; import com.jfinal.template.Template; import java.util.Map; public class TemplateToPDFJfinal extends BasePDF { protected static Engine myEngine; /** * 系統啓動時初始化,在啓動文件中的設置 */
    public static void setEngine(Engine engine) { TemplateToPDFJfinal.myEngine = engine; } public static Engine getEngine() { return myEngine; } @Override public String templateToString(Map<String, Object> data, String htmlTmp) { if(myEngine == null){ throw new RuntimeException("Did not setEngine(Engine engine),You must setEngine(Engine engine) after using this tool"); } myEngine.setDevMode(true); myEngine.setEncoding("UTF-8"); Template template = myEngine.getTemplate(htmlTmp); return template.renderToString(data); } }

4.代理類測試

import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class PDFProxy<T> { T target; public PDFProxy(T target) { this.target = target; } public Object getInstance() { return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getSuperclass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object invoke = method.invoke(target, args); return invoke; } }); } }

5.工廠類字體

public class PDFFactory { public static PDF getPDF(Class<?> p) { PDF pdf = null; try { pdf = (PDF) p.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } PDFProxy pdfProxy = new PDFProxy(pdf); return (PDF) pdfProxy.getInstance(); } }

6.數據封裝類this

import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; public class pdfTest { public static final String DESTJfinal = "/pdfJfinal.pdf"; /** * 簡單寫個方法測試 生成pdf * * @param templateName 模板名稱位置 * @param outPath 輸出位置 * @param pdfClass 使用的是哪一個實現類 */
    public static void test(String templateName, String outPath, Class<?> pdfClass) { //設置字體 //BasePDF.setFont("/xx/xx.ttf");
 PDF pdf = PDFFactory.getPDF(pdfClass); Map<String, Object> m = new HashMap<>(); m.put("red", "紅色"); try { pdf.writeToPDF(m, templateName, new FileOutputStream(new File(outPath))); } catch (FileNotFoundException e) { e.printStackTrace(); } } }

7.使用spa

/** * 測試報告使用 * 生成這一週的報告 * /report/handleTestReport * 這個會把pdf生成到項目到根目錄下,根據需求本身調 */ @Clear public void handleTestReport() { pdfTest.test("/mail/a.html", PathKit.getWebRootPath() + pdfTest.DESTJfinal, TemplateToPDFJfinal.class); }

 

好了上邊是全部到類代理

下邊把基礎的輔助資料寫一下

字體:我把字體放到根目錄下的js下了,具體看圖片,根據本身的需求,我寫了設置字體的方法沒,能夠本身設置

模版文件:我放到了根目錄下的mail下了

內容是:

<!--要求比較嚴格,html中的標籤必須有開始結束標籤, 但標籤須要加 / 好比:<meta charset="UTF-8" /> -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>jfinal模板使用</title>
    <style> body {
       //比較重要的一點,若是這裏設置的字體是咱們後臺設置的不同的,是沒有的字體,那麼,中文就不會顯示了,還有就是標籤必定要按照規定寫,必定要有閉合標籤或者 < xxx /> 這樣字的
font-family: SimHei,serif; } .pos { position: absolute; left: 200px; top: 5px; width: 200px; font-size: 10px; } .table-one { width: 700px; text-align: center; border-collapse: collapse; } /*細調試*/ .title-big{ font-size: 20px; } .title-middle{ font-size: 16px; } .title-small{ font-size: 13.33px; } .red{ background-color: #E11B22; } .black{ background-color: black; color: white; } </style> </head> <body> <div class="pos"> <!--圖片--> <table class="table-one"> <tr> <td colspan="5"> <img alt="tp" style="width: 153px;margin-left: 300px" src="/Users/renjianjun/Downloads/鬥圖/mao.jpg"/> </td> </tr> </table> <table border="1" class="table-one"> <tr> <td colspan="5" class="title-big black">20px標題</td> </tr> <tr class="title-middle"> <td class="red" rowspan="3"> <table> <tr><td>#(red)這裏是取值,爲了實驗沒有多寫,只有這三個取值</td></tr> <tr><td>#(red)</td></tr> <tr><td>#(red)</td></tr> </table> </td> <td colspan="4">數據</td> </tr> <tr class="title-middle"> <!-- <td class="red">紅色</td>--> <td colspan="4">數據</td> </tr> <tr class="title-middle"> <!-- <td class="red">紅色</td>--> <td colspan="4">數據</td> </tr> <tr class="title-middle black"> <td colspan="5">16px二級標題</td> </tr> <tr class="title-small"> <td>13px標題</td> <td>13px數據</td> <td>13px數據</td> <td>13px數據</td> <td>13px數據</td> </tr> <tr class="title-small"> <td>13px標題</td> <td>13px數據</td> <td>13px數據</td> <td>13px數據</td> <td>13px數據</td> </tr> <tr class="title-small"> <td>13px標題</td> <td>13px數據</td> <td colspan="2">13px數據</td> <td>13px數據</td> </tr> <tr> <td class="title-middle">16px標題</td> <td colspan="4" class="title-small">13px數據</td> </tr> <tr> <td class="title-middle">16px標題</td> <td colspan="4" class="title-small">13px數據</td> </tr> <tr> <td class="title-middle">16px標題</td> <td colspan="4" class="title-small">13px數據</td> </tr> <tr> <td class="title-middle">16px標題</td> <td colspan="4" class="title-small">13px數據</td> </tr> <tr> <td class="title-middle">16px標題</td> <td colspan="4" class="title-small">13px數據</td> </tr> </table> </div> </body> </html>

好了這個是模版的內容,下邊看一下預覽效果:

 可能有些地方使用了代理,又使用了工廠模式,麻煩了,可是一個是本身練手,一個是這樣子擴展性仍是不錯的。使用起來也簡單。

相關文章
相關標籤/搜索