Thomas
Thomas

Reputation: 31

AWS Lambda - generating PDF issue

I'd like to generate a pdf file using AWS Lambda, put it in the S3 bucket and distribute it later to a specific group of recipients. I have managed to generate and upload the pdf to the S3 bucket, however after I manually download the pdf, I cannot open it as the file seems to be corrupted/protected (the size is greater than 0B though, so I expect the information is saved to the file).

Edit: Added full Lambda function

Below the relevant code fragment :

import json
import numpy as np
import pandas as pd
import boto3
from matplotlib.backends.backend_pdf import PdfPages

def lambda_handler(event, context):
    
    df = pd.DataFrame(np.random.random((10**2,3)), columns = ("Column1", "Column2", "Column3"))
    
    fig, ax = plt.subplots(figsize=(12,4))
    ax.axis('tight')
    ax.axis('off')
    the_table = ax.table(cellText=df.values,colLabels=df.columns,loc='center')

    
    path = "/tmp/doc.pdf"
    pp = PdfPages(path)
    pp.savefig(fig, bbox_inches='tight')
    
 
    s = boto3.client('s3')
    s.upload_file(path, 'bucket_name', 'doc.pdf')
    

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Upvotes: 1

Views: 371

Answers (1)

Raghav Sharma
Raghav Sharma

Reputation: 68

While there are many missing compoenents to your question, and few concepts being taken in an incorrect manner - allow me to guide you a bit.

  1. AWS Lambda: From the documentation

AWS Lambda is a compute service that lets you run code without provisioning or managing servers

Clearly, it is not a mechanism that could "generate" PDF. However, if you have a code that can "generate" PDF, you can use AWS Lambda service to execute it on the cloud.

  1. Your code: Looks like this bit you have got it covered.

  2. Corrupt PDF on download: Can you try renaming the file with a .pdf extension and then try opening it? If this works, you can add a Content-Type header while uploading the file to S3 to mark it as a PDF file. More information in this question

Upvotes: 1

Related Questions