Rails 中的 wicked_pdf 在从 docker 中的字符串渲染 pdf 时无法读取本地临时文件

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

我正在使用

wicked_pdf
gem 从 Rails 中的 html 模板渲染 pdf。在本地主机上,它在生产环境(通过 Fargate 在 AWS ECS 中运行的 docker 映像)上运行时,导出端点会返回错误。该错误是由 wkhtmltopdf 二进制文件引起的:

["/usr/local/bundle/bin/wkhtmltopdf", "--enable-local-file-access", "--dpi", "80", "--margin-bottom", "1", "--header-html", "file:////tmp/wicked_header_pdf20230901-1-log5m3.html", "file:////tmp/wicked_pdf20230901-1-pxi12g.html", "/tmp/wicked_pdf_generated_file20230901-1-wa7ysm.pdf"]
Error: PDF could not be generated!
 Command Error: Loading pages (1/6)
[>                                                           ] 0%
[======>                                                     ] 10%
Error: Failed to load file:////tmp/wicked_header_pdf20230901-1-log5m3.html, with network status code 203 and http status code 0 - Error opening //tmp/wicked_header_pdf20230901-1-log5m3.html: No such file or directory
Error: Failed loading page file:////tmp/wicked_header_pdf20230901-1-log5m3.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: ContentNotFoundError

我该如何解决这个问题?唯一的区别是它在本地本地运行,在 docker 中使用

wkhtmltopdf-binary
gem。

ruby-on-rails docker wkhtmltopdf wicked-pdf
1个回答
0
投票

我通过在 Dockerfile 中设置

ENV TMPDIR=/tmp
解决了这个问题。由于某种原因,它没有在
ruby:x.x.x-slim
Docker 镜像中设置,我相信如果没有它,Rails 就无法正确解析临时路径。

设置后一切正常。

© www.soinside.com 2019 - 2024. All rights reserved.