Skip to content

Instantly share code, notes, and snippets.

@insightsbees
Last active May 14, 2024 12:06
Show Gist options
  • Save insightsbees/f7b4388c85ac750856f8722c94199566 to your computer and use it in GitHub Desktop.
Save insightsbees/f7b4388c85ac750856f8722c94199566 to your computer and use it in GitHub Desktop.
Display PDF in Streamlit
def show_pdf(file_path):
with open(file_path,"rb") as f:
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="800" height="800" type="application/pdf"></iframe>'
st.markdown(pdf_display, unsafe_allow_html=True)
show_pdf('post1-compressed.pdf')
@Royz2123
Copy link

This code is blocked when deploying to the streamlit community cloud - any idea why? I'm getting a "https://cs-practice-7gbnpyxkmg9.streamlit.app/"

@Royz2123
Copy link

Sorry, getting "This page has been blocked by Chrome" ***

@quantuan125
Copy link

Hello @Royz2123

Have you tried to get it to work?

@Royz2123
Copy link

@quantuan125 - yeah, I tried quite a bit but sadly no luck 😔

The best workaround I found was converting the PDF to an image and displaying it instead. Not ideal but does the job 🤷‍♂️

@quantuan125
Copy link

Hi @Royz2123

What solution did you find best in displaying the PDF as image?

I tried with fitz and PIL however, it kinda array display all images (=pages) from the PDF on the webapp. Do you use a placeholder or an image viewer or somethign that allows the ability to show one image and and "next"and "previous" button?

Thank you in advance

@Royz2123
Copy link

So basically I used the pdf2jpg library for converting the pdf into one long scrollable image. You can find the full code here, but the relevant snippet is:

if display_method == "images":
        # Create temporary folder for generated image
        tmp_sub_folder_path = create_tmp_sub_folder()

        # Save images in that sub-folder
        result = pdf2jpg.convert_pdf2jpg(pdf_path, tmp_sub_folder_path, pages="ALL")
        images = []
        for image_path in result[0]["output_jpgfiles"]:
            images.append(np.array(Image.open(image_path)))

        # Create merged image from all images + remove irrelevant whitespace
        merged_arr = np.concatenate(images)
        merged_arr = crop_white_space(merged_arr)
        merged_path = os.path.join(tmp_sub_folder_path, "merged.jpeg")
        Image.fromarray(merged_arr).save(merged_path)

        # Display the image
        st.image(merged_path)
        try_remove(tmp_sub_folder_path)

@lfoppiano
Copy link

FYI. You can use a new component for pdf rendering, and more: https://github.com/lfoppiano/streamlit-pdf-viewer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment