
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
streamlit-pdf-viewer
Advanced tools
Streamlit component that allows the visualisation and enrichment of PDF documents. You can see an application in action here.
Here some caveats to be aware of:
width
to show PDF document on tabs and expanders, otherwise, the viewer will not be
displayed on tabs not immediately visible.pdf_viewer()
into a
st.component(width:...)
setting the width.legacy
rendering is not supported on Chrome, due to security reasons.pip install streamlit-pdf-viewer
In your streamlit application, you can use it as:
import streamlit as st
from streamlit_pdf_viewer import pdf_viewer
pdf_viewer("str, path or bytes")
You can also customize the viewer with additional options:
import streamlit as st
from streamlit_pdf_viewer import pdf_viewer
# Display PDF with custom zoom, alignment, and separators
pdf_viewer(
"path/to/your/document.pdf",
width=700,
height=1000,
zoom_level=1.2, # 120% zoom
viewer_align="center", # Center alignment
show_page_separator=True # Show separators between pages
)
In the following table the list of parameters that can be provided to the pdf_viewer
function:
name | description |
---|---|
input | The source of the PDF file. Accepts a file path or binary data. |
width | Width of the PDF viewer in pixels. It defaults to 700 pixels. It supports both integer (pixel, e.g. 700 ) and string (percentages, e.g. 90% will make the pdf render to 90% of the container/window/screen width. If the pdf width is larger than the screen width, it will horizontally scroll). |
height | Height of the PDF viewer in pixels. If not provided, the viewer shows the whole content. |
annotations | A list of annotations to be overlaid on the PDF. Format is described here. |
pages_vertical_spacing | The vertical space (in pixels) between each page of the PDF. Defaults to 2 pixels. |
annotation_outline_size | Size of the outline around each annotation in pixels. Defaults to 1 pixel. |
rendering | Type of rendering: unwrap (default), legacy_iframe , or legacy_embed . The default value, unwrap shows the PDF document using pdf.js, and supports the visualisation of annotations. Other values are legacy_iframe and legacy_embed which use the legacy approach of injecting the document into an <embed> or <iframe> . They allow viewing the PDF using the viewer of the browser that contains additional features we are still working to implement in this component. IMPORTANT: :warning: The "legacy" methods work only with Firefox, and do not support annotations. :warning: |
pages_to_render | Filter the rendering to a specific set of pages. By default, all pages are rendered. |
render_text | Enable a layer of text on top of the PDF document. The text may be selected and copied. NOTE to avoid breaking existing deployments, we made this optional at first, also considering that having many annotations might interfere with the copy-paste. |
zoom_level | The zoom level of the PDF viewer. Can be a float (0.1-10.0), "auto" for fit-to-width, "auto-height" for fit-to-height, or None (defaults to auto-fit to width). When zoom controls are enabled, users can interactively adjust the zoom level. |
viewer_align | The alignment of the PDF viewer within its container. Can be "center" (default), "left" , or "right" . |
show_page_separator | Whether to show a horizontal separator line between PDF pages. Defaults to True . |
scroll_to_page | Scroll to a specific page when the component is rendered. The parameter is an integer, which represent the positional value of the page. E.g. 1, will be the first page. Default is None. Require ints and ignores the parameters below zero. |
scroll_to_annotation | Scroll to a specific annotation when the component is rendered. The parameter is an integer, which represent the positional value of the annotation. E.g. 1, will be the first annotation. Default is None (don't scroll). Mutually exclusive with scroll_to_page . Raise an exception if used with scroll_to_page |
on_annotation_click | Callback function that is called when an annotation is clicked. The function receives the annotation as a parameter. |
The annotation format has been derived from the Grobid's coordinate formats, which are described as a list of "bounding boxes".
The annotations are expressed as a dictionary of six elements; the page, x and y indicate the top left point.
The color
can be expressed following the HTML CSS convention.
The border
style also follow the HTML conventions limited to these values: solid
, dashed
, dotted
, double
, groove
, ridge
, inset
, outset
.
Any other value will result in the default value: solid
.
Annotation unique identifiers are expressed by the id
field, if id
is not specified, an identifier will be generated during rendering.
Furthermore, the HTML identifier will be generated as #annotation-{annotation.id}
.
Here is an example:
[
{
"page": 1,
"x": 220,
"y": 155,
"height": 22,
"width": 65,
"color": "red",
"border": "solid"
},
[...]
The example shown in our screenshot can be found here.
from streamlit_pdf_viewer import pdf_viewer
annotations = [
{
"page": 1,
"x": 220,
"y": 155,
"height": 22,
"width": 65,
"color": "red"
},
{
"page": 1,
"x": 220,
"y": 155,
"height": 22,
"width": 65,
"color": "red",
"border": "dotted"
}
]
def my_custom_annotation_handler(annotation):
print(f"Annotation {annotation} clicked.")
pdf_viewer(
"path/to/pdf",
on_annotation_click=my_custom_annotation_handler,
annotations=annotations
)
First, make sure that _RELEASE = False in streamlit_pdf_viewer/__init__.py
. To run the component in development mode,
use the following commands:
streamlit run streamlit_pdf_viewer/__init__.py
cd frontend
npm run serve
These commands will start the Streamlit application and serve the Node.js component. Please make sure you're in the correct directory before running these commands.
Build the frontend part:
cd frontend
export NODE_OPTIONS=--openssl-legacy-provider
npm run build
Make sure that _RELEASE = True in streamlit_pdf_viewer/__init__.py
.
move to the streamlit_application and run
pip install -e {path of component}
bump-my-version bump patch | minor | major
git push
git push --tags
The project was initiated by Luca Foppiano at the National Institute for Materials Science (NIMS) in Japan. Currently, the development is possible thanks to ScienciLAB.
Main contacts: Luca Foppiano and Tomoya Mato.
FAQs
Streamlit component for PDF visualisation and manipulation
We found that streamlit-pdf-viewer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.