通過html生成pdf捉撮,可使用睿印 工具包栋艳,睿印java工具包是集成openhtmltopdf抓谴,pdfbox的開源工具包钧椰,集成了thymeleaf 動(dòng)態(tài)腳本蛛壳,并可自動(dòng)加載字體杏瞻,默認(rèn)支持中文顯示。
- 首先新建java maven項(xiàng)目衙荐,引入jar包
<dependency>
<groupId>ink.rayin</groupId>
<artifactId>rayin-htmladapter-openhtmltopdf</artifactId>
<version>1.0.8</version>
</dependency>
- 新建html捞挥,保存至test/resources下,命名為element1.html
<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<style>
body {
font-family: FangSong,HanaMinB;
line-height: 1.2;
/*設(shè)置背景色*/
/*background: #00FF00 ;*/
/*設(shè)置背景圖片*/
/*background-image:url(data:image/gif;base64,AAAA) no-repeat fixed top;*/
}
/** 指定pdf紙張大小 **/
@page {
size: A4 ;
margin: 1cm;
/*margin-bottom: 1cm;*/
/*border: thin solid black;*/
}
.page_break { page-break-after:always;}
.seclev2 {
text-indent:20pt;
}
.seclev2c {
text-indent:40pt;
}
a {
text-decoration:none;
}
a:link {
color:#000000;
}
a:visited {color:#00FF00;}
</style>
</head>
<body>
<!-- 該部分為書簽生成使用到的tag href #后的名稱對(duì)應(yīng)的下方的id 名稱-->
<bookmarks>
<bookmark name="第一章" href="#section1">
<bookmark name="1.1 xxx" href="#subsec11"/>
<bookmark name="1.2 xxx" href="#subsec12"/>
<bookmark name="1.3 xxx" href="#subsec13"/>
</bookmark>
<bookmark name="第二章" href="#section2">
<bookmark name="2.1 xxxx" href="#subsec21"/>
<bookmark name="2.2 xxxx" href="#subsec22"/>
<bookmark name="2.3 xxx" href="#subsec23"/>
</bookmark>
<bookmark name="第三章" href="#section3">
<bookmark name="3.1 xxx" href="#subsec31"/>
<bookmark name="3.2 xxx" href="#subsec32"/>
<bookmark name="3.3 xxx" href="#subsec33"/>
</bookmark>
</bookmarks>
<div style="page-break-after:always;"></div>
<div id="section1" class="seclev1"><h1>第一章</h1></div>
<div id="subsec11" class="seclev2"><h2>1.1 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div id="subsec12" class="seclev2"><h2>1.2 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div id="subsec13" class="seclev2"><h2>1.3 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div style="page-break-after:always;"></div>
<div id="section2" ><h1>第二章</h1></div>
<div id="subsec21" class="seclev2"><h2>2.1 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div id="subsec22" class="seclev2"><h2>2.2 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div id="subsec23" class="seclev2"><h2>2.3 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div style="page-break-after:always;"></div>
<div id="section3"><h1>第三章</h1></div>
<div id="subsec31" class="seclev2"><h2>3.1 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div id="subsec32" class="seclev2"><h2>3.2 xxxx</h2>
<div>內(nèi)容</div>
</div>
<div id="subsec33" class="seclev2"><h2>3.3 xxxx</h2>
<div>內(nèi)容</div>
</div>
</body>
</html>
- 項(xiàng)目test中新建測試類
@Slf4j
public class PDFGeneratorOpenhtmltopdfTest {
static PdfGenerator pdfGenerator;
static {
try {
pdfGenerator = new PdfBoxGenerator();
pdfGenerator.init();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void markbooksTest() throws Exception {
log.info("markbooksTest start time:" + new Timestamp(System.currentTimeMillis()));
String outputFileClass = ResourceUtil.getResourceAbsolutePathByClassPath("");
// 生成pdf路徑
// generate pdf path
String outputFile = new File(outputFileClass)
.getParentFile().getParent()
+ "/tmp/"
+ "markbooks_"+System.currentTimeMillis() + ".pdf";
//數(shù)據(jù)參數(shù)可以為空
pdfGenerator.generatePdfFileByHtmlAndData(ResourceUtil.getResourceAbsolutePathByClassPath("element1.html"),null,outputFile);
log.info("markbooksTest end time:" + new Timestamp(System.currentTimeMillis()));
}
}
-
生成PDF如下
至此大功告成赫模!
pdfbox讀取pdf書簽
生成的pdf標(biāo)簽也可以通過pdfbox來讀取树肃。
@Test
public void bookmarksTest() throws IOException {
PDDocument pdfWithBookmarks = PDDocument.load(ResourceUtil.getResourceAsStream("bookmark.pdf"));
PDDocumentOutline pdo = pdfWithBookmarks.getDocumentCatalog().getDocumentOutline();
printBookmark(pdfWithBookmarks, pdo, "");
pdfWithBookmarks.close();
}
//遞歸獲取書簽
public void printBookmark(PDDocument pd, PDOutlineNode bookmark, String indentation) throws IOException{
PDOutlineItem current = bookmark.getFirstChild();
while (current != null) {
PDPage currentPage = current.findDestinationPage(pd);
// 獲取書簽對(duì)應(yīng)對(duì)的頁碼
Integer pageNumber = pd.getDocumentCatalog().getPages().indexOf(currentPage) + 1;
System.out.println(indentation + current.getTitle() + "----------------" + pageNumber);
printBookmark(pd, current, indentation + " ");
current = current.getNextSibling();
}
}