Python API

The main function of xhtml2pdf is CreatePDF().

xhtml2pdf.pisa.CreatePDF(src, dest=None, dest_bytes=False, path='', link_callback=None, debug=0, default_css=None, xhtml=False, encoding=None, xml_output=None, raise_exception=True, capacity=100 * 1024, context_meta=None, encrypt=None, signature=None, **_kwargs)

Create PDF.

Parameters:
  • src (str|io.BufferedIOBase) – The source to be parsed.

  • dest (io.BufferedIOBase) – The destination for the resulting PDF. This has to be a file object which will not be closed afterwards.

  • dest_bytes (bool) – If true, will return the data written to the file.

  • path (str) – The original file path or URL. This is needed to calculate the relative paths of images and stylesheets.

  • link_callback (Callable[str|pathlib.Path|None, str|None]) – Handler for special file paths (see below).

  • debug (int) –

    deprecated:

    this parameter is unused

  • default_css (str) – The default CSS definition. If None, the predefined CSS of xhtml2pdf is used.

  • xhtml (bool) –

    Force parsing the source as HTML. If omitted, the parser will try to guess this.

    deprecated:

    XHTML parsing will be removed in v0.2.8

  • encoding (str) – The source encoding. If omitted, this will be guessed by the HTML5 parser. This is helpful if the HTML file does not have <meta charset>.

  • xml_output (io.BufferedIOBase) – If given, the XML output of the document tree (not the document itself!) will be written to this file/buffer.

  • raise_exception (bool) –

    deprecated:

    this parameter is unused

  • capacity (int) – The capacity of the internal buffer, in bytes. If the document is bigger than the buffer, it will be managed in a temporary file rather than in-memory

  • context_meta (dict[str, str | tuple[float, float]]) – Metadata for the PDF document. May include fields: author, title, subject, keywords, and pagesize.

  • encrypt (str|reportlab.lib.pdfencrypt.StandardEncryption) – Either a password to protect the PDF with or a pre-built instance of encryption parameters.

  • signature (dict[str, Any]) – Signature parameters. Should contain at least engine with the values of "pkcs12", "pkcs11", or "simple".

Returns:

Return type:

xhtml2pdf.document.pisaStory|bytes