Linux command
wkhtmltopdf 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Convert HTML to PDF
wkhtmltopdf [input.html] [output.pdf]
Convert URL to PDF
wkhtmltopdf [https://example.com] [output.pdf]
Set page size
wkhtmltopdf --page-size [A4] [input.html] [output.pdf]
Set orientation
wkhtmltopdf --orientation [Landscape] [input.html] [output.pdf]
Add margins
wkhtmltopdf --margin-top [20mm] --margin-bottom [20mm] [input.html] [output.pdf]
Disable JavaScript
wkhtmltopdf --disable-javascript [input.html] [output.pdf]
Add header and footer
wkhtmltopdf --header-center "[title]" --footer-center "[page]/[topage]" [input.html] [output.pdf]
Multiple pages
wkhtmltopdf [page1.html] [page2.html] [output.pdf]
说明
wkhtmltopdf converts HTML to PDF using the WebKit rendering engine. It renders pages as a browser would, then outputs to PDF. Multiple input pages combine into a single PDF. Table of contents can be auto-generated from headings. Cover pages and headers/footers frame the content. Headers and footers support variables: page, topage, title, section, subsection, date, time, webpage. These enable automatic page numbers and running headers. Page breaks can be controlled via CSS print media rules. The tool respects page-break-before, page-break-after, and related properties. JavaScript delay handles dynamic content that loads asynchronously. For heavily JS-dependent pages, increasing delay ensures complete rendering. Authentication options handle basic auth, cookies, and custom headers. This enables converting protected pages.
参数
- --page-size _SIZE_
- Paper size: A4, Letter, Legal, etc.
- --orientation _ORIENT_
- Landscape or Portrait.
- --margin-top _SIZE_
- Top margin (e.g., 10mm, 1in).
- --margin-bottom _SIZE_
- Bottom margin.
- --margin-left _SIZE_
- Left margin.
- --margin-right _SIZE_
- Right margin.
- --dpi _DPI_
- Output resolution.
- --zoom _FACTOR_
- Zoom factor.
- --grayscale
- Generate grayscale PDF.
- --disable-javascript
- Don't run JavaScript.
- --javascript-delay _MS_
- Wait before printing.
- --header-html _FILE_
- HTML header file.
- --header-center _TEXT_
- Centered header text.
- --footer-html _FILE_
- HTML footer file.
- --footer-center _TEXT_
- Centered footer text.
- --toc
- Include table of contents.
- --cover _FILE_
- Cover page HTML.
- --cookie _NAME_ _VALUE_
- Set cookie.
- --username _USER_
- HTTP authentication user.
- --password _PASS_
- HTTP authentication password.
- --no-images
- Don't load images.
- --lowquality
- Generate lower quality PDF/PS to reduce file size.
- --encoding _ENCODING_
- Set the default text encoding.
FAQ
What is the wkhtmltopdf command used for?
wkhtmltopdf converts HTML to PDF using the WebKit rendering engine. It renders pages as a browser would, then outputs to PDF. Multiple input pages combine into a single PDF. Table of contents can be auto-generated from headings. Cover pages and headers/footers frame the content. Headers and footers support variables: page, topage, title, section, subsection, date, time, webpage. These enable automatic page numbers and running headers. Page breaks can be controlled via CSS print media rules. The tool respects page-break-before, page-break-after, and related properties. JavaScript delay handles dynamic content that loads asynchronously. For heavily JS-dependent pages, increasing delay ensures complete rendering. Authentication options handle basic auth, cookies, and custom headers. This enables converting protected pages.
How do I run a basic wkhtmltopdf example?
Run `wkhtmltopdf [input.html] [output.pdf]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does --page-size _SIZE_ do in wkhtmltopdf?
Paper size: A4, Letter, Legal, etc.