{"id":2847,"library":"xhtml2pdf","title":"xhtml2pdf","description":"xhtml2pdf is an active Python library that converts HTML and CSS into PDF documents, leveraging the ReportLab Toolkit, html5lib, and pypdf. It supports HTML5 and CSS 2.1 (with some CSS 3), offering a platform-independent solution for generating PDFs from web content. The project maintains a regular release cadence, with the current version being 0.2.17.","status":"active","version":"0.2.17","language":"en","source_language":"en","source_url":"https://github.com/xhtml2pdf/xhtml2pdf","tags":["pdf-generation","html-to-pdf","css-to-pdf","reporting","document-generation"],"install":[{"cmd":"pip install xhtml2pdf","lang":"bash","label":"Basic Install"},{"cmd":"pip install xhtml2pdf[pycairo]","lang":"bash","label":"Install with PyCairo (Recommended for CJK fonts/complex rendering)"}],"dependencies":[{"reason":"Core toolkit for PDF generation, handles low-level PDF primitives.","package":"reportlab","optional":false},{"reason":"HTML parsing and tree building.","package":"html5lib","optional":false},{"reason":"PDF manipulation (e.g., joining PDFs).","package":"pypdf","optional":false},{"reason":"Recommended rendering backend for ReportLab, especially for complex text rendering like CJK fonts. Requires system-wide Cairo graphics library.","package":"pycairo","optional":true},{"reason":"Legacy rendering backend for ReportLab, an alternative to PyCairo.","package":"renderpm","optional":true}],"imports":[{"note":"The `XML2PDF` and `XHTML2PDF` functions were deprecated in v0.2.7 and are slated for removal. The `pisa.CreatePDF` function is the recommended and widely used entry point for HTML to PDF conversion.","wrong":"from xhtml2pdf import XML2PDF","symbol":"pisa","correct":"from xhtml2pdf import pisa"}],"quickstart":{"code":"from io import BytesIO\nfrom xhtml2pdf import pisa\n\nhtml_content = '''\n<html>\n  <head>\n    <style>\n      @page { size: A4 portrait; margin: 1cm; }\n      h1 { color: #333; }\n      p { font-family: sans-serif; }\n    </style>\n  </head>\n  <body>\n    <h1>Hello from xhtml2pdf!</h1>\n    <p>This is a simple HTML to PDF conversion example.</p>\n    <p>Visit <a href=\"https://github.com/xhtml2pdf/xhtml2pdf\">xhtml2pdf on GitHub</a>.</p>\n  </body>\n</html>\n'''\n\ndef convert_html_to_pdf(source_html, output_filename):\n    result_file = open(output_filename, 'w+b') # Open in binary write mode\n    pisa_status = pisa.CreatePDF(\n        source_html,    # the HTML to convert\n        dest=result_file)  # file handle to receive result\n    result_file.close()    # close output file\n\n    if pisa_status.err:\n        print(f\"PDF creation failed with errors: {pisa_status.err}\")\n        return False\n    else:\n        print(f\"PDF created successfully: {output_filename}\")\n        return True\n\n# Example usage:\nconvert_html_to_pdf(html_content, \"example.pdf\")\n","lang":"python","description":"This quickstart demonstrates how to convert an HTML string into a PDF file using `pisa.CreatePDF`. The function takes the HTML content and a file handle (opened in binary write mode) as arguments. It then saves the generated PDF to the specified output file."},"warnings":[{"fix":"Ensure your project runs on Python 3.8 or a later version.","message":"Python 2 support was officially dropped in version 0.2.6. As of version 0.2.12, Python 3.7 is no longer supported either. The library now requires Python 3.8 or newer.","severity":"breaking","affected_versions":">=0.2.6 (Python 2), >=0.2.12 (Python 3.7)"},{"fix":"Migrate your code to use `from xhtml2pdf import pisa` and call `pisa.CreatePDF(...)` instead.","message":"The direct imports `XML2PDF` and `XHTML2PDF` (e.g., `from xhtml2pdf import XML2PDF`) were deprecated in v0.2.7 and are scheduled for removal in future versions. While `HTML2PDF` was mentioned as an alternative, the primary and recommended entry point for conversion is `pisa.CreatePDF`.","severity":"deprecated","affected_versions":">=0.2.7"},{"fix":"Always check the `xhtml2pdf` release notes or `pyproject.toml` for the exact `ReportLab` version compatibility, or install `xhtml2pdf` without specifying `ReportLab` to let pip resolve dependencies correctly.","message":"Compatibility with `ReportLab` versions is a recurring issue. Specifically, `xhtml2pdf 0.2.16` added compatibility for `reportlab >= 4.1`. Earlier versions like `0.2.12` and `0.2.11` had specific dependencies on `reportlab >= 4.0.4` and `reportlab >=3.5.53,<4` respectively. Mismatched versions can lead to unexpected errors.","severity":"gotcha","affected_versions":"All versions, specifically 0.2.11, 0.2.12, 0.2.16"},{"fix":"Ensure that `pypdf` is installed in your environment. If you directly import or rely on the underlying PDF library, update your imports/calls from `PyPDF2` or `PyPDF3` to `pypdf`.","message":"The underlying PDF library dependency for merging and other operations has changed. `PyPDF2` was replaced with `PyPDF3` in some earlier 0.2.x releases, and then `PyPDF3` was changed to `pypdf` in version 0.2.9.","severity":"breaking","affected_versions":">=0.2.9"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}