Streamlit PDF Viewer

0.0.28 · active · verified Thu Apr 16

streamlit-pdf-viewer is an active Streamlit component designed for visualizing and manipulating PDF documents directly within Streamlit applications. It currently stands at version 0.0.28, with a consistent release cadence, frequently adding new features like scroll behavior, zoom controls, and annotation capabilities. The library leverages pdf.js for rendering and supports annotations, text rendering, and various display options.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to display a PDF from a URL and how to upload and display a PDF file using `st.file_uploader`, ensuring the uploaded file persists across Streamlit reruns with `st.session_state`.

import streamlit as st
from streamlit_pdf_viewer import pdf_viewer
import os

st.set_page_config(layout="wide")

# Example 1: Display a PDF from a URL
st.subheader("PDF from URL")
pdf_viewer("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf", width=700)

# Example 2: Display an uploaded PDF file
st.subheader("Upload and View PDF")

# Initialize session state for the uploaded file to persist across reruns
if 'uploaded_pdf_file' not in st.session_state:
    st.session_state.uploaded_pdf_file = None

uploaded_file = st.file_uploader("Choose a PDF file", type=("pdf"), key="pdf_uploader")

if uploaded_file is not None:
    st.session_state.uploaded_pdf_file = uploaded_file.getvalue()

if st.session_state.uploaded_pdf_file is not None:
    pdf_viewer(st.session_state.uploaded_pdf_file, width=700, height=600)
else:
    st.info("Please upload a PDF file to view.")

view raw JSON →