无法在容器化AWS lambda中使用wkhtmltopdf

问题描述 投票:0回答:2

我必须用容器化 lambda 中的一些 html 页面制作 pdf 文件。为此,我尝试使用 pdfkit 和 wkhtmltopdf。我无法使用它并收到如图所示的错误-enter image description here

错误文本-

未找到 wkhtmltopdf 可执行文件:“./wkhtmltopdf”

如果此文件存在,请检查此进程是否可以读取它,或者您可以在方法调用中手动传递路径,请检查自述文件。否则请安装 wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

我的 lambda 代码:-

 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,
        )

我的docker文件-

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" ]

这是当我尝试通过运行 docker 容器来查找 wkhtmlpdf 时:- enter image description here

docker aws-lambda dockerfile wkhtmltopdf pdfkit
2个回答
0
投票

更新:问题已解决

这对我的案例有用。

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代码:

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,
        )

我提到的链接-https://micropyramid.com/blog/how-to-create-pdf-files-in-python-using-pdfkit/如何在基于Linux的(共享托管)上安装wkhtmltopdf网络服务器


0
投票

如果您想使用

public.ecr.aws/lambda/python:latest
,请使用dnf。

FROM public.ecr.aws/lambda/python:latest

RUN pip install pdfkit --target ${LAMBDA_TASK_ROOT}
RUN pip install boto3
RUN dnf install -y openssl xorg-x11-fonts-75dpi xorg-x11-fonts-Type1 libjpeg

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 function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.handler" ]
© www.soinside.com 2019 - 2024. All rights reserved.