LinuxCommandLibrary

weasyprint

Convert HTML documents to PDF

TLDR

Render an HTML file to PDF

$ weasyprint [path/to/input.html] [path/to/output.pdf]
copy

Render an HTML file to PNG, including an additional user stylesheet
$ weasyprint [path/to/input.html] [path/to/output.png] --stylesheet [path/to/stylesheet.css]
copy

Output additional debugging information when rendering
$ weasyprint [path/to/input.html] [path/to/output.pdf] --verbose
copy

Specify a custom resolution when outputting to PNG
$ weasyprint [path/to/input.html] [path/to/output.png] --resolution [300]
copy

Specify a base URL for relative URLs in the input HTML file
$ weasyprint [path/to/input.html] [path/to/output.png] --base-url [url_or_filename]
copy

SYNOPSIS

weasyprint [OPTIONS] <INPUT> <OUTPUT>

INPUT can be a URL, a local file path, or - for standard input.
OUTPUT can be a file path, or - for standard output.

PARAMETERS

-h, --help
    Show program’s help message and exit.

-v, --version
    Show program’s version number and exit.

-e ENCODING, --encoding ENCODING
    Set the input encoding for the HTML document.

-s STYLESHEET, --stylesheet STYLESHEET
    Path to a user stylesheet to apply. Can be specified multiple times for multiple stylesheets.

-m MEDIA_TYPE, --media-type MEDIA_TYPE
    Set the media type for the CSS, e.g., 'print', 'screen', 'projection'. Defaults to 'print'.

-u BASE_URL, --base-url BASE_URL
    Set the base URL for resolving relative paths in the document (e.g., for images, links).

-r RESOLUTION, --resolution RESOLUTION
    Set the resolution for PNG output, in dots per inch (DPI). Defaults to 96 DPI.

--optimize-size
    Optimize the PDF output for a smaller file size.

--full-html
    Force parsing the input as a complete HTML document, even if it appears to be a fragment.

--presentational-hints
    Apply presentational hints found in the HTML (e.g., width/height attributes on images).

--skip-by-default
    Skip resources that fail to load (e.g., images, stylesheets) instead of stopping the process.

--pdf-variant {pdf/ua-1,pdf/a-1,pdf/a-2,pdf/a-3,pdf/x-1a-2001}
    Set the PDF variant for compliance, such as PDF/A for archiving or PDF/UA for accessibility.

--pdf-version VERSION
    Set the PDF version number to be used for the output document (e.g., '1.7').

--uncompressed-margin-boxes
    Do not compress margin boxes in the PDF output, potentially increasing file size but aiding debugging.

--font-config PATH
    Path to an additional font configuration file for font discovery.

--hinting {none,slight,medium,full}
    Set the font hinting level to control how fonts are rendered.

--embed-fonts
    Embed fonts in the PDF output. This is the default behavior.

--no-embed-fonts
    Do not embed fonts in the PDF output, which can reduce file size but requires fonts to be available on the viewing system.

--substitute-colors
    Substitute colors for improved accessibility, converting some colors to more readable alternatives.

--setting NAME=VALUE
    Set a specific configuration setting for advanced control. Can be specified multiple times.

--attachment ATTACHMENT
    Path to a file to be embedded as an attachment in the PDF document.

--access-hint HINT
    Provide a hint to improve the accessibility of the generated PDF.

--media-feature NAME=VALUE
    Set a CSS media feature value for rendering. Can be specified multiple times.

DESCRIPTION

WeasyPrint is a powerful and flexible command-line tool and Python library that transforms HTML and CSS documents into high-quality PDF or PNG files. It stands out by fully supporting modern web standards, including CSS Paged Media specifications, making it ideal for generating complex documents like invoices, reports, and books directly from web content.

Unlike many web-to-PDF converters that rely on browser engines, WeasyPrint implements its own layout engine, which allows for precise control over typography, pagination, and graphic elements. It's particularly useful for automating document generation workflows, integrating seamlessly into web applications or command-line scripts. WeasyPrint aims to create pixel-perfect renditions of web pages, ensuring that the visual presentation in the output matches the intended design. Its ability to handle intricate CSS layouts and generate accessible PDFs makes it a valuable tool for developers and businesses alike.

CAVEATS

WeasyPrint provides excellent HTML/CSS rendering, but it has some limitations:
• It does not execute JavaScript. Pages requiring client-side scripting for content generation or layout will not render correctly.
• Complex or non-standard CSS layouts might not always be perfectly replicated.
• For very large or complex documents, memory usage and generation time can be significant.
• While supporting modern CSS, some bleeding-edge or experimental CSS features may not be fully implemented.

INPUT AND OUTPUT FLEXIBILITY

WeasyPrint supports various forms of input and output. Input can be read directly from a URL (e.g., http://example.com/document.html), a local file path (e.g., /path/to/document.html), or standard input (-), which allows piping HTML content from other commands. Similarly, output can be written to a specific file path (e.g., output.pdf) or to standard output (-), enabling further processing with other command-line tools.

CSS PAGED MEDIA SUPPORT

A key strength of WeasyPrint is its comprehensive support for the CSS Paged Media Module. This allows web developers to use familiar CSS properties to control page breaks, margins, headers, footers, page numbering, and other print-specific layouts, directly within their HTML and CSS, making it ideal for creating professional-quality print documents.

HISTORY

WeasyPrint was created by the French company Kozea, starting development around 2011-2012. It emerged as an open-source project with the goal of providing a robust and standards-compliant way to render HTML and CSS to PDF, particularly emphasizing the CSS Paged Media module. Its independent layout engine differentiates it from solutions relying on web browser engines, allowing for greater control and precision in document generation. Over the years, it has continuously evolved, adding support for new CSS features, improving performance, and enhancing PDF/A and PDF/UA capabilities for accessibility and archiving.

SEE ALSO

Copied to clipboard