文介紹如何通過Java后端程序代碼來展示如何將html轉(zhuǎn)為XML。此功能通過采用Word API- Free Spire.Doc for Java 提供的Document.saveToFile()方法來實(shí)現(xiàn);該方法支持的目標(biāo)文檔格式多達(dá) 30余種。另外,該API也提供了多種方法,如Document.saveToEpub()、Document.saveToFile()、Document.saveToImages()、Document.saveToSVG()、Document.saveToTxt()、Document.saveToTiff()等,可用于將源文件轉(zhuǎn)為Epub、圖片、文本文件等目標(biāo)文檔格式。
下面,將以html轉(zhuǎn)為xml格式為例,介紹如何實(shí)現(xiàn)轉(zhuǎn)換。以下是詳細(xì)方法及步驟。
通過 Maven倉庫 下載導(dǎo)入,如下配置pom.xml:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
如需手動(dòng)導(dǎo)入,需要下載 jar包 到本地,然后解壓,找到lib文件夾下的Spire.Doc.jar文件。在IDEA中打開“Project Structure”界面,執(zhí)行如圖步驟將本地路徑下的jar文件手動(dòng)引入Java程序:
轉(zhuǎn)換時(shí),可參考如下代碼步驟:
Java
import com.spire.doc.*;
public class HTMLtoXML {
public static void main(String[] args) {
//創(chuàng)建Document類的對(duì)象
Document doc=new Document();
//加載html文件
doc.loadFromFile("sample.html",FileFormat.Html);
//保存為XML格式
doc.saveToFile("HTMLtoXML.xml", FileFormat.Xml);
}
}
轉(zhuǎn)換效果:
—END—
原文出處:https://www.cnblogs.com/Yesi/p/16392430.html
如果本文對(duì)你有幫助,別忘記給我個(gè)3連 ,點(diǎn)贊,轉(zhuǎn)發(fā),評(píng)論,
學(xué)習(xí)更多JAVA知識(shí)與技巧,關(guān)注與私信博主(555),即可免費(fèi)領(lǐng)取
html轉(zhuǎn)為pdf的組件有很多,但是還沒有哪一款能達(dá)到這個(gè)效果,其只要原因是wkhtmltopdf使用webkit網(wǎng)頁渲染引擎開發(fā)的用來將 html轉(zhuǎn)成 pdf的工具,可以跟多種腳本語言進(jìn)行集成來轉(zhuǎn)換文檔。但是就使用簡(jiǎn)便性來說還是itext等組件占據(jù)優(yōu)勢(shì),如果你要轉(zhuǎn)換格式有比較高的要求,那么wkhtmltopdf絕對(duì)是不二之選!
下載路徑
官網(wǎng)地址 wkhtmltopdf.org/
github地址 github.com/wkhtmltopdf…
使用方法
java調(diào)用demo
public class HtmlToPdfInterceptor extends Thread { private InputStream is; public HtmlToPdfInterceptor(InputStream is){ this.is=is; } public void run(){ try{ InputStreamReader isr=new InputStreamReader(is, "utf-8"); BufferedReader br=new BufferedReader(isr); String line=null; while ((line=br.readLine()) !=null) { System.out.println(line.toString()); //輸出內(nèi)容 } }catch (IOException e){ e.printStackTrace(); } }}public class HtmlToPdf { //wkhtmltopdf在系統(tǒng)中的路徑 private static final String toPdfTool="D:\wkhtmltopdf\bin\wkhtmltopdf.exe"; /** * html轉(zhuǎn)pdf * @param srcPath html路徑,可以是硬盤上的路徑,也可以是網(wǎng)絡(luò)路徑 * @param destPath pdf保存路徑 * @return 轉(zhuǎn)換成功返回true */ public static boolean convert(String srcPath, String destPath){ File file=new File(destPath); File parent=file.getParentFile(); //如果pdf保存路徑不存在,則創(chuàng)建路徑 if(!parent.exists()){ parent.mkdirs(); } StringBuilder cmd=new StringBuilder(); cmd.append(toPdfTool); cmd.append(" "); cmd.append(" --header-line");//頁眉下面的線 cmd.append(" --header-center 這里是頁眉這里是頁眉這里是頁眉這里是頁眉 ");//頁眉中間內(nèi)容 //cmd.append(" --margin-top 30mm ");//設(shè)置頁面上邊距 (default 10mm) cmd.append(" --header-spacing 10 ");//(設(shè)置頁眉和內(nèi)容的距離,默認(rèn)0) cmd.append(srcPath); cmd.append(" "); cmd.append(destPath); boolean result=true; try{ Process proc=Runtime.getRuntime().exec(cmd.toString()); HtmlToPdfInterceptor error=new HtmlToPdfInterceptor(proc.getErrorStream()); HtmlToPdfInterceptor output=new HtmlToPdfInterceptor(proc.getInputStream()); error.start(); output.start(); proc.waitFor(); }catch(Exception e){ result=false; e.printStackTrace(); } return result; } public static void main(String[] args) { HtmlToPdf.convert("https://my.oschina.net/papio/blog/835645", "d:/wkhtmltopdf.pdf"); }}復(fù)制代碼
wkhtmltopdf 參數(shù)詳解
wkhtmltopdf [OPTIONS]... <input file> [More input files] <output file>常規(guī)選項(xiàng) --allow <path> 允許加載從指定的文件夾中的文件或文件(可重復(fù)) --book* 設(shè)置一會(huì)打印一本書的時(shí)候,通常設(shè)置的選項(xiàng) --collate 打印多份副本時(shí)整理 --cookie <name> <value> 設(shè)置一個(gè)額外的cookie(可重復(fù)) --cookie-jar <path> 讀取和寫入的Cookie,并在提供的cookie jar文件 --copies <number> 復(fù)印打印成pdf文件數(shù)(默認(rèn)為1) --cover* <url> 使用HTML文件作為封面。它會(huì)帶頁眉和頁腳的TOC之前插入 --custom-header <name> <value> 設(shè)置一個(gè)附加的HTTP頭(可重復(fù)) --debug-javascript 顯示的javascript調(diào)試輸出 --default-header* 添加一個(gè)缺省的頭部,與頁面的左邊的名稱,頁面數(shù)到右邊,例如: --header-left '[webpage]' --header-right '[page]/[toPage]' --header-line --disable-external-links* 禁止生成鏈接到遠(yuǎn)程網(wǎng)頁 --disable-internal-links* 禁止使用本地鏈接 --disable-javascript 禁止讓網(wǎng)頁執(zhí)行JavaScript --disable-pdf-compression* 禁止在PDF對(duì)象使用無損壓縮 --disable-smart-shrinking* 禁止使用WebKit的智能戰(zhàn)略收縮,使像素/ DPI比沒有不變 --disallow-local-file-access 禁止允許轉(zhuǎn)換的本地文件讀取其他本地文件,除非explecitily允許用 --allow --dpi <dpi> 顯式更改DPI(這對(duì)基于X11的系統(tǒng)沒有任何影響) --enable-plugins 啟用已安裝的插件(如Flash --encoding <encoding> 設(shè)置默認(rèn)的文字編碼 --extended-help 顯示更廣泛的幫助,詳細(xì)介紹了不常見的命令開關(guān) --forms* 打開HTML表單字段轉(zhuǎn)換為PDF表單域 --grayscale PDF格式將在灰階產(chǎn)生 --help Display help --htmldoc 輸出程序HTML幫助 --ignore-load-errors 忽略claimes加載過程中已經(jīng)遇到了一個(gè)錯(cuò)誤頁面 --lowquality 產(chǎn)生低品質(zhì)的PDF/ PS。有用縮小結(jié)果文檔的空間 --manpage 輸出程序手冊(cè)頁 --margin-bottom <unitreal> 設(shè)置頁面下邊距 (default 10mm) --margin-left <unitreal> 將左邊頁邊距 (default 10mm) --margin-right <unitreal> 設(shè)置頁面右邊距 (default 10mm) --margin-top <unitreal> 設(shè)置頁面上邊距 (default 10mm) --minimum-font-size <int> 最小字體大小 (default 5) --no-background 不打印背景 --orientation <orientation> 設(shè)置方向?yàn)闄M向或縱向 --page-height <unitreal> 頁面高度 (default unit millimeter) --page-offset* <offset> 設(shè)置起始頁碼 (default 1) --page-size <size> 設(shè)置紙張大小: A4, Letter, etc. --page-width <unitreal> 頁面寬度 (default unit millimeter) --password <password> HTTP驗(yàn)證密碼 --post <name> <value> Add an additional post field (repeatable) --post-file <name> <path> Post an aditional file (repeatable) --print-media-type* 使用的打印介質(zhì)類型,而不是屏幕 --proxy <proxy> 使用代理 --quiet Be less verbose --read-args-from-stdin 讀取標(biāo)準(zhǔn)輸入的命令行參數(shù) --readme 輸出程序自述 --redirect-delay <msec> 等待幾毫秒為JS-重定向(default 200) --replace* <name> <value> 替換名稱,值的頁眉和頁腳(可重復(fù)) --stop-slow-scripts 停止運(yùn)行緩慢的JavaScripts --title <text> 生成的PDF文件的標(biāo)題(第一個(gè)文檔的標(biāo)題使用,如果沒有指定) --toc* 插入的內(nèi)容的表中的文件的開頭 --use-xserver* 使用X服務(wù)器(一些插件和其他的東西沒有X11可能無法正常工作) --user-style-sheet <url> 指定用戶的樣式表,加載在每一頁中 --username <username> HTTP認(rèn)證的用戶名 --version 輸出版本信息退出 --zoom <float> 使用這個(gè)縮放因子 (default 1) 頁眉和頁腳選項(xiàng)--header-center* <text> (設(shè)置在中心位置的頁眉內(nèi)容) --header-font-name* <name> (default Arial) (設(shè)置頁眉的字體名稱)--header-font-size* <size> (設(shè)置頁眉的字體大小)--header-html* <url> (添加一個(gè)HTML頁眉,后面是網(wǎng)址)--header-left* <text> (左對(duì)齊的頁眉文本)--header-line* (顯示一條線在頁眉下)--header-right* <text> (右對(duì)齊頁眉文本)--header-spacing* <real> (設(shè)置頁眉和內(nèi)容的距離,默認(rèn)0)--footer-center* <text> (設(shè)置在中心位置的頁腳內(nèi)容) --footer-font-name* <name> (設(shè)置頁腳的字體名稱) --footer-font-size* <size> (設(shè)置頁腳的字體大小default 11)--footer-html* <url> (添加一個(gè)HTML頁腳,后面是網(wǎng)址)--footer-left* <text> (左對(duì)齊的頁腳文本)--footer-line* 顯示一條線在頁腳內(nèi)容上)--footer-right* <text> (右對(duì)齊頁腳文本)--footer-spacing* <real> (設(shè)置頁腳和內(nèi)容的距離)./wkhtmltopdf --footer-right '[page]/[topage]' http://www.baidu.com baidu.pdf./wkhtmltopdf --header-center '報(bào)表' --header-line --margin-top 2cm --header-line http://192.168.212.139/oma/ oma.pdf表內(nèi)容選項(xiàng)中 --toc-depth* <level> Set the depth of the toc (default 3) --toc-disable-back-links* Do not link from section header to toc --toc-disable-links* Do not link from toc to sections --toc-font-name* <name> Set the font used for the toc (default Arial) --toc-header-font-name* <name> The font of the toc header (if unset use --toc-font-name) --toc-header-font-size* <size> The font size of the toc header (default 15) --toc-header-text* <text> The header text of the toc (default Table Of Contents) --toc-l1-font-size* <size> Set the font size on level 1 of the toc (default 12) --toc-l1-indentation* <num> Set indentation on level 1 of the toc (default 0) --toc-l2-font-size* <size> Set the font size on level 2 of the toc (default 10) --toc-l2-indentation* <num> Set indentation on level 2 of the toc (default 20) --toc-l3-font-size* <size> Set the font size on level 3 of the toc (default 8) --toc-l3-indentation* <num> Set indentation on level 3 of the toc (default 40) --toc-l4-font-size* <size> Set the font size on level 4 of the toc (default 6) --toc-l4-indentation* <num> Set indentation on level 4 of the toc (default 60) --toc-l5-font-size* <size> Set the font size on level 5 of the toc (default 4) --toc-l5-indentation* <num> Set indentation on level 5 of the toc (default 80) --toc-l6-font-size* <size> Set the font size on level 6 of the toc (default 2) --toc-l6-indentation* <num> Set indentation on level 6 of the toc (default 100) --toc-l7-font-size* <size> Set the font size on level 7 of the toc (default 0) --toc-l7-indentation* <num> Set indentation on level 7 of the toc (default 120) --toc-no-dots* Do not use dots, in the toc輪廓選項(xiàng) --dump-outline <file> 轉(zhuǎn)儲(chǔ)目錄到一個(gè)文件 --outline 顯示目錄(文章中h1,h2來定) --outline-depth <level> 設(shè)置目錄的深度(默認(rèn)為4)頁腳和頁眉 * [page] 由當(dāng)前正在打印的頁的數(shù)目代替 * [frompage] 由要打印的第一頁的數(shù)量取代 * [topage] 由最后一頁要打印的數(shù)量取代 * [webpage] 通過正在打印的頁面的URL替換 * [section] 由當(dāng)前節(jié)的名稱替換 * [subsection] 由當(dāng)前小節(jié)的名稱替換 * [date] 由當(dāng)前日期系統(tǒng)的本地格式取代 * [time] 由當(dāng)前時(shí)間,系統(tǒng)的本地格式取代
作者:曹元
鏈接:https://juejin.im/post/6856547881873047559
來源:掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)注明出處。
上篇內(nèi)容。上次介紹了整個(gè)課程,目的是告訴大家學(xué)習(xí)路線,不要偏了。今天開始慢慢告訴大家這些課程的重點(diǎn)。也就是需要學(xué)些什么有用的東西,那些沒用的可以先不要去學(xué)習(xí),等學(xué)到一定程度了,再慢慢去挖掘。首先開始是網(wǎng)頁,因?yàn)樗容^簡(jiǎn)單。網(wǎng)頁就是HTML,HTML是超文本標(biāo)記語言的簡(jiǎn)稱。說白點(diǎn),就是比普通文本更加牛逼啦!我們?cè)诰W(wǎng)頁上看到的花花綠綠的文字,圖片都是超文本。所以,HTML就是來做網(wǎng)頁的。建立一個(gè)網(wǎng)頁非常簡(jiǎn)單,新建一個(gè)記事本文件,敲入以下代碼,就可以完成一個(gè)網(wǎng)頁。
<html>
<head>
<title>網(wǎng)頁的標(biāo)題</title>
</head>
<body>
你好,html世界。
</body>
</html>
然后把記事本的后綴名改成html就ok了。后綴名是什么如果還不知道的話,先去百度,下次再給大家科普。
就是這么簡(jiǎn)單,關(guān)于這些標(biāo)記,記住就行了。
然后注意寫的時(shí)候要有層次感。
還有一點(diǎn)非常重要,學(xué)習(xí)html一定要有容器的概念,什么是容器的概念,就是把<html></html>這一對(duì)標(biāo)記看成一個(gè)容器,他里面有<head></head>和<body></body>兩個(gè)兄弟容器,這些容器下面又可以寫其他容器。
需要記住的標(biāo)記有:
標(biāo)題標(biāo)記
<h1>
…
<h6>
有序列表
ol
子標(biāo)記 li
<ol type="a">
<li>第一次</li>
<li>第二次</li>
<li>第三次</li>
</ol>
無序列表
ul
li
例如
<ul>
<li>蘋果</li>
<ol type="a">
<li>一種水果</li>
<li>智能手機(jī)</li>
</ol>
<li>桔子</li>
<ol type="a">
<li>酸酸的水果</li>
<li>同時(shí)還有點(diǎn)甜</li>
</ol>
</ul>
自定義列表:<dt>是標(biāo)題,<dd>是解釋
<dl>
<dt>250</dt>
<dd>在地球上,人民夸某個(gè)人的時(shí)候喜歡使用你是250</dd>
<dt>隔壁老王</dt>
<dd>在地球上,夸某一位鄰居長(zhǎng)得帥,就叫隔壁老王</dd>
<dt>矮矬窮</dt>
<dd>長(zhǎng)得好看又有錢</dd>
<dt>高富帥</dt>
<dd>長(zhǎng)得丑還沒錢</dd>
</dl>
超鏈接
<a href="路徑">
圖片
<img src="路徑" alt="">
1 相對(duì)路徑就是指相對(duì)于當(dāng)前文件夾所在的路徑.(建議使用相對(duì)路徑)
..兩點(diǎn)表示到相對(duì)路徑的上一級(jí)目錄.
每一個(gè)\代表一個(gè)目錄
2 絕對(duì)路徑就是指帶了盤符的路徑.可以直接根據(jù)地址在電腦硬盤中找到.
表格
<table>
<tr>行
<td>單元格
bgcolor 背景顏色
width 寬度
height 高度
border 邊框?qū)挾?/p>
align 水平對(duì)齊 left 左對(duì)齊 center 居中 right 居右
<table>
<tr>
<td colspan="2">10</td>
<td rowspan="2">12</td>
</tr>
<tr>
<td>20</td>
<td>21</td>
</tr>
</table>
這里需要花兩天時(shí)間來練習(xí)和消化。
接下來就要開始學(xué)習(xí)表單知識(shí)了。
下次再給大家分享表單需要學(xué)習(xí)什么。
關(guān)注我的微信公眾號(hào)machine_matrix , 獲取更多知識(shí).
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。