HTML轉(zhuǎn)PDF(java)

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

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

準(zhǔn)備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">我的個(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é)果生成如下圖:

生成的pdf

注意:Html標(biāo)簽必須閉合

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

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

謝謝觀看,再見廉油!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惠险,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子抒线,更是在濱河造成了極大的恐慌班巩,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嘶炭,死亡現(xiàn)場離奇詭異抱慌,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)眨猎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門抑进,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人睡陪,你說我怎么就攤上這事寺渗。” “怎么了兰迫?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵信殊,是天一觀的道長。 經(jīng)常有香客問我汁果,道長涡拘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任须鼎,我火速辦了婚禮鲸伴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘晋控。我一直安慰自己汞窗,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布赡译。 她就那樣靜靜地躺著仲吏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蝌焚。 梳的紋絲不亂的頭發(fā)上裹唆,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機(jī)與錄音只洒,去河邊找鬼许帐。 笑死,一個(gè)胖子當(dāng)著我的面吹牛毕谴,可吹牛的內(nèi)容都是我干的成畦。 我是一名探鬼主播距芬,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼循帐!你這毒婦竟也來了框仔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤拄养,失蹤者是張志新(化名)和其女友劉穎离斩,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瘪匿,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡跛梗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了柿顶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茄袖。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖嘁锯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情聂薪,我是刑警寧澤家乘,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站藏澳,受9級特大地震影響仁锯,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜翔悠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一业崖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蓄愁,春花似錦双炕、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至丹拯,卻和暖如春站超,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背乖酬。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工死相, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人咬像。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓算撮,卻偏偏與公主長得像双肤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子钮惠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354