{"library":"weasyprint","title":"WeasyPrint","description":"WeasyPrint is an actively maintained Python library (current version 68.1) that transforms HTML and CSS documents into high-quality PDF files. It acts as an 'Awesome Document Factory' by rendering web content using web standards, making it suitable for generating reports, invoices, books, and other printable documents with precise layout control. Releases are frequent, often several times a year.","status":"active","version":"68.1","language":"en","source_language":"en","source_url":"https://github.com/Kozea/WeasyPrint","tags":["pdf","html-to-pdf","css-to-pdf","document-generation","rendering","print-layout"],"install":[{"cmd":"pip install weasyprint","lang":"bash","label":"Basic Python Installation"},{"cmd":"sudo apt install weasyprint","lang":"bash","label":"Ubuntu/Debian (recommended)"},{"cmd":"brew install python pango libffi\npip install weasyprint","lang":"bash","label":"macOS (Homebrew)"},{"cmd":"Follow the official documentation for GTK+ installation using MSYS2, then: pip install weasyprint","lang":"bash","label":"Windows (requires GTK+)"}],"dependencies":[{"reason":"WeasyPrint requires Python 3.10+.","package":"Python","optional":false},{"reason":"Crucial for text and font rendering, a system-level dependency.","package":"Pango","optional":false},{"reason":"Used for graphical rendering, a system-level dependency.","package":"Cairo","optional":false},{"reason":"Provides Pango and Cairo on Windows, and other core graphical libraries.","package":"GTK+","optional":false},{"reason":"Required for CSS parsing (version 1.5.0+ from v67.0).","package":"tinycss2","optional":false},{"reason":"Required for font handling (version 4.59.2+ from v67.0).","package":"fontTools","optional":false},{"reason":"Required for HTML parsing (replaces html5lib from v63.0, version 2.0.0b1+ from v67.0).","package":"tinyhtml5","optional":false},{"reason":"Required for CSS selector matching (version 0.8.0+ from v65.0).","package":"cssselect2","optional":false},{"reason":"Required for PDF generation backend (version 0.11.0+ from v63.0).","package":"pydyf","optional":false}],"imports":[{"symbol":"HTML","correct":"from weasyprint import HTML"},{"symbol":"CSS","correct":"from weasyprint import HTML, CSS"},{"note":"`default_url_fetcher()` is deprecated in v68.0. For custom URL fetching, use the new `URLFetcher` class. Returning dicts from custom fetchers will be removed in v69.0.","wrong":"default_url_fetcher(url)","symbol":"default_url_fetcher","correct":"from weasyprint import default_url_fetcher"}],"quickstart":{"code":"from weasyprint import HTML, CSS\n\nhtml_content = \"\"\"\n<!DOCTYPE html>\n<html>\n<head>\n    <title>My Document</title>\n    <style>\n        @page { size: A4; margin: 2cm; }\n        body { font-family: sans-serif; }\n        h1 { color: #336699; }\n        p { line-height: 1.5; }\n    </style>\n</head>\n<body>\n    <h1>Hello, WeasyPrint!</h1>\n    <p>This is a test document generated from HTML and CSS into a PDF.</p>\n    <img src=\"https://weasyprint.org/static/logo.png\" alt=\"WeasyPrint Logo\">\n</body>\n</html>\n\"\"\"\n\n# You can also load from a URL: HTML('https://weasyprint.org/')\n# Or from a file: HTML(filename='my_document.html')\n\n# Create an HTML object from a string\nhtml = HTML(string=html_content)\n\n# Optionally, add external CSS (or inline as shown above)\n# css = CSS(filename='style.css') # or CSS(string='body { font-size: 12pt; }')\n\n# Write the PDF to a file\nhtml.write_pdf('output.pdf')\n\nprint(\"PDF generated successfully as output.pdf\")","lang":"python","description":"This quickstart demonstrates how to convert a simple HTML string with embedded CSS into a PDF file using WeasyPrint. You can also load HTML from a URL or a local file. The `write_pdf` method saves the rendered document to the specified filename."},"warnings":[{"fix":"Upgrade your Python environment to 3.10 or later. Consider using a virtual environment.","message":"WeasyPrint versions 67.0 and higher require Python 3.10 or newer. Python 3.9 and older are no longer supported.","severity":"breaking","affected_versions":">=67.0"},{"fix":"Migrate custom URL fetchers to use the new `URLFetcher` class and ensure they return `URLFetcherResponse` instances.","message":"The `default_url_fetcher()` function is deprecated since v68.0 and will be removed in v69.0. Additionally, custom URL fetchers returning dictionaries are deprecated; they should return `URLFetcherResponse` objects instead.","severity":"breaking","affected_versions":">=68.0 (deprecation), >=69.0 (removal)"},{"fix":"Update your code to be compatible with `tinyhtml5` if you were relying on `html5lib`-specific behaviors or public APIs other than basic parsing. Review the `tinyhtml5` documentation for differences.","message":"WeasyPrint 63.0 replaced its underlying HTML parsing library `html5lib` with `tinyhtml5`. While `tinyhtml5` is a fork of `html5lib`, specific advanced features or internal APIs of `html5lib` may no longer be available or behave differently.","severity":"breaking","affected_versions":">=63.0"},{"fix":"Consult the WeasyPrint documentation for platform-specific installation instructions for these system dependencies (e.g., `apt install libpango-1.0-0` on Debian/Ubuntu, `brew install pango libffi` on macOS, or MSYS2/GTK+ on Windows).","message":"WeasyPrint relies on external system libraries like Pango, Cairo, and GDK-Pixbuf (often provided by GTK+ on Windows) for rendering. These are *not* installed by `pip` and must be installed separately based on your operating system.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Sanitize all untrusted input thoroughly. For server-side usage, implement a custom URL fetcher with strict access controls and timeouts, and be mindful of `file://` URIs.","message":"Using WeasyPrint with untrusted HTML or CSS can lead to various security vulnerabilities, including local file access, system information leaks, and infinite network requests/loops.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Report the false positive to your antivirus vendor. This is a known issue documented by the WeasyPrint team.","message":"Some antivirus software may incorrectly flag WeasyPrint as malware (false positive).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-06T00:00:00.000Z","next_check":"2026-07-05T00:00:00.000Z"}