python - Is it possible to use wkhtmltopdf on AWS lambda -
there node js github repos this. newbie node, therefore used python try out.
problem
i getting permission denied error on lambda. below code error code.
error
{ "errormessage": "[errno 13] permission denied", "errortype": "permissionerror", "stacktrace": [ [ "/var/task/htmltopdf.py", 50, "handler", "c=pdfkit.from_string(document,false,configuration=config )" ], [ "/var/task/pdfkit/api.py", 72, "from_string", "return r.to_pdf(output_path)" ], [ "/var/task/pdfkit/pdfkit.py", 129, "to_pdf", "stderr=subprocess.pipe)" ], [ "/var/lang/lib/python3.6/subprocess.py", 707, "__init__", "restore_signals, start_new_session)" ], [ "/var/lang/lib/python3.6/subprocess.py", 1326, "_execute_child", "raise child_exception_type(errno_num, err_msg)" ] ] }
code
import boto3 import os,subprocess boto3.s3.transfer import s3transfer bs4 import beautifulsoup import time import pdfkit import codecs lambda_task_root = os.environ.get('lambda_task_root', os.path.dirname(os.path.abspath(__file__))) curr_bin_dir = os.path.join(lambda_task_root, 'bin') lib_dir = os.path.join(lambda_task_root, 'lib') bin_dir = '/tmp/bin' def _init_bin(executable_name,bucket_name,region): start = time.clock() if not os.path.exists(bin_dir): print("creating bin folder") os.makedirs(bin_dir) print("copying binaries "+executable_name+" in /tmp/bin") client = boto3.client('s3', aws_access_key_id='xxxxxx', aws_secret_access_key='xxxxxx', region_name=region ) client.download_file(bucket_name, 'wkhtmltopdf', '/tmp/bin/wkhtmltopdf') newfile = os.path.join(bin_dir, executable_name) print("giving new binaries permissions lambda") os.chmod(newfile, 777) elapsed = (time.clock() - start) print(executable_name+" ready in "+str(elapsed)+'s.') def handler(event, context): bucket_name = event['bucketname'] region = event['regionname'] htmlfilename = event['htmlfilename'] pdfname = htmlfilename.split(".") client = boto3.client('s3', aws_access_key_id='xxxxxxxxxxxxxxxxx', aws_secret_access_key='xxxxxxxx', region_name=region ) client.download_file(bucket_name, htmlfilename, '/tmp/' + htmlfilename) _init_bin('wkhtmltopdf',event['bucketname'],event['regionname']) f=codecs.open('/tmp/'+htmlfilename,'r','utf-8') document=beautifulsoup(f.read()).get_text() path_wkthmltopdf ='/tmp/bin/wkhtmltopdf' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) c=pdfkit.from_string(document,false,configuration=config ) #transfer = s3transfer(client) #transfer.upload_file('/tmp/' + pdfname[0] + '.pdf', bucket_name, pdfname[0] + '.pdf')
i have tried keep wkhtmltopdf file in zip package , upload returns same error.
is possible aws lambda? have tried out ec2 instance , works fine.
Comments
Post a Comment