9
10

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 5 years have passed since last update.

複数のPNG画像をまとめて一つのPDFにする

Last updated at Posted at 2014-06-11

制作したシステムやコンテンツにレイアウトの崩れがないかざっくりと効率よくみるために、CasperJSやPhantomJSでまとめてスクリーンショットを撮ることがあります。

それらの結果の閲覧効率を上げたり、エビデンスとして他の誰かに共有するためにひとつのPDFにしたいケースがあるためシェルスクリプトを作成しました。

#!/bin/sh
 
###########################################
# Convert PNG files to one PDF
###########################################
 
CURRENT_DIR=$(cd $(dirname $0);pwd)
TMP_PDF1=${CURRENT_DIR}/tmp1.pdf
TMP_PDF2=${CURRENT_DIR}/tmp2.pdf
OUTPUT_PDF=${CURRENT_DIR}/screen.pdf
PNG_DIR=${CURRENT_DIR}/images/origin
 
rm ${OUTPUT_PDF}
 
find ${PNG_DIR} -name '*.png' | while read PNG; do
    echo ${PNG}
    if [ -f ${OUTPUT_PDF} ]; then
        convert -page A4 ${PNG} ${TMP_PDF1}
        mv ${OUTPUT_PDF} ${TMP_PDF2}
        pdftk ${TMP_PDF2} ${TMP_PDF1} cat output ${OUTPUT_PDF}
    else
        convert -page A4 ${PNG} ${OUTPUT_PDF}
    fi
done
 
if [ -f ${TMP_PDF1} ]; then
    rm ${TMP_PDF1}
fi
 
if [ -f ${TMP_PDF2} ]; then
    rm ${TMP_PDF2}
fi

ちなみにpdftkコマンドとconvertコマンドはbrewから下記のようにすればインストールされます。

brew install --devel casperjs
brew tap docmunch/pdftk
brew install pdftk
brew install imagemagick
9
10
6

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
9
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?