概要
RailsでPDFアップロードさせて表紙(1ページ目)のみPNGに変換してサムネイルとして表示させたい。
そのためにCentOSにghostscript+ImageMagickをインストールしないといけないが、yumでインストールするghostscript+ImageMagickでは変換がうまくいかない。(Table1)
そのためにソースから再インストールする必要がある。
Table1
$ convert sample-slide.pdf[0] sample-slide.png
**** Warning: File has an invalid xref entry: 70. Rebuilding xref table.
Error: /limitcheck in --run--
Operand stack:
--dict:6/15(L)-- C1 1 --dict:5/5(L)-- --dict:5/5(L)-- HWTTNI+HiraKakuPro-W6 --dict:11/12(ro)(G)-- --nostringval-- CIDFontObject --dict:7/7(L)-- 47182 --dict:7/7(L)-- --dict:5/13(L)-- --nostringval-- --nostringval-- 508369 373655
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1862 1 3 %oparray_pop 1861 1 3 %oparray_pop 1845 1 3 %oparray_pop --nostringval-- --nostringval-- 2 1 1 --nostringval-- %for_pos_int_continue --nostringval-- --nostringval-- false 1 %stopped_push --nostringval-- --nostringval-- --nostringval-- %array_continue --nostringval-- false 1 %stopped_push --nostringval-- %loop_continue --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- %array_continue --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- %loop_continue --nostringval-- --nostringval-- --nostringval-- %array_continue --nostringval-- --nostringval-- --nostringval--
Dictionary stack:
--dict:1157/1684(ro)(G)-- --dict:1/20(G)-- --dict:75/200(L)-- --dict:75/200(L)-- --dict:106/127(ro)(G)-- --dict:286/300(ro)(G)-- --dict:24/25(L)-- --dict:4/6(L)-- --dict:21/40(L)-- --dict:3/5(L)-- --dict:6/15(L)-- --dict:54/106(ro)(G)-- --dict:21/30(L)--
Current allocation mode is local
GPL Ghostscript 8.70: Unrecoverable error, exit code 1
convert: Postscript delegate failed `sample-slide.pdf': No such file or directory @ pdf.c/ReadPDFImage/611.
convert: missing an image filename `sample-slide.png' @ convert.c/ConvertImageCommand/2800.
ghostscript+ImageMagickのインストール
sudo su -
yum remove ImageMagick* ghostscript*
cd /usr/local/src
wget http://downloads.ghostscript.com/public/ghostscript-9.07.tar.gz
tar zxvf ghostscript-9.07.tar.gz
./configure
make
make install
wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar zxvf ImageMagick.tar.gz
./configure
make
make install
vim /etc/profile.d/rmagick.sh
export LD_LIBRARY_PATH=/usr/local/lib/
最後の/etc/profile.d/rmagick.sh
に記入する行は、gemでRmagickをインストールするのに必要。
ImageMagickの確認
サーバ上にサンプルのPDFを設置して、以下のコマンドを実行して変換がうまくいくか確認する。
$ convert sample.pdf[0] sample.png
Rmagickのインストール
Gemfile
gem 'rmagick', :require => 'RMagick'
$ bundle install
これで、Rmagickのインストールができる。
さきほど、/etc/profile.d/rmagick.sh
を作成していない場合は、このインストールでエラーが出てしまう。(Table2)
LoadError: libMagickCore-6.Q16.so.2: cannot open shared object file: No such file or directory - /var/www/sample/shared/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/rmagick-2.13.3/RMagick2.so
/var/www/sample/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/var/www/sample/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
/var/www/sample/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
/var/www/sample/shared/bundle/ruby/2.1.0/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
/var/www/sample/shared/bundle/ruby/2.1.0/gems/rmagick-2.13.3/lib/RMagick.rb:11:in `<top (required)>'
/var/www/sample/releases/20140803054150/config/application.rb:7:in `<top (required)>'
/var/www/sample/releases/20140803054150/Rakefile:4:in `require'
/var/www/sample/releases/20140803054150/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
RMagickでPDFの表紙のみpng画像に変換する
Rails内から以下のrubyを実行する。
file = params[:file]
pdf = Magick::ImageList.new(file.path + '[0]') #=> 1ページ目のみ抽出
cover_tmp = Rails.root.join('public', file.original_filename)
pdf[0].write(cover_tmp)
Magick::ImageList.new
実行時に、Magick::ImageList.new(file.path + '[0]')
とページを指定したほうが、Magick::ImageList.new(file.path)
と全てのページを指定するよりも変換が高速になる。
まとめ
ImageMagickインストール時には、対応バージョンやgemインストール時のPATH設定など、注意点がいくつかある。