Skip to main content

PDF Viewer for Flet

https://github.com/user-attachments/assets/9b30611e-b8b4-45fa-a8b6-a7a50d9a234d

PdfViewer is a powerful PDF viewer component for Flet applications. It provides a feature-rich interface for displaying and interacting with PDF documents in your Flet applications.

Installation

pip install flet_pdfviewer

Basic Usage

Here's a simple example of how to use the FletPdfviewer component:

import flet as ft
from flet_pdfviewer import FletPdfviewer

def main(page: ft.Page):
    pdf_viewer = FletPdfviewer(
        source="https://example.com/sample.pdf",
        source_type="network",
        show_bookmark=True,
        enable_double_tap_zooming=True
    )
    
    page.add(pdf_viewer)

ft.app(target=main)

Advanced Example

Here's a more comprehensive example that demonstrates various features:

import flet as ft
from flet_pdfviewer import FletPdfviewer

def main(page: ft.Page):
    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER

    # Create PDF viewer with event handling
    def handle_zoom_changed(e):
        print(f"Zoom level changed: {e}")

    pdf_viewer = FletPdfviewer(
        source="https://example.com/sample.pdf",
        source_type="network",
        show_bookmark=True,
        enable_double_tap_zooming=True,
        on_zoom_level_changed=handle_zoom_changed,
        password="optional_password",  # If PDF is password-protected
        expand=True
    )

    # Add controls for PDF interaction
    def jump_to_page_3(e):
        pdf_viewer.jump_to_page(3)

    def set_zoom_2x(e):
        pdf_viewer.set_zoom_level(2.0)

    page.add(
        pdf_viewer,
        ft.Row(
            [ft.ElevatedButton("Go to Page 3", on_click=jump_to_page_3),
             ft.ElevatedButton("Zoom 2x", on_click=set_zoom_2x)]
        )
    )

ft.app(target=main)

API Reference

Constructor Parameters

  • source (Optional[str]): The source of the PDF document. Can be a URL, file path, or asset path.
  • source_type (Optional[str]): Type of the source. Can be "network", "file", or "asset". Default is "network".
  • show_bookmark (Optional[bool]): Whether to show the bookmark panel. Default is True.
  • memory_bytes (Optional[str]): PDF data as bytes in memory.
  • password (Optional[str]): Password for protected PDF documents.
  • enable_double_tap_zooming (Optional[bool]): Enable zooming with double tap. Default is True.
  • on_zoom_level_changed (Optional[Callable]): Callback function for zoom level changes.

Standard Control Properties

  • width (OptionalNumber): Control width.
  • height (OptionalNumber): Control height.
  • expand (Optional[bool]): Whether the control should expand to fill available space.
  • opacity (OptionalNumber): Control opacity.
  • tooltip (Optional[str]): Tooltip text.
  • visible (Optional[bool]): Control visibility.

Properties

  • source: Get or set the PDF source.
  • source_type: Get or set the source type ("network", "file", "asset").
  • show_bookmark: Get or set bookmark panel visibility.
  • memory_bytes: Get or set PDF data in memory.
  • password: Get or set PDF password.
  • enable_double_tap_zooming: Get or set double tap zooming.

Methods

  • open_bookmark_view(): Opens the bookmark view.
  • jump_to_page(page_number: int): Jumps to the specified page number.
  • set_zoom_level(zoom_level: float): Sets the zoom level (between 1.0 and 3.0).
  • get_current_page() -> int: Gets the current page number (async).
  • get_current_zoom_level() -> float: Gets the current zoom level (async).
  • get_total_pages() -> int: Gets the total number of pages (async).

Events

  • on_zoom_level_changed: Triggered when zoom level changes.

Notes

  • For network PDFs, ensure the URL is accessible and the PDF is not blocked by CORS policies.
  • Password-protected PDFs require the correct password to be set before viewing.
  • Zoom levels are typically constrained between 1.0 and 3.0.
  • Async methods should be awaited when called.

Metadata

Release files for flet-pdfviewer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flet-pdfviewer 0.1.0
File Size Uploaded
flet_pdfviewer-0.1.0.tar.gz 12.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flet-pdfviewer 0.1.0
File Interpreter ABI Platform
flet_pdfviewer-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.0 kB

Release files / flet_pdfviewer-0.1.0.tar.gz

Download URL flet_pdfviewer-0.1.0.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
871502fac04cfdf7386173b04cf4a402137936ae2d8e4eb15fae947bac8d3db7
BLAKE2b-256 checksum
How to use checksums
8fa4994c022e3d58cb97af4a18a8a7e47a5b767d5028224a9f0316b6bc04e706
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / flet_pdfviewer-0.1.0-py3-none-any.whl

Download URL flet_pdfviewer-0.1.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99dff3e6991aece4cc8fa5ac8dd985f4e19b1163f52e848658931bd972833961
BLAKE2b-256 checksum
How to use checksums
5dd0c84a1fb773e3c31c7f5f553fdfb3b185ff89c16c583a5d0f41734b2d5474
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page