dockeraws-lambdadockerfilewkhtmltopdfpdfkit

Not able to use wkhtmltopdf in containerized AWS lambda


I have to make a pdf out of some html pages in a containerized lambda. For this purpose I am trying to use pdfkit and wkhtmltopdf. I am not able to use it an d receiving the error as shown in picture-enter image description here

Error text-

No wkhtmltopdf executable found: "./wkhtmltopdf"

If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

My lambda code:-

 import pdfkit as pdf
 def lambda_function:
    config = pdf.configuration(wkhtmltopdf='./wkhtmltopdf')
    pdf.from_file(
        filelist_new,
        output_filename,
        options={
            'margin-top': '0.2in',
            'margin-right': '0.2in',
            'margin-bottom': '0.4in',
            'margin-left': '0.2in',
            'orientation': 'Landscape',
            'page-size': 'A4',
            'encoding': 'UTF-8',
            'footer-line': '',
            'footer-spacing': 1,
            'footer-font-name': 'Times,serif',
            'footer-font-size': '10'
            },
        configuration=config,
        )

My docker file-

FROM umihico/aws-lambda-selenium-python:latest
RUN pip install pdfkit
RUN pip install boto3
RUN pip install wkhtmltopdf --target "./"
COPY lambda_function.py ./
CMD [ "lambda_function.lambda_handler" ]

and this is when I tried to find wkhtmlpdf by running the docker container:- enter image description here


Solution

  • Update: Issue got solved

    This worked for my case.

    DockerFile:

    FROM umihico/aws-lambda-selenium-python:latest
    RUN pip install pdfkit --target ${LAMBDA_TASK_ROOT}
    RUN pip install boto3
    RUN yum install -y openssl xorg-x11-fonts-75dpi xorg-x11-fonts-Type1
    RUN curl "https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.amazonlinux2.x86_64.rpm" -L -o wkhtmltox-0.12.6-1.amazonlinux2.x86_64.rpm
    RUN rpm -i wkhtmltox-0.12.6-1.amazonlinux2.x86_64.rpm
    COPY lambda_function.py ./
    CMD [ "lambda_function.lambda_handler" ]
    

    Lambda code:

    import pdfkit as pdf
    
    
    def lambda_function:
        config = pdf.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
        pdf.from_file(
            filelist_new,
            output_filename,
            options={
                'enable-local-file-access': '',
                'margin-top': '0.2in',
                'margin-right': '0.2in',
                'margin-bottom': '0.4in',
                'margin-left': '0.2in',
                'orientation': 'Landscape',
                'page-size': 'A4',
                'encoding': 'UTF-8',
                'footer-line': '',
                'footer-spacing': 1,
                'footer-font-name': 'Times,serif',
                'footer-font-size': '10'
                },
            configuration=config,
            )
    

    Links which I referred- https://micropyramid.com/blog/how-to-create-pdf-files-in-python-using-pdfkit/ , How to install wkhtmltopdf on a linux based (shared hosting) web server