wkhtmltopdf 是基於 Qt WebKit 的命令列工具,能將 HTML 網頁 (含 CSS、JS 渲染後畫面) 高保真轉為 PDF,支援頁首頁尾、目錄、封面、權限設定等進階功能。本文以 Win7 32/64 位為例,詳解安裝、基本用法、常見參數、輸出路徑控制。
wkhtmltox-0.12.x_msvc2013-win32.exe,64位下載 wkhtmltox-0.12.x_msvc2013-win64.exe)。C:\Program Files\wkhtmltopdf\bin 或 C:\Program Files (x86)\wkhtmltopdf\bin)。cmd 開啟命令提示字元。cd "C:\Program Files (x86)\wkhtmltopdf\bin" (依實際路徑調整)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">,系統安裝中文字體 (微軟正黑體等)。--javascript-delay 5000 (毫秒) 等待 JS 執行完成。--ignore-load-errors --disable-javascript (視情況) 或匯入憑證。wkhtmltopdf 輕量、免費、自動化友善,是批次生成報表、發票、文檔歸檔的首選利器。掌握核心參數,HTML 轉 PDF 不再是難題。