Spaces:
Running
Running
Commit
·
d580476
1
Parent(s):
e4cd2b5
PDF Link support
Browse files- pdf2preview.py +21 -5
pdf2preview.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
from PIL import Image, ImageFilter, ImageOps
|
| 3 |
import sys
|
| 4 |
import io
|
|
|
|
| 5 |
import fitz #pip install PyMuPDF
|
| 6 |
|
| 7 |
def add_border(image, border):
|
|
@@ -75,13 +76,26 @@ with col1:
|
|
| 75 |
with col2:
|
| 76 |
st.image("example.png")
|
| 77 |
st.file_uploader("Upload your PDF", type="pdf", key="file")
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
| 80 |
with st.spinner("Processing..."):
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
zoom = 2
|
| 83 |
mat = fitz.Matrix(zoom, zoom)
|
| 84 |
-
num_pages = file.
|
| 85 |
imgs = []
|
| 86 |
for page_num in range(num_pages):
|
| 87 |
page = file.load_page(page_num)
|
|
@@ -99,5 +113,7 @@ if st.session_state.file is not None:
|
|
| 99 |
b1, b2, b3 = st.columns([1, 1, 1])
|
| 100 |
with b2:
|
| 101 |
download = st.download_button(label="Download image", data=output, file_name="pdf2preview.png", mime="image/png")
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image, ImageFilter, ImageOps
|
| 3 |
import sys
|
| 4 |
import io
|
| 5 |
+
from utils import download_file, remove_file
|
| 6 |
import fitz #pip install PyMuPDF
|
| 7 |
|
| 8 |
def add_border(image, border):
|
|
|
|
| 76 |
with col2:
|
| 77 |
st.image("example.png")
|
| 78 |
st.file_uploader("Upload your PDF", type="pdf", key="file")
|
| 79 |
+
st.write("or")
|
| 80 |
+
url = st.text_input("Submit link to PDF")
|
| 81 |
|
| 82 |
+
|
| 83 |
+
if st.button('Generate preview'):
|
| 84 |
with st.spinner("Processing..."):
|
| 85 |
+
if st.session_state.file is not None:
|
| 86 |
+
file = fitz.open("pdf", st.session_state.file.read())
|
| 87 |
+
else:
|
| 88 |
+
try:
|
| 89 |
+
filename = download_file(url)
|
| 90 |
+
file = fitz.open(filename)
|
| 91 |
+
except:
|
| 92 |
+
e = ValueError('Could not download pdf file from URL')
|
| 93 |
+
st.error('Please enter a valid pdf URL', icon="🚨")
|
| 94 |
+
raise e
|
| 95 |
+
|
| 96 |
zoom = 2
|
| 97 |
mat = fitz.Matrix(zoom, zoom)
|
| 98 |
+
num_pages = file.page_count
|
| 99 |
imgs = []
|
| 100 |
for page_num in range(num_pages):
|
| 101 |
page = file.load_page(page_num)
|
|
|
|
| 113 |
b1, b2, b3 = st.columns([1, 1, 1])
|
| 114 |
with b2:
|
| 115 |
download = st.download_button(label="Download image", data=output, file_name="pdf2preview.png", mime="image/png")
|
| 116 |
+
if st.session_state.file is None:
|
| 117 |
+
remove_file(filename)
|
| 118 |
+
st.markdown("By [David Chuan-En Lin](https://chuanenlin.com). PDF URL support by [
|
| 119 |
+
Eliott Zemour](https://github.com/EliottZemour). Play with the code at https://github.com/chuanenlin/pdf2preview.")
|