5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails】【Docker】【Alpine】wicked_pdfを使用してpdfを出力する

Posted at

issue

macOSで動作させていたRailsアプリのPDF出力機能がDocker環境で動作しなくなった。

Error: PDF could not be generated!
スクリーンショット 2020-07-17 16.03.58.png

使用したgem

gem 'wicket_pdf'
gem 'wkhtmltopdf_binary'

solutions

Dockerfileに下記を追記

RUN apk add --no-cache \
    wkhtmltopdf \ 
    ttf-ubuntu-font-family
/usr/bin/wkhtmltopdf --version
-> wkhtmltopdf 0.12.5

wicket_pdf.rbからalpinelinux上のwkhtmltopdfを見るようにパスを指定する

wicket_pdf.rb
require "wicked_pdf"

WickedPdf.config = {
  exe_path: '/usr/bin/wkhtmltopdf'
}

日本語が化けた場合

AlpineLinuxにfont-ipa(日本語)をインストールする

terminal
  $ cd \
    && wget https://ipafont.ipa.go.jp/IPAfont/ipag00303.zip \
    && mkdir .fonts \
    && unzip ipag00303.zip -d .fonts -j \
    && fc-cache -fv ~/.fonts

本番環境ではポチポチインストールできないのでDockerfileに追記

Dockerfile
RUN cd \
    && wget https://ipafont.ipa.go.jp/IPAfont/ipag00303.zip \
    && mkdir .fonts \
    && unzip ipag00303.zip -d .fonts -j \
    && fc-cache -fv ~/.fonts

wgetコマンド
fc-cacheコマンド
unzipコマンド

appendix

pdf.html.erb
# 最低限なのでcss等適応したい場合は適宜変更する
<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
      <%= yield %>
  </body>
</html>
controlloer
respond_to do |format|
      format.html
      format.pdf do
        render pdf: "article_#{params[:id]}",
               layout: 'pdf.html',
               templete: 'articles/show',
               encoding: 'UTF-8',
               page_size: 'A4',
               margin: {
                 top: 10,
                 bottom: 10,
                 left: 20,
                 right: 20
               }
      end
end

reference

gem wicked_pdf
Does it work with Alpine?
Issue with wkhtmltopdf that generates PDF files for this plugin on default Sylius Docker
wicked_pdf Error: PDF could not be generated
RailsアプリでのPDFファイル生成
IPAフォントのダウンロード

5
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?