How to use the command `wkhtmltopdf` (with examples)
wkhtmltopdf
is an open-source command-line tool that provides a versatile means of converting HTML documents or web pages into PDF files. It serves as an essential utility for developers, technical writers, and digital professionals who need to generate PDF documents from web content for archiving, distribution, and presentation purposes. The tool is known for its ability to support various PDF conversion options, making it adaptable to different document presentation needs. More information about the tool can be found at its official website: wkhtmltopdf.org
.
Convert a HTML document into PDF
Code:
wkhtmltopdf input.html output.pdf
Motivation:
Converting HTML to PDF is a common requirement for many applications, ranging from creating downloadable user manuals to preserving web pages in a fixed format. This basic command enables users to straightforwardly take an HTML document, whether it’s local or web-based, and convert it into a fully-fledged PDF file, ensuring consistent formatting and presentation across different devices and platforms.
Explanation:
wkhtmltopdf
: This is the main command that initiates the conversion process.input.html
: The source HTML document that you want to convert. It can be a local file or a URL pointing to an existing webpage.output.pdf
: The name of the output PDF file where the converted content will be saved.
Example Output:
The result of this command is a PDF file named output.pdf
on your local machine, which includes all the content and design elements present in input.html
, rendered accurately to mirror the original document or webpage.
Specify the PDF page size
Code:
wkhtmltopdf --page-size A4 input.html output.pdf
Motivation:
Standardizing on a specific paper size is crucial for printed documents to ensure they fit correctly within physical media parameters, such as A4, Letter, or Legal sizes. Different paper sizes correspond to different use cases, such as corporate documentation or project submissions, where consistency in print layout is required. By specifying the page size at the conversion stage, you simplify the printing process and avoid excessive trimming or resizing post-conversion.
Explanation:
--page-size A4
: This argument specifies the desired size of the PDF pages. ‘A4’ is a widely used paper size, particularly in Europe and many other parts of the world.input.html
: The HTML document that needs conversion.output.pdf
: The name of the resulting PDF file.
Example Output:
Upon execution, an output.pdf
file is created, adhering to the A4 page dimensions. Content in input.html
is formatted to fit these dimensions, ensuring readability and compatibility with standard printers configured for A4 paper.
Set the PDF page margins
Code:
wkhtmltopdf --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm input.html output.pdf
Motivation:
Defining margins for a PDF document is essential for proper layout, ensuring that content does not run into the physical edges of the paper when printed and remains legible without requiring additional adjustments. Margins help create professional and aesthetically pleasing documents and are especially necessary when binding pages or when the document contains annotations or visual markers.
Explanation:
--margin-top|bottom|left|right 10mm
: These options set the margins for the respective sides of the pages to 10 millimeters. Adjusting the margins guarantees an elegant presentation by maintaining whitespace buffers around the content.input.html
: The HTML file to convert.output.pdf
: Specifies the destination PDF file.
Example Output:
The generated PDF file, output.pdf
, respects the 10mm margins on all sides, showcasing content cleanly and accessibly, whether viewed digitally or in printed form.
Set the PDF page orientation
Code:
wkhtmltopdf --orientation Landscape input.html output.pdf
Motivation:
Changing the orientation of a PDF can be vital for documents featuring wide tables, panoramic images, or layouts best viewed in a horizontal format. The primary orientations are Portrait and Landscape, with the latter providing a broader aspect ratio well-suited for designs that favor horizontal space over vertical.
Explanation:
--orientation Landscape
: This command specifies that the PDF should be formatted in landscape orientation, perfect for content that is wider than it is tall.input.html
: The source HTML content.output.pdf
: The path or name of the converted PDF file.
Example Output:
Executing this command produces an output.pdf
in Landscape orientation, allowing broader content to be displayed across the width of the page, making it easier to view or print such content efficiently.
Generate a greyscale version of the PDF document
Code:
wkhtmltopdf --grayscale input.html output.pdf
Motivation:
Creating a greyscale PDF is often necessary for reducing printing costs and ensuring document legibility when colored inks are unavailable or inappropriate, such as in official/public document submissions or archival records. Moreover, greyscale PDFs can mitigate color distractions, helping to emphasize content structure and hierarchy strictly.
Explanation:
--grayscale
: This flag converts colored HTML content into shades of grey, guaranteeing compatibility with monochrome printing and potentially reducing file size.input.html
: The input HTML source.output.pdf
: The PDF file output path.
Example Output:
The resulting output.pdf
portrays a greyscale version of the original input.html
content, effectively reducing color elements to their monochrome representations while preserving layout and readability.
Conclusion:
The wkhtmltopdf
command-line tool is a powerful asset for anyone needing to convert HTML documents into PDFs. Through multiple flexible parameters like page size, margins, orientation, and color scaling, users can cater to diverse formatting needs swiftly and accurately. Whether creating professional reports, preserving web pages, or optimizing documents for print, wkhtmltopdf
proves to be indispensable.