日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Lowagie 导出html的内容到 pdf

發(fā)布時(shí)間:2023/12/9 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lowagie 导出html的内容到 pdf 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用 com.Lowagie.itext 導(dǎo)出html的內(nèi)容到 pdf

? 學(xué)習(xí)了使用com.Lowagie.itext導(dǎo)出html的內(nèi)容到word,導(dǎo)出pdf的話(huà),會(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>&nbsp;</p>\n<p>啦啦</p>\n<p>&nbsp;</p>\n<p>&nbsp;</p>\n<p><img src=\"F://temp//back1.jpg\" alt=\"\" width=\"640\" height=\"640\" /></p>\n<p>&nbsp;</p>\n<p>發(fā)放</p>\n<p>&nbsp;</p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p>&nbsp;</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ì)顯示不出來(lái)。

結(jié)果:

出現(xiàn)的問(wèn)題:

?1,只出現(xiàn)了兩張圖片,沒(méi)出現(xiàn)字符;

?2,中文字符沒(méi)有展示

?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ù)類(lèi)型添加:

在輸出其類(lèi)型的時(shí)候,發(fā)現(xiàn)字符和圖片的是一樣的,表格的類(lèi)型不同,根據(jù)類(lèi)型不同,進(jìn)行添加

代碼:

private final static int FONT_NORMAL = Font.NORMAL;public static void main(String[] args) {String value = "<p>圖片導(dǎo)出</p>\n<p>&nbsp;</p>\n<p>啦啦</p>\n<p>&nbsp;</p>\n<p>&nbsp;</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>&nbsp;</p>\n<p>發(fā)放</p>\n<p>&nbsp;</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%;\">&nbsp;發(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%;\">&nbsp;發(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%;\">&nbsp;薩芬的</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>&nbsp;</p>\n<p><img src=\"F://temp//back2.jpg\" alt=\"\" width=\"300\" height=\"108\" /></p>\n<p>第三方</p>\n<p>&nbsp;</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)出來(lái)的內(nèi)容,只有表格,沒(méi)有實(shí)際的內(nèi)容。這個(gè)是老問(wèn)題了。對(duì)于表格的內(nèi)容,要拎出來(lái),然后放到PdfPTable里面,一個(gè)一個(gè)設(shè)置PdfCell。這個(gè)后面再添加。

總結(jié):

? ?使用 com.Lowagie.itext 導(dǎo)出html內(nèi)容到pdf上,就沒(méi)word那么簡(jiǎn)單,要根據(jù)實(shí)際的類(lèi)型一一處理, 要處理好中文。帶表格也得額外處理了,需要手動(dòng)拼接。后面試試其它的方式,看看有沒(méi)有不用特殊處理表格的。

總結(jié)

以上是生活随笔為你收集整理的Lowagie 导出html的内容到 pdf的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。