大家好,我是傻明蠶豆,最近搞了一個(gè)html轉(zhuǎn)pdf按厘,在這里把知識記錄下來盈电,希望對大家有幫助。
廢話不多說啊研,直奔主題。首先把你想要生成的pdf內(nèi)容做成html模版,然后把數(shù)據(jù)導(dǎo)入到模版中恍飘,再生成一個(gè)pdf文件。
準(zhǔn)備html如下圖所示:
html代碼:
<html>
<head>
? ? <link rel="stylesheet" href="F:/read/jianli/jianli.css"></link>
</head>
<body >
<div id="div1" style="background-image:url('F:/read/jianli/images/t01ed5a885549b6bf3b.jpg');">
<table border="1" width="100%" height="100%" align="center" valign="middle" align="center" cellpadding="10">
<tr>
? ? <th colspan="7">我的個(gè)人簡介</th>
</tr>
<tr>
? ? <th>姓名:</th>
? ? <td>${name}</td>
? ? <th>年齡:</th>
? ? <td>${age}</td>
? ? <th>民族:</th>
? ? <td>${nation}</td>
? ? <td rowspan="3" width="100"><img src="F:/read/jianli/images/small.png"/></td>
</tr>
<tr>
? ? <th >出生日期:</th>
? ? <td>${birthday}</td>
? ? <th >政治面貌:</th>
? ? <td>${politicsVisage}</td>
? ? <th >學(xué)歷:</th>
? ? <td>${educationBackground}</td>
</tr>
<tr>
? ? <th >專業(yè):</th>
? ? <td>${major}</td>
? ? <th >畢業(yè)學(xué)校:</th>
? ? <td>${school}</td>
? ? <th >郵編:</th>
? ? <td>${postcode}</td>
</tr>
<tr>
? ? <th >愛好:</th>
? ? <td>${hobby}</td>
? ? <th >籍貫:</th>
? ? <td>${nativePlace}</td>
? ? <th>郵箱:</th>
? ? <td colspan="2">${email}</td>
</tr>
<tr>
? ? <th height="160"> 自我介紹:</th>
? ? <td colspan="6">
? ? <p>來自廣東省某個(gè)鎮(zhèn)的一個(gè)小村莊里谴垫,我愛計(jì)算機(jī)章母,我愛...</p>
? ? <p>來自廣東省某個(gè)鎮(zhèn)的一個(gè)小村莊里,我愛JAVA翩剪,我愛...</p>
? ? <p>來自廣東省某個(gè)鎮(zhèn)的一個(gè)小村莊里乳怎,我愛Spring全家桶,我愛...</p>
? ? <p>注意:標(biāo)簽必須閉合</p>
? ? </td>
</tr>
</table>
</div>
</body>
</html>
css代碼:
th{
background-color: BurlyWood;
}
maven依賴:
<dependency>
? <groupId>org.apache.pdfbox</groupId>
? <artifactId>pdfbox-tools</artifactId>
? <version>2.0.11</version>
</dependency>
<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>
<dependency>
? <groupId>org.freemarker</groupId>
? <artifactId>freemarker</artifactId>
? <version>2.3.26-incubating</version>
</dependency>
<dependency>
? <groupId>jfree</groupId>
? <artifactId>jfreechart</artifactId>
? <version>1.0.2</version>
</dependency>
java代碼:
private static void test(){
? ? Map<String ,Object> map=new HashMap<>();
? ? map.put("name","傻明蠶豆");
? ? map.put("age","18");
? ? map.put("nation","漢族");
? ? map.put("birthday","19810808");
? ? map.put("politicsVisage","團(tuán)員");
? ? map.put("educationBackground","大學(xué)");
? ? map.put("major","電子技術(shù)");
? ? map.put("school","青山大學(xué)");
? ? map.put("postcode","528143");
? ? map.put("hobby","吃喝拉撒");
? ? map.put("nativePlace","粵");
? ? map.put("email","1060120317@qq.com");
? ? File modelFile = new File("F:/read/jianli");
? ? String htmlName = "jianli.html";
? ? String cssPath = "F:/read/jianli/jianli.css";
? ? String pdfPath ="F:/read/jianli/"+System.currentTimeMillis()+".pdf";
? ? try {
? ? ? ? createPDF(modelFile,htmlName, cssPath, pdfPath, map);
? ? ? ? System.out.println("success");
? ? }catch (Exception e){
? ? ? ? e.printStackTrace();
? ? }
}
private static void createPDF(File modelFile,String htmlName,String cssPath,String pdfPath,Map<String, Object> paramMap) throws Exception{
? ? Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
? ? configuration.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
? ? configuration.setDefaultEncoding("UTF-8");? //這個(gè)一定要設(shè)置前弯,不然在生成的頁面中 會亂碼
? ? configuration.setDirectoryForTemplateLoading(modelFile);
? ? //獲取或創(chuàng)建一個(gè)模版蚪缀。
? ? Template template = configuration.getTemplate(htmlName);
? ? StringWriter stringWriter = new StringWriter();
? ? BufferedWriter writer = new BufferedWriter(stringWriter);
? ? template.process(paramMap, writer); //把值寫進(jìn)模板
? ? String htmlStr = stringWriter.toString();
? ? writer.flush();
? ? writer.close();
? ? FileOutputStream outFile = new FileOutputStream(pdfPath);
? ? InputStream cssInputStream = new FileInputStream(cssPath);
? ? ByteArrayInputStream htmlInputStream = new ByteArrayInputStream(htmlStr.getBytes("UTF-8"));
? ? Document document = new Document();
? ? try {
? ? ? ? PdfWriter pdfWriter = PdfWriter.getInstance(document, outFile);
? ? ? ? document.open();
? ? ? ? FontProvider provider = new FontProvider();
? ? ? ? XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, htmlInputStream, cssInputStream, Charset.forName("UTF-8"),provider);
? ? } catch (Exception e) {
? ? ? ? e.printStackTrace();
? ? ? ? throw e;
? ? }finally {
? ? ? ? try {
? ? ? ? ? ? document.close();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? htmlInputStream.close();
? ? ? ? ? ? cssInputStream.close();
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
}
}
/**
* 字體
*/
private static class FontProvider extends XMLWorkerFontProvider {
? ? public Font getFont(final String fontname, final String encoding, final boolean embedded, final float size, final int style, final BaseColor color) {
? ? ? ? BaseFont bf = null;
? ? ? ? try {
? ? ? ? ? ? bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? Font font = new Font(bf, size, style, color);
? ? ? ? font.setColor(color);
? ? ? ? return font;
? ? }
}
運(yùn)行結(jié)果生成如下圖:
注意:Html標(biāo)簽必須閉合
這時(shí)候發(fā)現(xiàn)背景圖沒有,經(jīng)過一波操作博杖,改css樣式椿胯、把背景圖放到table、設(shè)置浮動剃根,結(jié)果還是不行哩盲。
希望有大神可以看到幫忙解決這個(gè)問題。
謝謝觀看,再見廉油!