如何使用Ruby的Dockerized Rails应用程序获得WKHTMLTOPDF:3.0.4-Alpine供邪恶的PDF一起使用

发布于 2025-02-12 03:28:57 字数 2753 浏览 1 评论 0原文

我正在尝试将Dockerized Rails应用程序升级到Ruby 3+和Rails 7。由于其他项目依赖性,我已经登陆了此版本的Ruby Docker Image Ruby:3.0.4-Alpine。这是我的Dockerfile:

FROM ruby:3.0.4-alpine

RUN apk --update add --no-cache build-base bash git vim curl postgresql-dev openssl-dev nodejs npm yarn tzdata less \
imagemagick postgresql-client gcompat

RUN mkdir /app
WORKDIR /app

COPY Gemfile Gemfile.lock package.json yarn.lock ./

RUN yarn set version berry

RUN bundle update --bundler
RUN bundle install --jobs 5

ADD . /app

RUN yarn install

RUN yarn install --frozen-lockfile \
  && RAILS_SECRET_KEY_BASE=secret_key_base RAILS_ENV=production bundle exec rails assets:precompile apipie:cache \
  && rm -rf node_modules

EXPOSE 5000

VOLUME ["/app/public", "/usr/local/bundle"]

CMD bash -c "bundle exec puma -C config/puma.rb"

此时,我的Rails应用程序中无法使用此Dockerfile的唯一依赖性是wkhtmltopdf

我希望将wkhtmltopdf与Alpine软件包Keeper(APK)一起安装,作为Run APK-UPK-UPDATE add -no-Cache的一部分。

但是,看来具有wkhtmltopdf的最新版本的Alpine可用的软件包是高山3.14 。 Alpine 3.15 State的发行说明“ QT5-QTWEBKIT,KDEWEBKIT,WKHTMLTOPDF和PY3-PDFKIT由于已知的漏洞和缺乏上游支持QTWebKit而被删除。”

因此,除了关闭Alpine Linux之外,还会呈现其他依赖性问题。我希望不这样做,因为这似乎是大多数Dockerized Rails应用程序在可预见的将来使用的版本,我的选择是什么?

我还尝试使用 wkhtmltopdf_binary gem当我尝试使用 wicked_pdf 使用wkhtmltopdf安装该gem

/usr/local/bundle/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf:61:in `<top (required)>': Invalid platform, must be running on Ubuntu
16.04/18.04/20.04 CentOS 6/7/8, Debian 9/10, archlinux amd64, or intel-based Cocoa macOS (missing binary: /usr/local/bundle/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf_alpine_3.16.0_i386). (RuntimeError) from /usr/local/bundle/bin/wkhtmltopdf:25:in `load' from /usr/local/bundle/bin/wkhtmltopdf:25:in `<main>'

时相信我会遇到此错误,因为Docker Image Ruby:3.0.4-Alpine正在运行Alpine 3.16,Alpine在版本3.14上删除了对WKHTMLTOPDF的支持。自2016年以来,wkhtmltopdf_binary GEM尚未更新,因此它不会针对高山的最新版本。

那么,我有什么选择呢?有没有办法可以在我的dockerfile中撰写wkhtmltopdf的兼容安装?如果是这样,我该怎么办?

我找不到另一个针对安装WKHTMLTOPDF的高山版本的GEM。有一个我缺少的吗?

这必须是一个常见的问题(Ruby 3+在Docker Alpine图像上运行的Rails应用需要使用WKHTMLTOPDF导出PDF)。

或者,是否有另一种方法可以在Ruby中导出PDF,而不取决于我找不到的WKHTMLTOPDF? (我发现了其他Nodejs选项,但是这引入了其他一组依赖项,因此最好是Docker/Ruby解决方案。)

I'm trying to upgrade a Dockerized Rails app to Ruby 3+ and Rails 7. Due to other project dependencies, I've landed on this version of Ruby Docker image ruby:3.0.4-alpine. Here's my Dockerfile:

FROM ruby:3.0.4-alpine

RUN apk --update add --no-cache build-base bash git vim curl postgresql-dev openssl-dev nodejs npm yarn tzdata less \
imagemagick postgresql-client gcompat

RUN mkdir /app
WORKDIR /app

COPY Gemfile Gemfile.lock package.json yarn.lock ./

RUN yarn set version berry

RUN bundle update --bundler
RUN bundle install --jobs 5

ADD . /app

RUN yarn install

RUN yarn install --frozen-lockfile \
  && RAILS_SECRET_KEY_BASE=secret_key_base RAILS_ENV=production bundle exec rails assets:precompile apipie:cache \
  && rm -rf node_modules

EXPOSE 5000

VOLUME ["/app/public", "/usr/local/bundle"]

CMD bash -c "bundle exec puma -C config/puma.rb"

At this point the only dependency in my Rails app that doesn't work with this Dockerfile is wkhtmltopdf.

I would prefer to install wkhtmltopdf with the Alpine Package Keeper (apk) as part of RUN apk --update add --no-cache.

But, it appears that the latest version of Alpine that has the wkhtmltopdf package available is Alpine 3.14. The release notes for Alpine 3.15 state "qt5-qtwebkit, kdewebkit, wkhtmltopdf, and py3-pdfkit have been removed due to known vulnerabilities and lack of upstream support for qtwebkit."

So other than switching off of Alpine Linux, which would present other dependency issues. I would prefer not to do as that seems to be the version used by most Dockerized Rails apps for the foreseeable future, what are my options?

I also tried using the wkhtmltopdf_binary gem, but that was last updated in 2016. When I try to export a PDF using wicked_pdf with wkhtmltopdf installed by that gem, I get this error:

/usr/local/bundle/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf:61:in `<top (required)>': Invalid platform, must be running on Ubuntu
16.04/18.04/20.04 CentOS 6/7/8, Debian 9/10, archlinux amd64, or intel-based Cocoa macOS (missing binary: /usr/local/bundle/gems/wkhtmltopdf-binary-0.12.6.5/bin/wkhtmltopdf_alpine_3.16.0_i386). (RuntimeError) from /usr/local/bundle/bin/wkhtmltopdf:25:in `load' from /usr/local/bundle/bin/wkhtmltopdf:25:in `<main>'

I believe I'm getting this error because the docker image ruby:3.0.4-alpine is running Alpine 3.16 and Alpine dropped support for wkhtmltopdf on version 3.14. The wkhtmltopdf_binary gem hasn't been updated since 2016, so it wouldn't target more recent versions of Alpine.

So, what are my options to work around this? Is there a way to script a compatible installation of the wkhtmltopdf in my Dockerfile? If so, how would I do that?

I haven't been able to find another gem that targets more recent versions of Alpine that install wkhtmltopdf. Is there one out there that I'm missing?

This has to be a common problem (Rails app running on Docker Alpine image with Ruby 3+ that needs to export PDFs using wkhtmltopdf).

Or, is there another way to export PDFs in ruby that doesn't depend on wkhtmltopdf that I haven't found? (I've found other NodeJS options, but that introduces a whole other set of dependencies, so a Docker/Ruby solution is preferable.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎歌 2025-02-19 03:28:57

我也有一个类似的问题,并通过添加:

FROM surnet/alpine-wkhtmltopdf:3.16.2-0.12.6-full as wkhtmltopdf

RUN apk add --no-cache \
        libstdc++ \
        libx11 \
        libxrender \
        libxext \
        libssl1.1 \
        ca-certificates \
        fontconfig \
        freetype \
        ttf-droid \
        ttf-freefont \
        ttf-liberation
COPY --from=wkhtmltopdf /bin/wkhtmltopdf /bin/libwkhtmltox.so /bin/

有关更多详细信息,请参见 https://github.com/surnet/docker-wkhtmltopdf/pkgs/container/alpine-whtmltpdf#other-images

docker容器将在路径上具有wkhtmltopdf。我们在铁路应用中使用WickedPDF GEM,而不是直接使用WKHTMLTOPDF。

I had a similar issue and fixed it by adding:

FROM surnet/alpine-wkhtmltopdf:3.16.2-0.12.6-full as wkhtmltopdf

RUN apk add --no-cache \
        libstdc++ \
        libx11 \
        libxrender \
        libxext \
        libssl1.1 \
        ca-certificates \
        fontconfig \
        freetype \
        ttf-droid \
        ttf-freefont \
        ttf-liberation
COPY --from=wkhtmltopdf /bin/wkhtmltopdf /bin/libwkhtmltox.so /bin/

For more details see https://github.com/Surnet/docker-wkhtmltopdf/pkgs/container/alpine-wkhtmltopdf#other-images

The docker container will then have wkhtmltopdf on the path. We are using the WickedPdf gem in our rails app instead of wkhtmltopdf directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文