Book

class plutoprint.Book(size: PageSize = PAGE_SIZE_A4, margins: PageMargins = PAGE_MARGINS_NORMAL, media: MediaType = MEDIA_TYPE_PRINT)[source]
__init__(size: PageSize = PAGE_SIZE_A4, margins: PageMargins = PAGE_MARGINS_NORMAL, media: MediaType = MEDIA_TYPE_PRINT) None[source]

Initializes a new Book instance with the specified page size, margins, and media type.

Parameters:
  • size – The initial page size.

  • margins – The initial page margins.

  • media – The media type used for media queries.

get_viewport_width() float[source]

Returns the width of the viewport.

Returns:

The width of the viewport in pixels.

get_viewport_height() float[source]

Returns the height of the viewport.

Returns:

The height of the viewport in pixels.

get_document_width() float[source]

Returns the width of the document.

Returns:

The width of the document in pixels.

get_document_height() float[source]

Returns the height of the document.

Returns:

The height of the document in pixels.

get_page_count() int[source]

Returns the number of pages in the document.

Returns:

The number of pages.

get_page_size() PageSize[source]

Returns the initial page size.

Returns:

The initial page size.

get_page_size_at(page_index: int) PageSize[source]

Returns the size of the page at the specified index.

Parameters:

page_index – The index of the page.

Returns:

The size of the specified page.

get_page_margins() PageMargins[source]

Returns the initial page margins.

Returns:

The initial page margins.

get_media_type() MediaType[source]

Returns the media type.

Returns:

The media type.

set_metadata(metadata: PDFMetadata, value: str) None[source]

Sets the metadata of the PDF document.

The PDF_METADATA_CREATION_DATE and PDF_METADATA_MODIFICATION_DATE values must be in ISO-8601 format: YYYY-MM-DDThh:mm:ss. An optional timezone of the form “[+/-]hh:mm” or “Z” for UTC time can be appended. All other metadata values can be any string.

Parameters:
  • metadata – The type of metadata to set.

  • value – The value of the metadata.

get_metadata(metadata: PDFMetadata) str[source]

Gets the value of the specified metadata.

Parameters:

metadata – The type of metadata to get.

Returns:

The value of the specified metadata.

load_url(url: str, user_style: str = ..., user_script: str = ...) None[source]

Loads the document from the specified URL.

Parameters:
  • url – The URL to load the document from.

  • user_style – An optional user-defined style to apply.

  • user_script – An optional user-defined script to run after the document has loaded.

load_data(data: str | bytes, mime_type: str = ..., text_encoding: str = ..., user_style: str = ..., user_script: str = ..., base_url: str = ...) None[source]

Loads the document from the specified data.

Parameters:
  • data – The data to load the document from.

  • mime_type – The MIME type of the data.

  • text_encoding – The text encoding of the data.

  • user_style – An optional user-defined style to apply.

  • user_script – An optional user-defined script to run after the document has loaded.

  • base_url – The base URL for resolving relative URLs.

load_image(data: str | bytes, mime_type: str = ..., text_encoding: str = ..., user_style: str = ..., user_script: str = ..., base_url: str = ...) None[source]

Loads the document from the specified image data.

Parameters:
  • data – The image data to load the document from.

  • mime_type – The MIME type of the data.

  • text_encoding – The text encoding of the data.

  • user_style – An optional user-defined style to apply.

  • user_script – An optional user-defined script to run after the document has loaded.

  • base_url – The base URL for resolving relative URLs.

load_xml(data: str, user_style: str = ..., user_script: str = ..., base_url: str = ...) None[source]

Loads the document from the specified XML data.

Parameters:
  • data – The XML data to load the document from.

  • user_style – An optional user-defined style to apply.

  • user_script – An optional user-defined script to run after the document has loaded.

  • base_url – The base URL for resolving relative URLs.

load_html(data: str, user_style: str = ..., user_script: str = ..., base_url: str = ...) None[source]

Loads the document from the specified HTML data.

Parameters:
  • data – The HTML data to load the document from.

  • user_style – An optional user-defined style to apply.

  • user_script – An optional user-defined script to run after the document has loaded.

  • base_url – The base URL for resolving relative URLs.

clear_content() None[source]

Clears the content of the document.

render_page(canvas: Canvas, page_index: int) None[source]

Renders the specified page to the given canvas.

Parameters:
  • canvas – The canvas to render the page on.

  • page_index – The index of the page to render.

render_document(canvas: Canvas, rect: Tuple[float, float, float, float] = ...) None[source]

Renders the entire document to the given canvas.

Parameters:
  • canvas – The canvas to render the document on.

  • rect – The rectangle specifying the area to render.

write_to_pdf(path: str | bytes | PathLike, page_start: int = MIN_PAGE_COUNT, page_end: int = MAX_PAGE_COUNT, page_step: int = 1) None[source]

Writes the document to a file at the specified path as PDF.

Parameters:
  • path – The file path where the PDF document will be written.

  • page_start – The first page in the range to be written (inclusive).

  • page_end – The last page in the range to be written (inclusive).

  • page_step – The increment used to advance through pages in the range.

write_to_pdf_stream(stream: BinaryIO, page_start: int = MIN_PAGE_COUNT, page_end: int = MAX_PAGE_COUNT, page_step: int = 1) None[source]

Writes the document to a writable binary stream as PDF.

Parameters:
  • stream – The writable binary stream where the PDF document will be written.

  • page_start – The first page in the range to be written (inclusive).

  • page_end – The last page in the range to be written (inclusive).

  • page_step – The increment used to advance through pages in the range.

write_to_png(path: str | bytes | PathLike, width: int = -1, height: int = -1) None[source]

Writes the document to a file at the specified path as PNG.

Parameters:
  • path – The file path where the PNG image will be written.

  • width – The desired width in pixels, or -1 to auto-scale based on the document size.

  • height – The desired height in pixels, or -1 to auto-scale based on the document size.

write_to_png_stream(stream: BinaryIO, width: int = -1, height: int = -1) None[source]

Writes the document to a writable binary stream as PNG.

Parameters:
  • stream – The writable binary stream where the PNG image will be written.

  • width – The desired width in pixels, or -1 to auto-scale based on the document size.

  • height – The desired height in pixels, or -1 to auto-scale based on the document size.

custom_resource_fetcher: ResourceFetcher | None = None

Optional fetcher that overrides the default when fetching resources for this document.

plutoprint.MIN_PAGE_COUNT: int = Ellipsis

Represents a sentinel value less than any valid page count.

plutoprint.MAX_PAGE_COUNT: int = Ellipsis

Represents a sentinel value greater than any valid page count.