title: Is it possible to use wkhtmltopdf on AWS lambda
body: <p>There are some Node Js github repos for this. I am a newbie to Node, therefore I used python to try this out.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I am getting permission denied error on lambda. Below is my code as well as error with the code.</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>{
        "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></pre>
  
  <p><strong>Code</strong></p>
  
  <pre><code>import boto3
  import os,subprocess
  from boto3.s3.transfer import S3Transfer
  from 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 for "+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 for 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')
  </code></pre>
  
  <p><strong>I have tried</strong> to keep the wkhtmltopdf file in the zip package and then upload it but that returns the same error.</p>
  
  <p>Is it possible with Aws lambda? I have already tried this out with EC2 instance and it works fine.</p>