Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 影像與多媒體

wkhtmltopdf Win7 實戰:HTML 網頁一鍵轉高保真 PDF 完整指南

編輯:影像與多媒體

wkhtmltopdf 是基於 Qt WebKit 的命令列工具,能將 HTML 網頁 (含 CSS、JS 渲染後畫面) 高保真轉為 PDF,支援頁首頁尾、目錄、封面、權限設定等進階功能。本文以 Win7 32/64 位為例,詳解安裝、基本用法、常見參數、輸出路徑控制。

安裝步驟

  1. 官網 下載頁面 選擇對應版本 (Win7 32位下載 wkhtmltox-0.12.x_msvc2013-win32.exe,64位下載 wkhtmltox-0.12.x_msvc2013-win64.exe)。
  2. 雙擊安裝,一路「下一步」,記住安裝路徑 (預設 C:\Program Files\wkhtmltopdf\bin 或 C:\Program Files (x86)\wkhtmltopdf\bin)。
  3. 安裝完成無桌面圖標、無視窗介面 (純後台程式)。

基本使用 (CMD 命令列)

  1. Win+R → 輸入 cmd 開啟命令提示字元。
  2. 切換至安裝目錄:
    cd "C:\Program Files (x86)\wkhtmltopdf\bin" (依實際路徑調整)
  3. 執行轉換:
    wkhtmltopdf.exe https://www.example.com output.pdf
    藍色:程式;紅色:目標網址;綠色:輸出檔名 (含路徑可自訂)

輸出檔預設生成於執行目錄 (即 bin 資料夾)。若指定完整路徑 (如 D:\PDFs\report.pdf) 則存放於指定位置。

常用進階參數

參數說明範例
-s / --page-size紙張尺寸-s A4, -s Letter
-O / --orientation方向-O Portrait (直向), -O Landscape (橫向)
--margin-top/bottom/left/right邊距 (mm)--margin-top 20
--header-html自訂頁首 HTML--header-html header.html
--footer-html自訂頁尾 HTML--footer-html footer.html
--toc生成目錄--toc --toc-level 2
--cover封面頁--cover cover.html
--grayscale灰階輸出--grayscale
--no-pdf-compression關閉 PDF 壓縮 (利於後續編輯)--no-pdf-compression

實戰範例:生成帶目錄、頁碼、封面的專業報表

wkhtmltopdf.exe ^
  --page-size A4 ^
  --margin-top 25 --margin-bottom 25 --margin-left 20 --margin-right 20 ^
  --header-html header.html ^
  --footer-html footer.html ^
  --toc --toc-level 2 ^
  --cover cover.html ^
  https://intranet.company.com/quarterly-report ^
  D:\Reports\Q3_Report.pdf

常見問題排查

  • 路徑錯誤:用 where wkhtmltopdf 或一切檔搜尋 wkhtmltopdf.exe 確認真實路徑。
  • 中文亂碼/缺字:確保網頁 <meta charset="utf-8">,系統安裝中文字體 (微軟正黑體等)。
  • JS 渲染不全:加參數 --javascript-delay 5000 (毫秒) 等待 JS 執行完成。
  • HTTPS 憑證錯誤:加 --ignore-load-errors --disable-javascript (視情況) 或匯入憑證。

wkhtmltopdf 輕量、免費、自動化友善,是批次生成報表、發票、文檔歸檔的首選利器。掌握核心參數,HTML 轉 PDF 不再是難題。

Copyright © Windows教程網 All Rights Reserved