2021-08-16
Adobe, Moodle, PDF, PDFtk, ShellScript
moodleからフッター付のまとめpdfを作成
注意
v2の自記事があります. moodleからヘッダー付のまとめpdfを作成 (v2) http://qiita.com/keitasumiya/items/665453b54a9ffb4cfdaf
はじめに
open sourceのmoodleという授業支援システムからダウンロードした提出物のファイルをフッター付でpdfにまとめるtipsです. 出来具合はちょっと不格好です.
念のためいっておきますが, 下記の学生番号等はもちろんダミーの適当なものにしてあります. 本記事内のダミーならば動くようにはしてありますが, 場合によっては必要に応じて変更してください.
また, pdftkというcommandを使用していますので, 未installであれば http://qiita.com/keitasumiya/items/83756caf2865291707fb 等を参考にinstallしてください.
任意ファイルをpdfに変換するためにはacrobat Acrobat Pro DC 2015.006.30306 を使用しています. commandでいければいいんですが... どなたかご存知でしたら教えていただけると嬉しいです.
準備(ダウンロード)
課題, 評定, 評定操作, 全ての提出をダウンロードする
でfileをダウンロード.

課題, 評定, 評定操作, 評定表を表示する, 評定者レポート, テキストファイル
で評定者リストのcsvをダウンロード.

初期設定
ダウンロードしてきたファイルを下記のように配置
対象のfileの拡張子は何でも良い
現在のdirectoryは
$ pwd/Users/hogeuser/Downloads/pdf_test/editted
にいると仮定している.
$ tree.├── file│ ├── Sato\ Jiro_123102_assignsubmission_file_課?\214.pdf│ ├── Yamada\ Taro_123001_assignsubmission_file_abc?\217?\201?課?\214.docx│ └── 山?\234?\ ?\211?\203\216_123023_assignsubmission_onlinetext_onlinetext.html└── students└── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv

csvの中身:
名,姓,IDナンバー,所属組織,部署,メールアドレス,"課題a (実データ)","課題b (実データ)","課題c (実データ)","コース合計 (実データ)",このコースからの最新ダウンロードTaro,Yamada,,,,1ab9999t@hogehoge.jp,-,-,-,-,1234567890Jiro,Sato,,,,1hg9980j@hogehoge.jp,-,-,-,-,1234567890Naoko,Kimura,,,,1cd9997n@hogehoge.jp,-,-,-,-,1234567890三郎,山本,,,,1ef9990s@hogehoge.jp,-,-,-,-,1234567890
commands
csvをstudent.txtにして, 1行目のlabelを消す
$ cd students/$ sed -e '1d' *.csv > students.txt
$ tree ....├── file│ ├── Sato\ Jiro_123102_assignsubmission_file_課?\214.pdf│ ├── Yamada\ Taro_123001_assignsubmission_file_abc?\217?\201?課?\214.docx│ └── 山?\234?\ ?\211?\203\216_123023_assignsubmission_onlinetext_onlinetext.html└── students├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv└── students.txt

Taro,Yamada,,,,1ab9999t@hogehoge.jp,-,-,-,-,1234567890Jiro,Sato,,,,1hg9980j@hogehoge.jp,-,-,-,-,1234567890Naoko,Kimura,,,,1ef9997n@hogehoge.jp,-,-,-,-,1234567890三郎,山本,,,,1ef9990s@hogehoge.jp,-,-,-,-,1234567890
student情報の整理
xxxxxxxxxx$ cat students.txt | awk -F',| ' '{print $2"_"$1"_"substr($6,2,6)}' > students_edited.txt
xxxxxxxxxxYamada_Taro_ab9999Sato_Jiro_hg9980Kimura_Naoko_ef9997山本_三郎_ef9990
スペースの削除
xxxxxxxxxx$ cd ../file/$ ls | awk '{a=$0; gsub(" ","_",a); print "mv \""$0"\"",a}' | sh
xxxxxxxxxx$ tree ....├── file│ ├── Sato_Jiro_123102_assignsubmission_file_課?\214.pdf│ ├── Yamada_Taro_123001_assignsubmission_file_abc?\217?\201?課?\214.docx│ └── 山?\234?_?\211?\203\216_123023_assignsubmission_onlinetext_onlinetext.html└── students├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv├── students.txt└── students_edited.txt
fileの名前の変更
xxxxxxxxxx$ ls | sed 's/\./zzzz/g' | awk -F'_|zzzz| ' '{a=$0; gsub("zzzz",".",a); print "mv",a,$1"_"$2"."$NF}' | sh
xxxxxxxxxx$ tree ....├── file│ ├── Sato_Jiro.pdf│ ├── Yamada_Taro.docx│ └── 山?\234?_?\211?\203\216.html└── students├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv├── students.txt└── students_edited.txt
file名に学生番号を追加
xxxxxxxxxx$ for i in `ls`; do echo $i" "`grep ${i%.*} ../students/students_edited.txt` | sed 's/\./ /g' |awk -F' |_' '{print "mv "$1"_"$2"."$3" "$6"_"$1$2"."$3}' | sh; done
xxxxxxxxxx$ tree ....├── file│ ├── ab9999_YamadaTaro.docx│ ├── ef9990_山?\234??\211?\203\216.html│ └── hg9980_SatoJiro.pdf└── students├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv├── students.txt└── students_edited.txt
acrobatでfile内のファイルを個別にpdf化
xxxxxxxxxxツールpdfを作成複数のファイル複数のpdfファイルを作成次へfile内のファイルをドラック&ドロップOKOK

pdfを別のdirectoryに移す
xxxxxxxxxx$ mkdir ../pdf$ cp *.pdf ../pdf/
xxxxxxxxxx$ tree ....├── file│ ├── ab9999_YamadaTaro.docx│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.html│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf└── students├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv├── students.txt└── students_edited.txt
フッター用のtxtの作成
xxxxxxxxxx$ cd ../pdf/$ ls | awk -F'_| ' '{print "touch","____________________________________________________________________________"$1".txt"}' | sh$ mkdir ../txt$ ls | grep .txt | awk '{print "mv",$0,"../txt"}' | sh
xxxxxxxxxx$ tree ....├── file│ ├── ab9999_YamadaTaro.docx│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.html│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf├── students│ ├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv│ ├── students.txt│ └── students_edited.txt└── txt├── ____________________________________________________________________________ab9999.txt├── ____________________________________________________________________________ef9990.txt└── ____________________________________________________________________________hg9980.txt
acrobatでtxt内のファイルを個別にpdf化
xxxxxxxxxxツールpdfを作成複数のファイル複数のpdfファイルを作成次へtxt内のファイルをドラック&ドロップOKOK
フッター用pdfを移す
xxxxxxxxxx$ mkdir ../txt_pdf$ cd ../txt$ ls | grep .pdf | awk '{print "mv",$0,"../txt_pdf"}' | sh
xxxxxxxxxx$ tree ....├── file│ ├── ab9999_YamadaTaro.docx│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.html│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf├── students│ ├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv│ ├── students.txt│ └── students_edited.txt├── txt│ ├── ____________________________________________________________________________ab9999.txt│ ├── ____________________________________________________________________________ef9990.txt│ └── ____________________________________________________________________________hg9980.txt└── txt_pdf├── ____________________________________________________________________________ab9999.pdf├── ____________________________________________________________________________ef9990.pdf└── ____________________________________________________________________________hg9980.pdf
フッターpdfと目的のファイルを重ねる
xxxxxxxxxx$ cd ../pdf$ mkdir ../output_pdf$ ls | awk -F'_| ' '{print "pdftk",$0,"background ../txt_pdf/____________________________________________________________________________"$1".pdf","output ../output_pdf/"$1".pdf"}' | sh
xxxxxxxxxx$ tree ....├── file│ ├── ab9999_YamadaTaro.docx│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.html│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf├── output_pdf│ ├── ab9999.pdf│ ├── ef9990.pdf│ └── hg9980.pdf│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf├── students│ ├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv│ ├── students.txt│ └── students_edited.txt├── txt│ ├── ____________________________________________________________________________ab9999.txt│ ├── ____________________________________________________________________________ef9990.txt│ └── ____________________________________________________________________________hg9980.txt└── txt_pdf├── ____________________________________________________________________________ab9999.pdf├── ____________________________________________________________________________ef9990.pdf└── ____________________________________________________________________________hg9980.pdf
最後の重ねるコマンドは, 本当はフッターpdfを上からstampしたい:
xxxxxxxxxx$ ls | awk -F'_| ' '{print "pdftk",$0,"stamp ../txt_pdf/____________________________________________________________________________"$1".pdf","output ../output_pdf/"$1".pdf"}' | sh
ただしこうすると, txtから作成したフッターpdfに白背景があるらしく, すべてフッターpdfに上書きされてしまうのが難点. フッターpdfの作成方法を見直す必要がある.
結果のpdfをmergeする
xxxxxxxxxx$ cd ../output$ pdftk *.pdf output ../output.pdf
この結合作業はacrobatの
xxxxxxxxxxツールpdfを作成複数のファイルファイルを結合次へtxt内のファイルをドラック&ドロップファイルを結合
でも良い.
xxxxxxxxxx$ tree ....├── file│ ├── ab9999_YamadaTaro.docx│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.html│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf├── output.pdf├── output_pdf│ ├── ab9999.pdf│ ├── ef9990.pdf│ └── hg9980.pdf│ ├── ab9999_YamadaTaro.pdf│ ├── ef9990_山?\234??\211?\203\216.pdf│ └── hg9980_SatoJiro.pdf├── students│ ├── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv│ ├── students.txt│ └── students_edited.txt├── txt│ ├── ____________________________________________________________________________ab9999.txt│ ├── ____________________________________________________________________________ef9990.txt│ └── ____________________________________________________________________________hg9980.txt└── txt_pdf├── ____________________________________________________________________________ab9999.pdf├── ____________________________________________________________________________ef9990.pdf└── ____________________________________________________________________________hg9980.pdf
このoutput.pdfがフッター付の完成ファイルです.
完成物

まとめ
初期設定
xxxxxxxxxx$ pwd/Users/hogeuser/Downloads/pdf_test/editted
xxxxxxxxxx$ tree.├── file│ ├── Sato\ Jiro_123102_assignsubmission_file_課?\214.pdf│ ├── Yamada\ Taro_123001_assignsubmission_file_abc?\217?\201?課?\214.docx│ └── 山?\234?\ ?\211?\203\216_123023_assignsubmission_onlinetext_onlinetext.html└── students└── 987654_2017\ ?\225?\232-20170510_0831-comma_separated.csv
手順
xxxxxxxxxxcd students/sed -e '1d' *.csv > students.txtcat students.txt | awk -F',| ' '{print $2"_"$1"_"substr($6,2,6)}' > students_edited.txtcd ../file/ls | awk '{a=$0; gsub(" ","_",a); print "mv \""$0"\"",a}' | shls | sed 's/\./zzzz/g' | awk -F'_|zzzz| ' '{a=$0; gsub("zzzz",".",a); print "mv",a,$1"_"$2"."$NF}' | shfor i in `ls`; do echo $i" "`grep ${i%.*} ../students/students_edited.txt` | sed 's/\./ /g' |awk -F' |_' '{print "mv "$1"_"$2"."$3" "$6"_"$1$2"."$3}' | sh; done
acrobatでfile内のファイルを個別にpdf化
xxxxxxxxxxmkdir ../pdfcp *.pdf ../pdf/cd ../pdf/ls | awk -F'_| ' '{print "touch","____________________________________________________________________________"$1".txt"}' | shmkdir ../txtls | grep .txt | awk '{print "mv",$0,"../txt"}' | sh
acrobatでtxt内のファイルを個別にpdf化
xxxxxxxxxxmkdir ../txt_pdfcd ../txtls | grep .pdf | awk '{print "mv",$0,"../txt_pdf"}' | shcd ../pdfmkdir ../output_pdfls | awk -F'_| ' '{print "pdftk",$0,"background ../txt_pdf/____________________________________________________________________________"$1".pdf","output ../output_pdf/"$1".pdf"}' | shcd ../output_pdfpdftk *.pdf output ../output.pdf
refs
1行消す http://takuya-1st.hatenablog.jp/entry/2014/08/07/012422
acrobatでのファイル結合 https://helpx.adobe.com/jp/acrobat/using/merging-files-single-pdf.html
acrobatでのヘッター, フッター https://helpx.adobe.com/jp/acrobat/using/add-headers-footers-pdfs.html
iphotoをつかったファイル名付加印刷 https://discussionsjapan.apple.com/thread/10085126?tstart=0
pdftk 気をつけること http://qiita.com/keitasumiya/items/83756caf2865291707fb
pdftk http://nwpct1.hatenablog.com/entry/2013/12/14/192914 https://www.pdflabs.com/tools/pdftk-server/ http://www.slab.ces.kyutech.ac.jp/~saitoh/ja/pdftk.html
空ファイルを作る http://qiita.com/kaw/items/3bbd6c2da1dde92bd8e5
コマンド: 過去の自分のを見て思い出す用 http://qiita.com/keitasumiya/items/7df5014a8c00c64ceda3 http://qiita.com/keitasumiya/items/7e63fffab0ebf7516317
awk https://codezine.jp/article/detail/7912 http://qiita.com/sand_bash/items/3d746c7e292908c6167e
拡張子取り出し http://d.hatena.ne.jp/zariganitosh/20100921/get_file_name_ext_dir http://pgman.exblog.jp/14865819/ http://qiita.com/mriho/items/b30b3a33e8d2e25e94a8
sed http://qiita.com/takc923/items/bdce8c1a2126cac52492
grep https://hydrocul.github.io/wiki/commands/grep.html
imagemagick http://qiita.com/hsagae/items/1b72fb9f3d6024ba46f0 http://www.imagemagick.org/Usage/annotating/
acrobat commandline https://www.lifedesignedit.com/use_adobe_pdf_command_line_options