我在下面提到了许多有用的链接:Not able to use wkhtmltopdf in containerized AWS lambdaHow I can use python pdfkit and wkhtmltopdf in aws lambda?
但我在成功运行docker后不断收到操作系统错误。
下面是lambda代码:
import json
import os
import sys
import subprocess
from jinja2 import Environment, FileSystemLoader
# from flask import Flask
import pdfkit
def lambda_handler(event, context):
#print("==============>>",os.getcwd(), os.path.exists('wkhtml_file/wkhtmltopdf'))
env = Environment(loader=FileSystemLoader("templates/"))
content = env.get_template("esign_pdf_old.html")
# # data_main = json.dumps(json_file)
content_main = content.render(pdf_data=event)
# # # Set up the options for PDFKit
path_to_wkhtmltopdf = "/usr/local/bin/wkhtmltopdf"
config = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf)
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'
}
# Convert HTML file to PDF
#output_path = f"/pdf_files/{event.get("message").get('user_details').get('customer_id')}"
os.makedirs("output",exist_ok=True)
pdfkit.from_string(content_main, output_path="output",configuration=config,
options=options)
return {
'statusCode': 200,
'body': ['Coming from lambda main 2',"wkhtmltopdf is installed"]
}
下面是Docker文件
FROM umihico/aws-lambda-selenium-python:latest
RUN pip install pdfkit==1.0.0 --target ${LAMBDA_TASK_ROOT}
RUN pip install jinja2==3.1.2
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
# ADD wkhtml_file wkhtml_file
ADD templates templates
COPY lambda_function.py ./
CMD [ "lambda_function.lambda_handler" ]
下面是我在运行API时得到的错误:
http://localhost:9000/2015 - 03 - 31/functions/function/invocations
{"errorMessage": "wkhtmltopdf exited with non-zero code 1. error:\nQPainter::begin(): Returned
false\nExit with code 1, due to unknown error.\n", "errorType": "OSError", "requestId":
"94020d9f-22ae-4565-afd0-b97fec9d90be", "stackTrace": [" File
\"/var/task/lambda_function.py\", line 39, in lambda_handler\n
pdfkit.from_string(content_main, output_path=\"output\",configuration=config,
options=options)\n", " File \"/var/task/pdfkit/api.py\", line 75, in from_string\n return
r.to_pdf(output_path)\n", " File \"/var/task/pdfkit/pdfkit.py\", line 201, in to_pdf\n
self.handle_error(exit_code, stderr)\n", " File \"/var/task/pdfkit/pdfkit.py\", line 158, in
handle_error\n raise IOError(\"wkhtmltopdf exited with non-zero code {0}. error:\\n{1}\".format(exit_code, error_msg))\n"]}
注意:我使用Windows来构建Docker。我应该尝试Linux解决这个问题吗?
1条答案
按热度按时间58wvjzkj1#
下面的代码工作正常,唯一的问题是我删除了选项。