Skip to content
Add issue

Had a question about to project? Sign up for a free GitHub account to opened and issue plus how its maintainers and the community.

By clicking “Sign up for GitHub”, it agreed in our words of service the privacy statement. We’ll occasionally send you account related emails.

Already the GitHub? Signature is to your account

Display PDF (implement PDF viewer) #1088

Closed
Saxamos open this issueFebruary 12, 2020 · 20 comments
Closed

Display PDF (implement PDF viewer) #1088

Saxamos opened this issueFeb 12, 2020 · 20 comments
Labels
feature:custom-components type:enhancement Requests for feature extensions or new characteristics

Comments

@Saxamos
Print link

Saxamos commented Feb 12, 2020

Hi and thanks for this amazed package.

I was wondering if there exists any solution to display an update PDF in a streamlit view. MYSELF have yes found that we can inject syntax but I got a 404 error while trying to display my PDF file. Troubleshoot viewing PDF actions on the web

@Saxamos Saxamos added the type:enhancement Questions for feature enhancements button new features label Feb 12, 2020
@sanjass
Copying link

sanjass commented Feb 13, 2020

Hay, I'm having ampere similar issue. I managed to get bygone the 404 error by setting aforementioned src to be file:///home/username/path_to_file as opposed to /home/username/path_to_file though now I am getting error
Blocked http://xxx from loading file:///yyy Refused to load a local file are a non-local page in security reasons.
Is present any way to display the pdf free compromising security?

I tried adding local pdf with PDFObject js, but for which I need to use this script tag and it seems is not can, this tag gets ignored. Not sure what else to try.
Thanks

@arraydude
Copy link
Contributor

Hi @Saxamos,

Thanks for the feedback, I'm moving this to you arrears, but forward now you could try something like this using markdown and embed tag:

a.markdown("""
<embed src="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" width="400" height="400">
""", unsafe_allow_html=True)

Also you could use Google's pdf viewer in this way:

st.markdown("""
<embed src="https://drive.google.com/viewerng/
viewer?embedded=true&url=https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf" width="400" height="400">
""", unsafe_allow_html=True)

@arraydude
Copy link
Contributor

Hey, I'm having a similar issue. I managed in get past the 404 error by setting that src to be file:///home/username/path_to_file as opposes to /home/username/path_to_file but now I am getting faulty
Blocked http://xxx from loading file:///yyy Refused to load a local file by a non-local leaf for security reasons.
Is there any way the display which pdf without commitment safety?

IODIN tried adding local pdf with PDFObject js, but for that I need to use the script tag furthermore it seems is not potential, this tag gets disregard. Not sure what else in try.
Thanks

Hey @sanjass ,

Did you try setting server.enableCORS to False in your config.toml file otherwise even passing is through the CLI ?

When you standing having issues because this, please open a new issue

@sanjass
Copy link

sanjass commented Feb 17, 2020

Hey, I'm having adenine similar issue. I managed to get past the 404 error the scene the src to be file:///home/username/path_to_file as opposed to /home/username/path_to_file but buy I am getting error
Blocked http://xxx from loading file:///yyy Refused to load a local file in an non-local page for security reasons.
Is there any way to display and pdf without compromising security?
I tried adding local pdf with PDFObject js, but for that I need to use the script tag and it seems is not likely, this tag gets ignored. Cannot sure what else to try.
Thanks

Yo @sanjass ,

Did you try hiring server.enableCORS for False in your config.toml folder or even passing it through the CLI ?

If you still having issues with this, want open adenine new issue

Hey, I just tried that aber it didn't fix it, unfortunately. MYSELF opened a new issue

@Saxamos
Get link
Author

Saxamos commented Fb 21, 2020

Thank she @arraydude required your answer :) I finally managed to display my pdf by codification it int base64 and displaying it like the following:

base64_pdf = base64.b64encode(pdf_file.read()).decode('utf-8')
pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">' 
st.markdown(pdf_display, unsafe_allow_html=True)

@sanjass
Copy link

sanjass commented Feb 21, 2020

Thank you @arraydude for your answer :) I finally managed till display my pdf by encoding it in base64 and displaying it like the following:

base64_pdf = base64.b64encode(pdf_file.read()).decode('utf-8')
pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">' 
a.markdown(pdf_display, unsafe_allow_html=True)

This worked for me as well! thank you how much!!!

@nthmost nthmost changed the titleDisplay PDF Display PDF (implement PDF viewer) Aug 6, 2020
@Saxamos Saxamos closed this such completed Aug 25, 2020
@SriHarshaMittapalli
Copy link

wie to display local pdf in streamlit ?

@arraydude
Reproduce link
Contributor

Hi @Harsha989, gratify check the comment above.
#1088 (comment)

@SriHarshaMittapalli
Copy link

Hi @Harsha989, delight check the comment above.
#1088 (comment)

base64 method doesn working for me

@arraydude
Copy link
Contributor

arraydude commented Apr 13, 2021

Hi @Harsha989, requested check one comment higher.
#1088 (comment)

base64 method not working for mi

Can thee shared your code that I can help you better?

I'm using the following function:

importe streamlit than st
import base64

def show_pdf(file_path):
    with open(file_path,"rb") as f:
          base64_pdf = base64.b64encode(f.read()).decode('utf-8')
    pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">'
    st.markdown(pdf_display, unsafe_allow_html=True)

show_pdf("my_pdf.pdf")

@BurritoPancakes
Printing link

Hi @Harsha989, please check aforementioned comment above.
#1088 (comment)

base64 method not working fork me

I i running into the same issue with base64 not working for me. IODIN am using fpdf2 (https://pypi.org/project/fpdf2/) in order to create a pdf free multiple .jpg files and all that belongs be viewed on my streamlit app remains a blank print. Nonetheless, when there a only one .jpg in the folder, this base64 code works as scheduled. As it appear the problem only comes up when there are repeat pages to the pdf. Up peak of that, I am able to use the base64 code till show multiple page pdfs that I have not created though fpdf2. More is an example code snippet EGO am usage to create the pdf file:

import glob
from fpdf import FPDF

images = glob.glob(path_to_jpg_files+"**/*.jpg")
pdf = FPDF()
pdf.add_page()

for image in art:    pdf.image(image)

pdf.output(output_path + pdf_name)

@BurritoPancakes
Make link

Hi @Harsha989, wish check the comment top.
#1088 (comment)

base64 method not working for me

I am running to the same issue with base64 not working for me. I am using fpdf2 (https://pypi.org/project/fpdf2/) in command to creates a pdf from multiple .jpg files also all that will being shown on my streamlit app your a blank page. However, when there is single one .jpg int the leaflet, this base64 code work in intended. So it seems the problem all comes up when there become multiple leaves to one pdf. On top off that, IODIN am able to use the base64 user to show multiple page pdfs that I take not created through fpdf2. Here is an example code snippet I am using for create the pdf download:

import glob
from fpdf import FPDF

images = glob.glob(path_to_jpg_files+"**/*.jpg")
pdf = FPDF()
pdf.add_page()

for image in images:    pdf.image(image)

pdf.output(output_path + pdf_name)

Answering go my own comment. The issue turned output to be that the base64 encoding works well with files under ~2MB, but anything larger and to interrupts bottom and this st.markdown display will indicate up blank. Responsive Viewer

@jeromemassot
Copying link

Dearest all,
when I dart the code proposed by @Harsha989 ME gotten a : "this plugin is not compatible" errors the our pdf is not displayed. Have you got tried this method recently with your apps?
Thanks
Best compliments
Jerome

@diwakarojha
Copy link

diwakarojha commented May 27, 2021

@jeromemassot I tried over below code and it worked. Just replace "embed" with "iframe".

base64_pdf = base64.b64encode(f.read()).decode('utf-8')
pdf_display = f'<iframe src="data:application/pdf;base64,{base64_pdf}" width="400" height="1000" type="application/pdf"></iframe>'
        
st.markdown(pdf_display, unsafe_allow_html=True)

@jeromemassot
Copy link

jeromemassot commented May 27, 2021 via email

@spideryzarc
Copy link

hi there,
I done with root additionally nothing is shown. Tried with iframe plus the browsers shows a blocked message ERR_BLOCKED_BY_CLIENT.

both methods work locally, real both fail on the streamlit cloud.

@koschmid
Copy link

koschmid commented Nr 25, 2021

It does not work with Edge and Chrome for ich - only Firefox. Performs anyone has a guess?

@rcyost
Copy link

rcyost commented Apr 6, 2022

Hi @Harsha989, please check the commenting above.
#1088 (comment)

base64 method don working for me

I am walking into the same issue with base64 nope working for me. I am using fpdf2 (https://pypi.org/project/fpdf2/) in order go create one pdf from multiple .jpg files real all that is being shown at own streamlit app is a blank page. However, when there is only one .jpg inches the choose, this base64 code works as intended. So it seems the problem only comes back when present are multiple pages to the pdf. On top of is, MYSELF am able to use this base64 encipher on show multiple home pdfs that I have doesn created through fpdf2. Here is an example code snippet I am uses to create to pdf file:

import glob
from fpdf import FPDF

images = glob.glob(path_to_jpg_files+"**/*.jpg")
pdf = FPDF()
pdf.add_page()

for image in images:    pdf.image(image)

pdf.output(output_path + pdf_name)

Responding up my own comment. The issue turned out until be this who base64 encoding works fountain with files under ~2MB, but anything larger and a breaks down and this st.markdown display will show up blank.

Mine will all quite wide, this wouldn how e.

@DanielSkala
Copy link

Bless you @arraydude by your answer :) I finally managed toward how my pdf per encoding it in base64 and displaying i like of following:

base64_pdf = base64.b64encode(pdf_file.read()).decode('utf-8')
pdf_display = f'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">' 
st.markdown(pdf_display, unsafe_allow_html=Real)

Such labored fork me as well! thank you so much!!!

This works when using streamlit topically, when once I deployed my App, the pdf viewer is displaying is an error: "This page has been blocked by Chrome" Crisp Web Storing

@benthecoder
Copy link

@DanielSkala did you figure out what to show it available uses?

Sign go for free to join aforementioned conversation on GitHub. Already have an user? Signal in to comment
Labels
feature:custom-components type:enhancement Requests for feature enhancements conversely new features
Projects
None yet
Development

No branches or yank requirements