Skip to content

Commit

Permalink
correct arguments for page vertical spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
lfoppiano committed Jan 18, 2024
1 parent 7260a92 commit 37f5863
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
15 changes: 8 additions & 7 deletions streamlit_pdf_viewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
)


def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = None, key=None, annotations=[],
page_margin=2, annotation_outline_size=1):
def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = None, key=None,
annotations=(),
pages_vertical_spacing=2,
annotation_outline_size=1
):
"""
pdf_viewer function to display a PDF file in a Streamlit app.
Expand All @@ -32,10 +35,8 @@ def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = N
:param height: Height of the PDF viewer in pixels. If not provided, the viewer show the whole content.
:param key: An optional key that uniquely identifies this component. Used to preserve state in Streamlit apps.
:param annotations: A list of annotations to be overlaid on the PDF. Each annotation should be a dictionary.
:param page_margin: The margin (in pixels) between each page of the PDF. It adjusts the spacing between pages.
Defaults to 2 pixels.
:param annotation_outline_size: Size of the outline around each annotation in pixels.
Defaults to 1 pixel.
:param pages_vertical_spacing: The vertical space (in pixels) between each page of the PDF. Defaults to 2 pixels.
:param annotation_outline_size: Size of the outline around each annotation in pixels. Defaults to 1 pixel.
The function reads the PDF file (from a file path, URL, or binary data), encodes it in base64,
and uses a Streamlit component to render it in the app. It supports optional annotations and adjustable margins.
Expand All @@ -57,7 +58,7 @@ def pdf_viewer(input: Union[str, Path, bytes], width: int = 700, height: int = N

base64_pdf = base64.b64encode(binary).decode('utf-8')
component_value = _component_func(binary=base64_pdf, width=width, height=height, key=key, default=0,
annotations=annotations, page_margin=page_margin,
annotations=annotations, pages_vertical_spacing=pages_vertical_spacing,
annotation_outline_size=annotation_outline_size)
return component_value

Expand Down
8 changes: 4 additions & 4 deletions streamlit_pdf_viewer/frontend/src/PdfViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default {
const pageIndex = page - 1;
let height = 0;
for (let i = 0; i < pageIndex; i++) {
height += Math.floor(pageHeights.value[i] * pageScales.value[i]) + props.args.page_margin; // Add margin for each page
height += Math.floor(pageHeights.value[i] * pageScales.value[i]) + props.args.pages_vertical_spacing; // Add margin for each page
}
return height;
};
Expand Down Expand Up @@ -73,7 +73,7 @@ export default {
canvas.height = viewport.height;
canvas.width = viewport.width;
canvas.style.display = "block";
canvas.style.marginBottom = `${props.args.page_margin}px`;
canvas.style.marginBottom = `${props.args.pages_vertical_spacing}px`;
return canvas;
};
Expand Down Expand Up @@ -103,11 +103,11 @@ export default {
maxWidth.value = canvas.width;
}
totalHeight.value += canvas.height;
totalHeight.value += props.args.page_margin;
totalHeight.value += props.args.pages_vertical_spacing;
await renderPage(page, canvas);
}
// Subtract the margin for the last page as it's not needed
totalHeight.value -= props.args.page_margin;
totalHeight.value -= props.args.pages_vertical_spacing;
};
Expand Down

0 comments on commit 37f5863

Please sign in to comment.