我正在使用下面的代码将.pdf转换为.png,它可以转换,但当我下载并打开文件时,它会说:“看起来我们不支持这种文件格式”
添加了GhostScript二进制作为层,并向lambda添加了S3 bucket触发器
import sys
import os
import boto3
import urllib
import subprocess
import traceback
_SUPPORTED_FILE_EXTENSION = '.pdf'
s3 = boto3.client('s3')
# Absolute path to Ghostscript executable here or command name if Ghostscript is
# in your PATH.
GHOSTSCRIPTCMD = "gs"
def pdf_to_image(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
s3_file_path = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
if not s3_file_path.endswith(_SUPPORTED_FILE_EXTENSION):
raise Exception("Only .pdf files are supported by this module.")
try:
pdf_file = '/tmp/input.pdf'
s3.download_file(bucket_name , s3_file_path, pdf_file)
if os.path.isfile(pdf_file):
print("Given file downloaded to, Path : {}.".format(pdf_file))
gs_pdf_to_png(pdf_file, 300, s3_file_path)
except Exception as e:
raise e
def gs_pdf_to_png(pdffilepath, resolution, s3_file_path):
if not os.path.isfile(pdffilepath):
print("Given file dose not exists, Path : {}.".format(pdffilepath))
pdffiledir = os.path.dirname(pdffilepath)
pdffilename = os.path.basename(pdffilepath)
pdfname, ext = os.path.splitext(pdffilepath)
try:
pngpath = "{}-Page%d.png".format(pdfname)
cmd = '%s -dSAFER -sstdout=%%stderr -dBATCH -dNOPAUSE -sDEVICE=png16m -r300 -sOutputFile=%s %s' % (GHOSTSCRIPTCMD, pngpath, pdffilepath)
print(cmd)
os.system(cmd)
pngpath = "{}-Page1.png".format(pdfname)
if os.path.isfile(pngpath):
print("Converted PNG file saved to, Path : {}.".format(pngpath))
DESTS3BUCKET = "Dest-poc1"
key = "{}{}".format(os.path.dirname(s3_file_path), os.path.basename(pngpath))
print("Uploading PNG file to destination Bucket : {}, Key : {}.".format(DESTS3BUCKET, key))
s3.put_object(Body=pngpath, Bucket=DESTS3BUCKET, Key=key)
except OSError as e:
print("OSError while running Ghostscript subprocess. err: {}".format(e))
raise e
except Exception as e:
print("Error while running Ghostscript subprocess. err: {}".format(e))
raise e
我花了很多时间来解决这个问题,但是运气不好,如果你愿意,请帮我解决
提前谢谢
目前没有回答
相关问题 更多 >
编程相关推荐