如何获得wkhtmltopdf与Dockerized Rails应用程序运行ruby:3.0.4-alpine与Wick



我正在尝试将Dockerized Rails应用程序升级到Ruby 3+和Rails 7。由于其他项目的依赖关系,我已经降落在这个版本的Ruby Docker镜像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 Package Keeper (apk)一起安装,作为RUN apk --update add --no-cache的一部分。

但是,有wkhtmltopdf包可用的Alpine的最新版本似乎是Alpine 3.14。由于已知漏洞和缺乏上游对qtwebkit的支持,Alpine 3.15的发布说明"qt5-qtwebkit、kdewebkit、wkhtmltopdf和py3-pdfkit"已被删除。

因此,除了关闭阿尔卑斯Linux之外,这将带来其他依赖问题。我宁愿不这样做,因为这似乎是大多数Dockerized Rails应用程序在可预见的未来使用的版本,我的选择是什么?

我还尝试使用wkhtmltopdf_binary gem,但上次更新是在2016年。当我尝试使用该gem安装的wkhtmltopdf和wicked_pdf导出PDF时,我得到这个错误:

/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镜像ruby:3.0.4-alpine正在运行Alpine 3.16, Alpine在3.14版本上放弃了对wkhtmltopdf的支持。wkhtmltopdf_binarygem自2016年以来就没有更新过,所以它不会针对最新版本的Alpine。

那么,我有什么选择来解决这个问题呢?是否有办法在我的Dockerfile脚本wkhtmltopdf的兼容安装?如果有,我该怎么做?

我还没有找到另一个gem,目标是安装wkhtmltopdf的最新版本的Alpine。我还缺一个吗?

这必须是一个常见的问题(Rails应用程序运行在Docker Alpine image与Ruby 3+,需要使用wkhtmltopdf导出pdf)。

或者,是否有另一种方法来导出pdf在ruby,不依赖于wkhtmltopdf,我还没有发现?(我已经找到了其他NodeJS选项,但这引入了一套完全不同的依赖关系,所以Docker/Ruby解决方案更可取。)

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

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-wkhtmltopdf#other-images

docker容器在路径上会有wkhtmltopdf。我们在rails应用中使用了WickedPdf gem,而不是直接使用wkhtmltopdf。