PDF Serializer Reference

class pdfnaut.cos.serializer.PdfSerializer[source]

Bases: object

A serializer that is able to produce a valid PDF document.

Parameters:

eol (bytes, optional) – The end of line sequence to use when serializing. One of CR, LF, or CRLF. Defaults to CRLF.

__init__(*, eol: Literal[b'\r\n', b'\r', b'\n'] = b'\r\n') None[source]
content

The buffer containing the serialized content to be written.

generate_xref_section(rows: list[tuple[int, FreeXRefEntry | InUseXRefEntry | CompressedXRefEntry]]) list[PdfXRefSubsection][source]

Generates a cross-reference section from a list of rows.

Each row consists of a two-element tuple containing the object number of the XRef entry and the entry itself. The object numbers will determine the amount of subsections created and the entries within them.

The output is a list of XRef subsections that can be then serialized by either write_standard_xref_section() or write_compressed_xref_section().

write_compressed_xref_section(section: PdfXRefSection) int[source]

Appends a compressed XRef stream (see ISO 32000-2:2020 § 7.5.8 “Cross-Reference Streams”) from section (to use as part of the extent) to the document.

Returns the startxref offset that should be written to the document.

write_eof() None[source]

Appends the end of file (EOF) marker to the document.

write_header(version: str, *, with_binary_marker: bool = True) None[source]

Appends the PDF file header to the document (see § 7.5.2, “File Header”).

Parameters:
  • version (str) – A string representing the version of the PDF file.

  • with_binary_marker (bool, optional) – Whether to also append the recommended binary marker. Defaults to True.

write_object(reference: PdfReference | tuple[int, int], contents: bool | int | float | bytes | PdfArray | PdfDictionary | PdfHexString | PdfName | PdfReference | PdfNull | PdfStream) int[source]

Appends an indirect object to the document.

Parameters:
  • reference (PdfReference | tuple[int, int]) – The object number and generation to which the object should be assigned.

  • contents (PdfObject | PdfStream) – The contents to associate with the reference.

Returns:

The offset where the indirect object starts.

Return type:

int

write_standard_xref_section(subsections: list[PdfXRefSubsection]) int[source]

Appends a standard XRef section (see ISO 32000-2:2020 § 7.5.4 “Cross-Reference Table”) to the document. Returns the startxref offset that should be written to the document.

write_trailer(trailer: PdfDictionary | None = None, startxref: int | None = None) None[source]

Appends a standard trailer to the document (see ISO 32000-2:2020 § 7.5.5 “File Trailer”) alongside the startxref offset.

Both arguments are optional, indicating their presence in the appended output. If the XRef section written previously was an XRef stream, the trailer has already been written and should be None.