adbird(広告鳥) 備忘録

PDFに簡単な文字入力

追記:LibreOfficeのDrawや、GIMP、Xournalなどの方が簡単…。


ある既存の単一ページのPDFデータに、ちょっとしたタイトルめいたテキストをページの上部に入れたいときに。

Pandoc、wkhtmltopdf、pdftkが必要。

スクリプト(textinpdf.sh)

テキストエディタで、以下の内容をtextinpdf.sh として保存。

#挿入したい文字列をtextinpdf.mdに保存
touch textinpdf.md
echo %mdtitle >> textinpdf.md
echo "$1" >> textinpdf.md

#pandocでmdをhtmlに変換、調整。フォントサイズ等の変更はこちら。
pandoc textinpdf.md -s -o textinpdf.html
sed -i '/header/d' textinpdf.html
sed -i '/h1 class/d' textinpdf.html
sed -i -e 's/<p>/<p style="font-family:'IPAexゴシック';font-size:15pt;">/g' textinpdf.html

#wkhtmltopdfでpdfに変換。
wkhtmltopdf --disable-smart-shrinking --margin-top $2 --margin-left $3 textinpdf.html textinpdf.pdf

#textinpdf.pdfを背景として重ねる。
pdftk $4.pdf background textinpdf.pdf output $4_`date +%Y%m%d`.pdf

#不必要なファイルを削除。
rm -f textinpdf.md
rm -f textinpdf.html
rm -f textinpdf.pdf

実行

上記 textinpdf.sh を、文字を入れたいPDFデータ(ここでは Test.pdf )と同じフォルダ内に入れて、

 $ sh textinpdf.sh "ここにテキスト" 13 20 Test

を実行。

  • "ここにテキスト" :ダブルクォート(" ")の中にテキストを書く。
  • 13       :ページ上からの位置。
  • 20       :ページ左からの位置。
  • Test      :文字列を入れたいPDFデータの名前。拡張子はいらない。