SL-FO 需要格式化軟件來產(chǎn)生輸出。
XSL-FO 處理器
一個(gè) XSL-FO 處理器是一個(gè)用于格式化輸出 XSL 文檔的軟件程序。
大多數(shù)的 XSL-FO 處理器可以輸出 HTML、PDF 文檔和質(zhì)量打印。
下面介紹一些知名的 XSL-FO 處理器。
Antenna House Formatter V5
Antenna House Formatter V5 是為 PDF 或打印格式化 XML 文檔的軟件程序。
訪問 Antenna House
Altova 的 StyleVision
StyleVision 基于您的設(shè)計(jì)自動(dòng)生成一致性標(biāo)準(zhǔn)的 XSLT 和 XSL:FO 樣式表,以及相應(yīng)的 HTML、RTF、PDF、 Word 2007 輸出,等等。
Ecrion 的 XF 產(chǎn)品
XSL-FO 格式化的一些產(chǎn)品!
要使用的是wkhtmltopdf的Python封裝——pdfkit
安裝
1. Install python-pdfkit:
$ pip install pdfkit
2. Install wkhtmltopdf:
$ sudo apt-get install wkhtmltopdf
sudo yum intsall wkhtmltopdf
brew install Caskroom/cask/wkhtmltopdf
使用
一個(gè)簡(jiǎn)單的例子:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
你也可以傳遞一個(gè)url或者文件名列表:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以傳遞一個(gè)打開的文件:
with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf')
如果你想對(duì)生成的PDF作進(jìn)一步處理, 你可以將其讀取到一個(gè)變量中:
# 設(shè)置輸出文件為False,將結(jié)果賦給一個(gè)變量
pdf = pdfkit.from_url('http://google.com', False)
你可以制定所有的 wkhtmltopdf 選項(xiàng) http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 你可以移除選項(xiàng)名字前面的 '--' .如果選項(xiàng)沒有值, 使用None, False or * 作為字典值:
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'no-outline': None
}
pdfkit.from_url('http://google.com', 'out.pdf', options=options)
默認(rèn)情況下, PDFKit 將會(huì)顯示所有的 wkhtmltopdf 輸出. 如果你不想看到這些信息,你需要傳遞一個(gè) quiet 選項(xiàng):
options = {
'quiet': ''
}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf的命令語法 , TOC 和 Cover 選項(xiàng)必須分開指定:
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
當(dāng)你轉(zhuǎn)換文件、或字符串的時(shí)候,你可以通過css選項(xiàng)指定擴(kuò)展的 CSS 文件。
# 單個(gè) CSS 文件
css = 'example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css = ['example.css', 'example2.css']
pdfkit.from_file('file.html', options=options, css=css)
你也可以通過你的HTML中的meta tags傳遞任意選項(xiàng):
body = """
<html>
<head>
<meta name="pdfkit-page-size" content="Legal"/>
<meta name="pdfkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
pdfkit.from_string(body, 'out.pdf') #with --page-size=Legal and --orientation=Landscape
配置
每個(gè)API調(diào)用都有一個(gè)可選的參數(shù)。這應(yīng)該是pdfkit.configuration()API 調(diào)用的一個(gè)實(shí)例. 采用configuration 選項(xiàng)作為初始化參數(shù)。可用的選項(xiàng)有:
示例 :針對(duì)wkhtmltopdf不在系統(tǒng)路徑中(不在$PATH里面)
PATH里面):
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)
問題
IOError:'No wkhtmltopdf executable found':
確保 wkhtmltopdf 在你的系統(tǒng)路徑中(PATH), 會(huì)通過 configuration進(jìn)行了配置 (詳情看上文描述)。 在Windows系統(tǒng)中使用where wkhtmltopdf命令 或 在 linux系統(tǒng)中使用 which wkhtmltopdf 會(huì)返回 wkhtmltopdf二進(jìn)制可執(zhí)行文件所在的確切位置.
如果出現(xiàn)這個(gè)錯(cuò)誤意味著 PDFKit不能處理一個(gè)輸入。你可以嘗試直接在錯(cuò)誤信息后面直接運(yùn)行一個(gè)命令來查看是什么導(dǎo)致了這個(gè)錯(cuò)誤 (某些版本的 wkhtmltopdf會(huì)因?yàn)槎五e(cuò)誤導(dǎo)致處理失敗)
確保兩項(xiàng):
1)、你的系統(tǒng)中有中文字體
2)、在html中加入
下面是我隨便寫的一個(gè)HTML表格:
<html>
<head><meta charset="UTF-8"></head>
<body>
<table width="400" border="1">
<tr>
<th align="left">Item....</th>
<th align="right">1</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">1.10</td>
</tr>
<tr>
<td align="left">化妝品</td>
<td align="right">.00</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">0.40</td>
</tr>
<tr>
<th align="left">tOTAL</th>
<th align="right">01.50</th>
</tr>
</table>
</body>
</html>
下面是生成的PDF截圖
XSL 指擴(kuò)展樣式表語言(EXtensible Stylesheet Language)。
萬維網(wǎng)聯(lián)盟 (W3C) 開始發(fā)展 XSL 的原因是:存在著對(duì)于基于 XML 的樣式表語言的需求。
HTML 使用預(yù)先定義的標(biāo)簽,標(biāo)簽的意義很容易被理解。
HTML 元素中的 <table> 元素定義表格 - 并且瀏覽器清楚如何顯示它。
向 HTML 元素添加樣式是很容易的。通過 CSS,很容易告知瀏覽器用特定的字體或顏色顯示一個(gè)元素。
XML 不使用預(yù)先定義的標(biāo)簽(我們可以使用任何喜歡的標(biāo)簽名),并且這些標(biāo)簽的意義并不都那么容易被理解。
<table> 元素意味著一個(gè) HTML 表格,一件家具,或是別的什么東西 - 瀏覽器不清楚如何顯示它。
XSL 可描述如何來顯示 XML 文檔!
XSL 包括三部分:
1. XSLT 簡(jiǎn)介一種用于轉(zhuǎn)換 XML 文檔的語言。
2. XPath 簡(jiǎn)介一種用于在 XML 文檔中導(dǎo)航的語言。
3. XSL-FO 簡(jiǎn)介一種用于格式化 XML 文檔的語言。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。