Skip to content

Instantly share code, notes, and snippets.

@mahi424
Last active June 26, 2024 08:30
Show Gist options
  • Save mahi424/a24551fb4ab36ebd1ecfc439dc91100a to your computer and use it in GitHub Desktop.
Save mahi424/a24551fb4ab36ebd1ecfc439dc91100a to your computer and use it in GitHub Desktop.
html-to-pdf-via-lambda
import subprocess
import os
import boto3
import json
import base64
# https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-4/wkhtmltox-0.12.6-4.amazonlinux2_lambda.zip
# add this as layer
region_name = 'ap-south-1'
s3 = boto3.client('s3')
def getEventBody(event):
try:
body = json.loads(event['body'])
return body
except json.JSONDecodeError as e:
print('Bad Request: Invalid JSON', e)
raise e
def getKeyFromEvent(event, key='html'):
body = getEventBody(event)
try:
val = body.get(key)
return val
except Exception as e:
print(e)
raise e
def get_base64_of_pdf(pdf_file_path):
try:
with open(pdf_file_path, "rb") as pdf_file:
pdf_content = pdf_file.read()
base64_pdf = base64.b64encode(pdf_content).decode('utf-8')
return base64_pdf
print("PDF encoded in base64")
except Exception as e:
print(f"Failed to encode PDF in base64: {str(e)}")
return {
'statusCode': 500,
'body': 'Failed to encode PDF'
}
def uploadToS3(pdf_file_path, pdf_key, bucket_name):
try:
s3.upload_file(pdf_file_path, bucket_name, pdf_key, ExtraArgs={'ContentType': 'application/pdf'})
print(f"PDF uploaded to: s3://{bucket_name}/{pdf_key}")
# Constructing the S3 URL
s3_url = f"https://{bucket_name}.s3.{region_name}.amazonaws.com/{pdf_key}"
print(f"Object URL: {s3_url}")
return s3_url
except Exception as e:
print("Error uploading PDF to S3:", str(e))
raise e
def lambda_handler(event, context):
html_data = getKeyFromEvent(event, "html")
pdf_filename = getKeyFromEvent(event, "filename")
path = getKeyFromEvent(event, "path")
bucket = getKeyFromEvent(event, "bucket")
return_base_64 = False
try:
return_base_64 = getKeyFromEvent(event, "return_base_64") == True
except Exception as e:
return_base_64 = False
print(pdf_filename, path, bucket)
html_file_path = "/tmp/input.html"
pdf_file_path = "/tmp/output.pdf"
try:
with open(html_file_path, "w") as f:
f.write(html_data)
print('file written')
except Exception as e:
print(e)
raise e
# Convert HTML to PDF using wkhtmltopdf
print('writing pdf')
try:
process = subprocess.Popen(["wkhtmltopdf", html_file_path, pdf_file_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
if process.returncode != 0:
print(f"Failed to convert HTML to PDF. Stdout: {stdout.decode()}, Stderr: {stderr.decode()}")
else:
print('pdf written')
except Exception as e:
print(e)
print(f"Failed to convert HTML to PDF: {str(e)}")
# try:
# subprocess.run(["wkhtmltopdf", html_file_path, pdf_file_path], check=True)
# print('pdf written')
# except Exception as e:
# print(e)
# print(f"Failed to convert HTML to PDF: {str(e)}")
# path = aadhaar_xml
file_key = f'{path}/{pdf_filename}'
try:
s3_url = uploadToS3(pdf_file_path, file_key, bucket)
print('s3_url', s3_url)
except Exception as e:
print(f"Failed to upload PDF to S3: {str(e)}")
return {
'statusCode': 500,
'body': 'Failed to upload PDF to S3'
}
base_64 = ""
if return_base_64:
base_64 = get_base64_of_pdf(pdf_file_path)
# s3_url = '123456'
try:
os.remove(html_file_path)
os.remove(pdf_file_path)
except Exception as e:
print(e)
raise e
return {
'statusCode': 200,
'body': json.dumps({'s3_url': s3_url, 'base_64': base_64})
}
@mahi424
Copy link
Author

mahi424 commented Jun 26, 2024

curl --location 'https://<my-lambda-url>/default/html-to-pdf' \
--header 'Content-Type: application/json' \
--data '{
    "html": "<!DOCTYPE html><html><head><title>WOW</title></head><body>This is SAmple</body></html>",
    "filename": "adhd5.pdf",
    "return_base_64": true
}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment