Lowagie 导出html的内容到 pdf
使用 com.Lowagie.itext 導(dǎo)出html的內(nèi)容到 pdf
? 學(xué)習(xí)了使用com.Lowagie.itext導(dǎo)出html的內(nèi)容到word,導(dǎo)出pdf的話,會(huì)是什么情況?可以直接用嗎? 有什么不同呢?
處理:
引入itext:
<dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.0.8</version> </dependency>直接引用:
public static void main(String[] args) throws Exception {OutputStream out = new FileOutputStream("d://exportFile//f"+System.currentTimeMillis()+".pdf");Document document = new Document(PageSize.A4);PdfWriter.getInstance(document, out);document.open();String content = "<p>圖片導(dǎo)出</p>\n<p> </p>\n<p>啦啦</p>\n<p> </p>\n<p> </p>\n<p><img src=\"F://temp//back1.jpg\" alt=\"\" width=\"640\" height=\"640\" /></p>\n<p> </p>\n<p>發(fā)放</p>\n<p> </p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p> </p>";StyleSheet styleSheet = new StyleSheet();List htmlList = HTMLWorker.parseToList(new StringReader(content), styleSheet);Paragraph context = new Paragraph();for (Object aHtmlList : htmlList) {Element e = (Element) aHtmlList;context.add(e);}document.add(context);document.close();System.out.println("ok"); }PS: 字符里面的圖片,根據(jù)實(shí)際的進(jìn)行替換。大小要合適,不然會(huì)顯示不出來。
結(jié)果:
出現(xiàn)的問題:
?1,只出現(xiàn)了兩張圖片,沒出現(xiàn)字符;
?2,中文字符沒有展示
?3,展示的內(nèi)容出現(xiàn)重疊
處理中文:
BaseFont bfChinese = BaseFont.createFont(ResourceUtils.getFile("classpath:static/Fonts/simfang.ttf").getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);在resources下,添加對(duì)應(yīng)的fonts,fonts文件下載
根據(jù)類型添加:
在輸出其類型的時(shí)候,發(fā)現(xiàn)字符和圖片的是一樣的,表格的類型不同,根據(jù)類型不同,進(jìn)行添加
代碼:
private final static int FONT_NORMAL = Font.NORMAL;public static void main(String[] args) {String value = "<p>圖片導(dǎo)出</p>\n<p> </p>\n<p>啦啦</p>\n<p> </p>\n<p> </p>\n<p><img src=\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2F20170105%2Fb01b-fxzkfuk2266724.png&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1623917122&t=0eef227b3c5f25073c174523fd5410c7\" alt=\"\" width=\"640\" height=\"640\" /></p>\n<p> </p>\n<p>發(fā)放</p>\n<p> </p>\n<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n<tbody>\n<tr>\n<td style=\"width: 16.6667%;\">大豐收</td>\n<td style=\"width: 16.6667%;\">阿凡達(dá)</td>\n<td style=\"width: 16.6667%;\">執(zhí)行</td>\n<td style=\"width: 16.6667%;\">打算</td>\n<td style=\"width: 16.6667%;\">阿道夫</td>\n<td style=\"width: 16.6667%;\">展旭</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">安撫</td>\n<td style=\"width: 16.6667%;\">啊</td>\n<td style=\"width: 16.6667%;\">的</td>\n<td style=\"width: 16.6667%;\">大</td>\n<td style=\"width: 16.6667%;\"> 發(fā)</td>\n<td style=\"width: 16.6667%;\">發(fā)送到</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">阿道夫</td>\n<td style=\"width: 16.6667%;\">ad</td>\n<td style=\"width: 16.6667%;\">答復(fù)</td>\n<td style=\"width: 16.6667%;\">阿斯蒂芬</td>\n<td style=\"width: 16.6667%;\"> 發(fā)多少</td>\n<td style=\"width: 16.6667%;\">發(fā)的</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">啊</td>\n<td style=\"width: 16.6667%;\">的</td>\n<td style=\"width: 16.6667%;\">更舒服的</td>\n<td style=\"width: 16.6667%;\">國(guó)防生的</td>\n<td style=\"width: 16.6667%;\"> 薩芬的</td>\n<td style=\"width: 16.6667%;\">阿斯蒂芬</td>\n</tr>\n<tr>\n<td style=\"width: 16.6667%;\">啊</td>\n<td style=\"width: 16.6667%;\">是大法官</td>\n<td style=\"width: 16.6667%;\">十多個(gè)</td>\n<td style=\"width: 16.6667%;\">殺伐果斷</td>\n<td style=\"width: 16.6667%;\">殺伐果斷</td>\n<td style=\"width: 16.6667%;\">用復(fù)合弓的</td>\n</tr>\n</tbody>\n</table>\n<p> </p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p> </p>";htmlToPdf(value, "d://exportFile//g"+System.currentTimeMillis()+".pdf"); }public static void htmlToPdf(String content, String pdfPath) {Document document = new Document();try {BaseFont bfChinese = BaseFont.createFont(ResourceUtils.getFile("classpath:static/Fonts/simfang.ttf").getAbsolutePath(),BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);StyleSheet styleSheet = new StyleSheet();styleSheet.loadTagStyle("body", "leading", "16,0");PdfWriter.getInstance(document, new FileOutputStream(pdfPath));document.open();List htmlList = HTMLWorker.parseToList(new StringReader(content), styleSheet);Paragraph context = new Paragraph();context.setKeepTogether(false);Font FontChinese = new Font(bfChinese, 10, FONT_NORMAL);for (Object aHtmlList : htmlList) {Element e = (Element) aHtmlList;System.out.println(e.type() + e.getChunks().toString() + ": "+ e.toString());int type = e.type();if(type == 12){ // 文本和圖片List chunks = e.getChunks();if(CollectionUtils.isEmpty(chunks)){continue;}for (Object object : chunks) {Chunk chunk = (Chunk) object;HashMap attributes = chunk.getAttributes();if (MapUtils.isEmpty(attributes)) {context = addParagraph(chunk.getContent());context.setIndentationLeft(50);context.setIndentationRight(50);} else {context = addParagraph("");context.setIndentationLeft(50);Image image = chunk.getImage();context.add(image);}document.add(context);}}else if( type == 23){ // 表格PdfPTable table = (PdfPTable) aHtmlList;document.add(table);}}document.close();System.out.println("ok");}catch(Exception e) {e.printStackTrace();} }??這邊表格導(dǎo)出來的內(nèi)容,只有表格,沒有實(shí)際的內(nèi)容。這個(gè)是老問題了。對(duì)于表格的內(nèi)容,要拎出來,然后放到PdfPTable里面,一個(gè)一個(gè)設(shè)置PdfCell。這個(gè)后面再添加。
總結(jié):
? ?使用 com.Lowagie.itext 導(dǎo)出html內(nèi)容到pdf上,就沒word那么簡(jiǎn)單,要根據(jù)實(shí)際的類型一一處理, 要處理好中文。帶表格也得額外處理了,需要手動(dòng)拼接。后面試試其它的方式,看看有沒有不用特殊處理表格的。
總結(jié)
以上是生活随笔為你收集整理的Lowagie 导出html的内容到 pdf的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 从零开始学_JavaScript_系列(
- 下一篇: 字节对齐测试实例