Skip to content

Instantly share code, notes, and snippets.

@mitzi2funk
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitzi2funk/91bb87d3861558e4f0d4 to your computer and use it in GitHub Desktop.
Save mitzi2funk/91bb87d3861558e4f0d4 to your computer and use it in GitHub Desktop.
ImageMagickとPHPを用いてPDFを画像変換する ref: http://qiita.com/mitzi2funk/items/fc81f77270ad997a80d3
# rpm -qa | grep "make\|gcc"
libgcc-4.4.7-4.el6.x86_64
gcc-c++-4.4.7-4.el6.x86_64
make-3.81-20.el6.x86_64
gcc-4.4.7-4.el6.x86_64
# yum install make gcc
# cd /usr/local/share/ghostscript/9.15/Resource/Init
# cp -p cidfmap cidfmap.org
# vi cidfmap
----------
/Ryumin-Light /IPAEXMincho ;
/GothicBBB-Medium /IPAGothic ;
/HeiseiMin-W3 /Ryumin-Light ;
/HeiseiKakuGo-W5 /GothicBBB-Medium ;
%%% Alias for dvips
/ipamincho /IPAMincho ;
/ipapmincho /IPAPMincho ;
/ipagothic /IPAGothic ;
/ipapgothic /IPAPGothic ;
/ipam /IPAMincho ;
/ipamp /IPAPMincho ;
/ipag /IPAGothic ;
/ipagp /IPAPGothic ;
/ipaexm /IPAEXMincho ;
/ipaexg /IPAEXGothic ;
/IPAMincho
<<
/FileType /TrueType
/CSI [(Japan1) 6]
/Path (/usr/share/fonts/ipa/TrueType/ipam.ttf)
/SubfontID 0
>> ;
/IPAPMincho
<<
/FileType /TrueType
/CSI [(Japan1) 6]
/Path (/usr/share/fonts/ipa/TrueType/ipamp.ttf)
/SubfontID 0
>> ;
/IPAGothic
<<
/FileType /TrueType
/CSI [(Japan1) 6]
/Path (/usr/share/fonts/ipa/TrueType/ipag.ttf)
/SubfontID 0
>> ;
/IPAPGothic
<<
/FileType /TrueType
/CSI [(Japan1) 6]
/Path (/usr/share/fonts/ipa/TrueType/ipagp.ttf)
/SubfontID 0
>> ;
/IPAEXMincho
<<
/FileType /TrueType
/CSI [(Japan1) 6]
/Path (/usr/share/fonts/ipa/TrueType/ipaexm.ttf)
/SubfontID 0
>> ;
/IPAEXGothic
<<
/FileType /TrueType
/CSI [(Japan1) 6]
/Path (/usr/share/fonts/ipa/TrueType/ipaexg.ttf)
/SubfontID 0
>> ;
/Arial
<<
/FileType /TrueType
/CSI [(Artifex) (Unicode) 0]
/Path (/usr/share/fonts/msttcorefonts/arial.ttf)
/SubfontID 0
>> ;
# cd /usr/local/share/ghostscript/9.15/Resource/Init
# cp -p gs_res.ps gs_res.ps.org
# vi gs_res.ps
------
% /FontResourceDir (Font) .resource_dir_name
/FontResourceDir (/usr/local/share/ghostscript/9.15/Resource/Font/) .resource_dir_name
% /GenericResourceDir () .resource_dir_name
/GenericResourceDir (/usr/local/share/ghostscript/9.15/Resource) .resource_dir_name
# which gs
/usr/local/bin/gs
# gs [オプション] [入力ファイル]
# cd /usr/local/share/ghostscript/9.15/examples
# gs -sDEVICE=pdfwrite -sOutputFile=tiger.pdf tiger.eps
>>showpage, press <return> to continue<< ←エンター押下
GS>
GS>quit
# ls -l tiger.pdf
tiger.pdf
ヘルプの内容を記載しただけです
-sDEVICE=[出力デバイス] ※DEVICEとなっているが、今回はPNGやJPEGなどの画像形式を指定
-sOutputFile=[出力ファイル名]
-dBATCH ※プロンプトを自動的に抜け処理を実行してくれる
-r[解像度] ※デフォルトは72dpi
-g<width>x<height> ※page size in pixels
# yum install ImageMagick ImageMagick-devel
# yum install php-pear php-devel httpd-devel
# yum groupinstall 'Development tools'
# pecl install imagick
Please provide the prefix of Imagemagick installation [autodetect] : そのままエンターで自動的にImageMagickを探してもらう
# cp -p /etc/php.ini /etc/php.ini.org
# vi /etc/php.ini
------
extension=imagick.so こちらを追記
# php -m | grep imagick
imagick
# cd /usr/local/share/ghostscript/9.15/examples
# convert tiger.pdf tiger.png
# ls tiger.png
tiger.png
# convert annots.pdf annots.png
# ls annots*.png
annots-0.png annots-1.png annots-2.png annots-3.png annots-4.png annots-5.png
※ページ毎に画像ファイルへ変換される
# cd /var/www/html
# mkdir /var/www/html/pdf
# chown root.apache /var/www/html/pdf
# chmod 775 /var/www/html/pdf
# vi /var/www/html/pdf/Imagick-Ghostscript.php
<?php
//ImageMagic定義
$im = new imagick();
//$im->setResolution(144,144);
//対象のディレクトリを指定(カレントディレクトリ);
$serch_dir = dirname(__FILE__);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($serch_dir,
FilesystemIterator::CURRENT_AS_FILEINFO |
FilesystemIterator::KEY_AS_PATHNAME |
FilesystemIterator::SKIP_DOTS
)
);
// 拡張子がpdfのファイルのみ抽出
$files = new RegexIterator($files, '/^.+\.pdf$/i', RecursiveRegexIterator::MATCH);
foreach($files as $file_path => $file_info) {
// echo 'file name : '. basename($file_path) .PHP_EOL; //ファイル名
// echo 'file path : '. $file_path .PHP_EOL; //ファイルのフルパス
// echo 'file size : '. $file_info->getSize() .PHP_EOL; //ファイルサイズ
// echo 'contents : '. file_get_contents($file_path) .PHP_EOL; //ファイルの内容を取得
// echo 'serch dir : '. $serch_dir .PHP_EOL; //
//ファイル名のみ取得
$file_name = basename($file_path);
$reg="/(.*)(?:\.([^.]+$))/";
preg_match($reg,$file_name,$retArr);
$file_name = $retArr[1];
$extension = $retArr[2];
//ImageMagic処理
//im->readimage($file_path);
//$page_count = $im->getImageScene();
//PDFから画像ファイルへの変換処理
$output = shell_exec("convert ".$file_path." ".$serch_dir."/".$file_name.".png 2>&1");
print_r ($output);
// for($i = 0; $i <= $page_count-1; $i++) {
// $im->setIteratorIndex($i);
// $im->setImageFormat('png');
// $im->writeimage($serch_dir.'/'.$file_name . $i . "png");
// }
}
$im->clear();
?>
# chmod 775 /var/www/html/pdf/Imagick-Ghostscript.php
# chown root.apache /var/www/html/pdf/Imagick-Ghostscript.php
# php /var/www/html/pdf/Imagick-Ghostscript.php
# chmod 775 /var/www/html/pdf/Imagick-Ghostscript.php
# chown root.apache /var/www/html/pdf/Imagick-Ghostscript.php
# php /var/www/html/pdf/Imagick-Ghostscript.php
# view /var/log/httpd/error_log
----------
GPL Ghostscript 8.70: Unrecoverable error, exit code 1 ★バージョンが古い
convert: Postscript delegate failed `./sample.pdf': No such file or directory @ pdf.c/ReadPDFImage/611.
convert: missing an image filename `./sample_thumb.jpg' @ convert.c/ConvertImageCommand/2800.
# rpm -qa | grep "libXt-devel"
libXt-1.1.4-6.1.el6.x86_64
# ln -sb /usr/local/bin/gs /usr/bin/gs
----------
lrwxrwxrwx 1 root root 17 1月 5 22:36 2015 /usr/bin/gs -> /usr/local/bin/gs
# cd /usr/local/src/
# curl -OL http://downloads.ghostscript.com/public/ghostscript-9.15.tar.gz
# tar -zxvf ghostscript-9.15.tar.gz
# cd ghostscript-9.15
# ./configure --disable-compile-inits --with-x --with-drivers=ALL --without-luratech --with-libiconv=gnu --prefix=/usr/local
※オプション「--disable-compile-inits」を指定しないと、インストール後の設定変更ができなくなるとのこと
(省略)
Libtiff is now configured for x86_64-unknown-linux-gnu
Installation directory: /usr/local
Documentation directory: ${prefix}/share/doc/tiff-4.0.1
C compiler: gcc -g -O2 -Wall -W
C++ compiler: g++ -g -O2
Enable runtime linker paths: no
Enable linker symbol versioning: no
Support Microsoft Document Imaging: yes
Use win32 IO: no
Support for internal codecs:
CCITT Group 3 & 4 algorithms: yes
Macintosh PackBits algorithm: yes
LZW algorithm: yes
ThunderScan 4-bit RLE algorithm: yes
NeXT 2-bit RLE algorithm: yes
LogLuv high dynamic range encoding: yes
Support for external codecs:
ZLIB support: yes
Pixar log-format algorithm: yes
JPEG support: yes
Old JPEG support: yes
JPEG 8/12 bit dual mode: no
ISO JBIG support: no
LZMA2 support: no
C++ support: yes
OpenGL support: no
(省略)
configure: WARNING: Unable to include opvp/oprp driver due to missing or disabled prerequisites...
configure: WARNING: Unable to include opvp/oprp driver due to missing iconv/libiconv...
※上記、configure中に気になったメッセージ抜粋 → この後、問題となることはありませんでした。
# make
# make install
# cd /usr/local/src
# mkdir font
# cd font
# wget 'http://dl.ipafont.ipa.go.jp/IPAexfont/IPAexfont00201.zip'
# wget 'http://dl.ipafont.ipa.go.jp/IPAfont/IPAfont00303.zip'
# unzip IPAfont00303.zip
inflating: IPAfont00303/ipag.ttf
inflating: IPAfont00303/ipagp.ttf
inflating: IPAfont00303/ipam.ttf
inflating: IPAfont00303/ipamp.ttf
# unzip IPAexfont00201.zip
inflating: IPAexfont00201/ipaexg.ttf
inflating: IPAexfont00201/ipaexm.ttf
# mkdir -p /usr/share/fonts/ipa/TrueType/
# cp -p ./IPAexfont00201/*.ttf /usr/share/fonts/ipa/TrueType/.
# cp -p ./IPAfont00303/*.ttf /usr/share/fonts/ipa/TrueType/.
# ls -l /usr/share/fonts/ipa/TrueType/
ipaexg.ttf
ipaexm.ttf
ipag.ttf
ipagp.ttf
ipam.ttf
ipamp.ttf
・フォントキャシュクリア
# fc-cache -fv
・フォント一覧で名称を確認
# fc-list | grep -i ipa
IPAexゴシック,IPAexGothic:style=Regular,Regulare
IPAゴシック,IPAGothic:style=Regular
IPA Pゴシック,IPAPGothic:style=Regular
IPAex明朝,IPAexMincho:style=Regular
IPA明朝,IPAMincho:style=Regular
IPA P明朝,IPAPMincho:style=Regular
# yum install rpm-build ttmkfdir
# cd /usr/local/src/
# wget http://www.cabextract.org.uk/cabextract-1.4-1.src.rpm
# rpmbuild --rebuild cabextract-1.4-1.src.rpm
# rpm -ivh /root/rpmbuild/RPMS/x86_64/cabextract-1.4-1.x86_64.rpm
※buildしたrpmファイルは、「$HOME/rpmbuild/RPMS/x86_64/」配下に格納される
# cd /usr/local/src/
# wget http://corefonts.sourceforge.net/msttcorefonts-2.5-1.spec
# rpmbuild -bb msttcorefonts-2.5-1.spec
# rpm -ivh /root/rpmbuild/RPMS/noarch/msttcorefonts-2.5-1.noarch.rpm
※buildしたrpmファイルは、「$HOME/rpmbuild/RPMS/noarch/」配下に格納される
・インストール先は以下
/usr/share/fonts/msttcorefonts/*.ttf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment