HTML轉(zhuǎn)PDF(java)

大家好,我是傻明蠶豆,最近搞了一個html轉(zhuǎn)pdf,在這里把知識記錄下來,希望對大家有幫助。

廢話不多說,直奔主題。首先把你想要生成的pdf內(nèi)容做成html模版,然后把數(shù)據(jù)導(dǎo)入到模版中,再生成一個pdf文件。

準備html如下圖所示:

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">我的個人簡介</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>來自廣東省某個鎮(zhèn)的一個小村莊里,我愛計算機,我愛...</p>

? ? <p>來自廣東省某個鎮(zhèn)的一個小村莊里,我愛JAVA,我愛...</p>

? ? <p>來自廣東省某個鎮(zhèn)的一個小村莊里,我愛Spring全家桶,我愛...</p>

? ? <p>注意:標簽必須閉合</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","團員");

? ? 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");? //這個一定要設(shè)置,不然在生成的頁面中 會亂碼

? ? configuration.setDirectoryForTemplateLoading(modelFile);

? ? //獲取或創(chuàng)建一個模版。

? ? Template template = configuration.getTemplate(htmlName);

? ? StringWriter stringWriter = new StringWriter();

? ? BufferedWriter writer = new BufferedWriter(stringWriter);

? ? template.process(paramMap, writer); //把值寫進模板

? ? 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;

? ? }

}

運行結(jié)果生成如下圖:

生成的pdf

注意:Html標簽必須閉合

這時候發(fā)現(xiàn)背景圖沒有,經(jīng)過一波操作,改css樣式、把背景圖放到table、設(shè)置浮動,結(jié)果還是不行。

希望有大神可以看到幫忙解決這個問題。

謝謝觀看,再見!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容