pla*_*smy 3 pdfkit wkhtmltopdf flask docker
我在 Python Flask 中有一段代码,可以使用 HTML 模板生成 pdf 文件。当我单独运行它时,代码运行得很好,但是当我尝试在 Docker 容器内运行它时,一旦我调用生成报告的端点,docker 就会崩溃并重置。它只是保持加载状态,然后返回错误(在我用来测试的 Postman 中)。
PDF的代码如下:
def create_report(download_uuid):
transactions = get_transaction_for_report(download_uuid)
config = pdfkit.configuration(wkhtmltopdf=environ.get('WKHTMLTOPDF'))
file_obj = io.BytesIO()
with zipfile.ZipFile(file_obj, 'w') as zip_file:
for transaction in transactions:
html = render_template("report.html", transaction=transaction)
pdf = pdfkit.from_string(html, False, configuration=config)
data = zipfile.ZipInfo('{}.pdf'.format(transaction['control_number']))
data.compress_type = zipfile.ZIP_DEFLATED
zip_file.writestr(data, pdf)
file_obj.seek(0)
return send_file(file_obj, attachment_filename="forms.zip", as_attachment=True)
Run Code Online (Sandbox Code Playgroud)
它返回一个 zip 文件,但 zip 文件内是 pdf 文件。此外,如果我删除 pdf 生成部分,zip 文件会正常返回。这是我的 Dockerfile:
FROM madnight/docker-alpine-wkhtmltopdf as wkhtmltopdf_image
FROM python:3.9-alpine
RUN adduser -D custom
WORKDIR /home/Project
COPY requirements.txt requirements.txt
RUN python -m venv venv
RUN venv/bin/pip install --upgrade pip
RUN apk add make automake gcc g++ subversion python3-dev jpeg-dev zlib-dev libffi-dev musl-dev openssl-dev freetype freetype-dev ttf-freefont libxrender qt5-qtbase-dev
RUN venv/bin/pip install -r requirements.txt
RUN venv/bin/pip install gunicorn
COPY Project Project
COPY boot.sh app.py .env run.py create_database.py config.py ./
COPY templates templates
COPY static static
COPY --from=wkhtmltopdf_image /bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
RUN chmod +x boot.sh
ENV FLASK_APP app.py
USER root
RUN chown -R custom ./
USER custom
EXPOSE 9001
ENTRYPOINT ["./boot.sh"]
Run Code Online (Sandbox Code Playgroud)
我应该说,这是许多很多尝试使其发挥作用的最后一次迭代。本质上,我尝试通过curl获取wkhtmltox,我尝试将wkhtmltopdf放在不同的目录中。到目前为止还没有任何效果。我不知道我错过了什么。这基本上是我需要解决的问题才能完成这个项目,因此任何帮助都将非常感激。
编辑:docker-compose.yml
version: '2'
services:
app:
build: .
networks:
- custom
ports:
- "9001:9001"
volumes:
- "./static:/home/EventismedEquipmentAPI/static"
external_links:
- eventismed-equipment:db
networks:
custom:
external: true
Run Code Online (Sandbox Code Playgroud)
让我们解决这个问题。
wkhtmltopdf我已经成功地在 Docker 容器上独立运行。
Dockerfile:
# /sf/answers/4391600951/
# Create image based on the official openjdk 8-jre-alpine image from the dockerhub
FROM openjdk:8-jre-alpine
# Install wkhtmltopdf
# /sf/answers/3984775301/
RUN apk add --no-cache wkhtmltopdf ttf-dejavu
ENTRYPOINT ["sh"]
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml:
version: '3.8'
services:
wkhtmltopdf:
image: wkhtmltopdf
container_name: wkhtmltopdf
build:
dockerfile: Dockerfile
context: .
Run Code Online (Sandbox Code Playgroud)
然后:
docker-compose build
docker run -ti --rm -v /tmp:/tmp wkhtmltopdf
Run Code Online (Sandbox Code Playgroud)
容器内部:
$ cd /tmp
$ wkhtmltopdf https://www.google.com test.pdf
Run Code Online (Sandbox Code Playgroud)
然后您将在 Mac 上看到 pdf 文件:/tmp/test.pdf
首先让我知道这是否有效。
| 归档时间: |
|
| 查看次数: |
5932 次 |
| 最近记录: |