AWS Lambda上的PDF2图像

2024-06-17 12:07:46 发布

您现在位置:Python中文网/ 问答频道 /正文

在AWS Lambda上使用PDF2Image时,我遇到以下问题。这是我在本地机器和AWS Lambda上运行的代码

def lambda_handler(event, context):
    source = "somebucket/folder/test.pdf"
    source_obj = s3.Object(event['bucket'], source).get()
    body = source_obj['Body'].read()    
    filename, extension = os.path.splitext(source)
    info = pdfinfo_from_bytes(body, userpw=None, poppler_path=None)
    maxPages = info['Pages']
    
   if maxPages > MAX_PAGES:
        maxPages = MAX_PAGES
            
    for index in range(1, maxPages+1,2):
     
        pages = convert_from_bytes(
            body,
            first_page=index,
            last_page=index+1,
            jpegopt={
                "quality": 20,
                "progressive": True,
                "optimize": True
            }
        )
        imageIndex = 0
        for page in pages:
           
            # Converting PDF file to imae
            in_mem_file = io.BytesIO()
            page.save(in_mem_file,format="JPEG")
            print(in_mem_file.getbuffer().nbytes)
            destination = f"{filename}_{index}_{imageIndex}.jpg" 
            object = s3.Object(event['bucket'],destination)
            object.put(Body=in_mem_file.getvalue())
            in_mem_file.close()
            imageIndex = imageIndex + 1

当我在本地机器上运行此程序时(基本上是相同的代码,仅不包括AWS Lambda部分),我看到:

354597

在Lambda环境中运行时,我会看到以下内容:

108963

所以,基本上,在Lambda上生成的JPEG比在本地机器上生成的JPEG小。当我比较结果文件时,我看到由AWS Lambda生成的文件缺少原始PDF文件中的一些文本

Lambda功能存储器设置为4096 Mb PDF文件大小为26 Kb

不幸的是,我无法共享该文件,因为它属于保密协议

提前谢谢


Tags: 文件lambdain机器awseventsourceindex