著互聯網的迅猛發展,HTML網頁文件已成為我們獲取信息、學習知識和分享內容的主要形式之一。然而,當需要將大量的HTML網頁內容整理、保存或分享時,PDF格式因其良好的可讀性和兼容性而備受青睞。此時,一款能夠將HTML網頁文件批量轉換為PDF文檔的工具顯得尤為重要。而“首助編輯高手”軟件正是這樣一款高效、便捷的工具,它能夠幫助用戶輕松實現HTML到PDF的批量轉換,讓文件處理變得更加簡單和高效。
1. 選擇批量轉換格式:軟件支持多種文件格式的批量轉換,包括但不限于HTML轉PDF。無論您是需要將單個文件轉換,還是需要對整個文件夾中的文件進行批量處理,它都能輕松應對。
2. 支持多種轉換模式:除了基本的格式轉換,軟件還提供了多種轉換模式供用戶選擇。您可以根據需要選擇不同的轉換模式,以滿足不同的輸出需求。
3. 選擇HTML轉PDF:在軟件界面中,您可以輕松選擇“HTML轉PDF”功能。這一功能專門針對HTML文件設計,確保轉換后的PDF文件保留原HTML文件的所有內容和格式。
4. 通過添加文件導入多個HTML網頁文件:支持一次性導入多個HTML網頁文件,讓您無需重復操作,即可實現批量轉換。只需簡單幾步,即可輕松完成文件的導入。
5. 選擇新文件保存路徑:在轉換過程中,您可以自由選擇新文件的保存路徑,方便您隨時查看和管理轉換后的文件。
6. 一鍵開始轉換:完成以上步驟后,只需點擊“開始轉換”按鈕,軟件就會立即開始工作。轉換過程快速而穩定,讓您無需等待。
7. 查看轉換效果:轉換完成后,您可以立即查看轉換后的PDF文件效果。軟件確保轉換質量高、還原度強,讓您無需擔心文件內容的丟失或變形。
8. 對比原文件效果:最后也可以打開原來的網頁文件對比一下,均為HTML格式的,說明轉換成功
軟件以其強大的功能和簡潔的操作界面,為用戶提供了將HTML網頁文件批量轉為PDF文檔的解決方案。無論是個人用戶還是企業用戶,都可以利用這款軟件輕松實現網頁內容的整理和分享,提高工作效率,同時也保證了文件的可讀性和兼容性。選擇“首助編輯高手”,讓HTML轉PDF變得更加簡單和高效,為您的工作和學習帶來極大的便利。
# txtPreview.js
# 引入jschardet獲取文件編碼
import jschardet from 'jschardet'
##
# 獲取文件的encoding碼
# @param {Object File} file input上傳的txt文件
# @return {String} encoding 返回文件的編碼格式
#
function checkEncoding (file) {
return new Promise((resolve, reject) => {
let reader = new FileReader()
reader.readAsDataURL(file) # 獲取txt文件的base64碼
reader.onload = function (e) {
let base64Str = reader.result
let str = atob(base64Str.split(';base64,')[1])
let encoding = jschardet.detect(str) # 通過jschardet獲取txt文件對應的編碼格式
encoding = encoding.encoding
if (encoding === 'window-1252') {
encoding = 'ANSI'
}
resolve(encoding)
}
})
}
##
# 返回file內容
# @param {Object File} file input上傳的txt文件
# @return {String} 返回txt文件的內容
#
const txtPreview = (file) => {
return new Promise((resolve, reject) => {
checkEncoding(file).then(encoding => {
let reader = new FileReader()
reader.readAsText(file, encoding) # 通過txt文件編碼格式獲取文件內容(字符串形式)
reader.onloadend = function (e) {
resolve(reader.result)
}
})
})
}
export default txtPreview
<html>
<body>
<form>
<input type="file" id="files" name="files"/>
</form>
<div id="resText" style="word-wrap: break-word; white-space: divpre-wrap;"></div>
<script>
import txtPreview from './txtPreview.js'
document.getElementById('files').addEventListener('change', handleFileSelect, false);
function handleFileSelect(e) {
let file = e.target.files[0]
txtPreview(file).then(res => {
document.getElementById("resText").innerHTML = res;
})
}
</script>
</body>
</html>
文轉載于https://lmangoxx.github.io/tags/FileReader/
要使用的是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
使用
一個簡單的例子:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')
你也可以傳遞一個url或者文件名列表:
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')
也可以傳遞一個打開的文件:
with open('file.html') as f:
pdfkit.from_file(f, 'out.pdf')
如果你想對生成的PDF作進一步處理, 你可以將其讀取到一個變量中:
# 設置輸出文件為False,將結果賦給一個變量
pdf = pdfkit.from_url('http://google.com', False)
你可以制定所有的 wkhtmltopdf 選項 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 你可以移除選項名字前面的 '--' .如果選項沒有值, 使用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)
默認情況下, PDFKit 將會顯示所有的 wkhtmltopdf 輸出. 如果你不想看到這些信息,你需要傳遞一個 quiet 選項:
options = {
'quiet': ''
}
pdfkit.from_url('google.com', 'out.pdf', options=options)
由于wkhtmltopdf的命令語法 , TOC 和 Cover 選項必須分開指定:
toc = {
'xsl-style-sheet': 'toc.xsl'
}
cover = 'cover.html'
pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)
當你轉換文件、或字符串的時候,你可以通過css選項指定擴展的 CSS 文件。
# 單個 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傳遞任意選項:
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
配置
每個API調用都有一個可選的參數。這應該是pdfkit.configuration()API 調用的一個實例. 采用configuration 選項作為初始化參數。可用的選項有:
示例 :針對wkhtmltopdf不在系統路徑中(不在$PATH里面)
PATH里面):
config = pdfkit.configuration(wkhtmltopdf='/opt/bin/wkhtmltopdf'))
pdfkit.from_string(html_string, output_file, configuration=config)
問題
IOError:'No wkhtmltopdf executable found':
確保 wkhtmltopdf 在你的系統路徑中(PATH), 會通過 configuration進行了配置 (詳情看上文描述)。 在Windows系統中使用where wkhtmltopdf命令 或 在 linux系統中使用 which wkhtmltopdf 會返回 wkhtmltopdf二進制可執行文件所在的確切位置.
如果出現這個錯誤意味著 PDFKit不能處理一個輸入。你可以嘗試直接在錯誤信息后面直接運行一個命令來查看是什么導致了這個錯誤 (某些版本的 wkhtmltopdf會因為段錯誤導致處理失敗)
確保兩項:
1)、你的系統中有中文字體
2)、在html中加入
下面是我隨便寫的一個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截圖
*請認真填寫需求信息,我們會在24小時內與您取得聯系。