Last active
February 9, 2023 13:39
-
-
Save HiCraigChen/942f61c91160de82d56912cdde30ce89 to your computer and use it in GitHub Desktop.
Convert PDF to image using Python with pdf2image library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chalice import Chalice, Response | |
from pdf2image import convert_from_bytes | |
import os | |
from io import BytesIO | |
app = Chalice(app_name='pdf2image') | |
@app.route('/') | |
def index(): | |
# Download and read pdf file | |
os.system("curl https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf -o /tmp/file.pdf") | |
f = open('/tmp/file.pdf','rb') | |
infile = f.read() | |
f.close() | |
# Set poppler path | |
poppler_path = "/var/task/lib/poppler-utils-0.26/usr/bin" | |
images = convert_from_bytes(infile,dpi=150,poppler_path=poppler_path) | |
for img_page in images: | |
img_page.save("/tmp/file.jpg","jpeg") | |
f = open('/tmp/file.jpg','rb') | |
out = BytesIO(f.read()) | |
return Response( | |
out.getvalue(), | |
status_code=200, | |
headers={'Content-Type': 'image/jpeg'} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment