moodleからヘッダー付のまとめpdfを作成 (v2)

moodleからフッター付のまとめpdfを作成
http://qiita.com/keitasumiya/items/7975b068f22ddb7ddb82
の変更記事です. 

前記事を真面目に書きすぎて修正するのが面倒になったので, 新記事にしました. 

# 変更点
- txtのpdf化をpandocでやる
http://qiita.com/keitasumiya/items/8f4febe8d5a0b66a4b9a
- フッターではなく, ヘッダーに. 
- 各種ファイルのpdf化はlibreofficeでやる

にしたことで, 少しだけ楽になりました. 

# 問題点
- 濁点のファイル名に未対応
- ```$```のあるファイル名に未対応
- ~~```(```もしくは```)```のあるファイル名に未対応~~

# environments

```
mac osx 10.11 el capitan
pandoc 1.17.0.3 < Compiled with texmath 0.8.6.3, highlighting-kate 0.6.2.
pdftk2.02
LibreOffice 5.3.3.2
```

# git
https://github.com/KeitaSumiya/shellscript/tree/master/moodle2pdf_header

# tree

```
$ 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
```

# commands

```
mkdir pdf
mkdir txt
mkdir txt_pdf
mkdir output_pdf

cd students/
sed -e '1d' *.csv > students.txt
cat students.txt | awk -F',| ' '{print $2"_"$1"_"substr($6,2,6)}' > students_edited.txt

cd ../file/
ls | awk '{a=$0; gsub(" ","_",a); print "mv \""$0"\" \""a"\""}' | sh
ls | sed 's/\./zzzz/g' | awk -F'_|zzzz| ' '{a=$0; gsub("zzzz",".",a); print "mv \""a"\" \""$1"_"$2"."$NF"\""}' | sh
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
```

```
for i in `ls | grep -v .pdf`; do soffice --headless --convert-to pdf:writer_pdf_Export $i; done
ここは
soffice --headless --convert-to pdf:writer_pdf_Export *
でひとまとめにしてもおkです. 
上記は, pdfを飛ばしたかったためです. 

libreofficeだと表示が変わったりするので, 確実に変換したければacrobatでの変換をオススメします. 
```

```
cp *.pdf ../pdf/

cd ../pdf/
ls | sed 's/\./_/g' | awk -F'_| ' '{print "echo",$1"_"$2,">",$1".txt"}' | sh
ls | grep .txt | awk '{print "mv",$0,"../txt"}' | sh

cd ../txt
ls | sed 's/\./_/g' | awk -F'_| ' '{print "pandoc",$1".txt -o",$1".pdf -V documentclass=ltjarticle --latex-engine=lualatex -V geometry:margin=0mm -V papersize:a4"}' | sh
ls | grep .pdf | awk '{print "mv",$0,"../txt_pdf"}' | sh

cd ../pdf
ls | awk -F'_| ' '{print "pdftk",$0,"stamp ../txt_pdf/"$1".pdf","output ../output_pdf/"$1".pdf"}' | sh

cd ../output_pdf
pdftk *.pdf output ../output.pdf
```

# results

```
$ 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
├── 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
```

## acrobat版
![スクリーンショット 2017-05-22 13.55.00.png](https://qiita-image-store.s3.amazonaws.com/0/102253/81276fab-85a3-9778-4453-1e364f2e13c2.png)
![スクリーンショット 2017-05-22 13.55.11.png](https://qiita-image-store.s3.amazonaws.com/0/102253/f44c3708-88f2-c06d-84ce-e5edb69f8244.png)

## libreoffice版
![スクリーンショット 2017-05-22 18.13.50.png](https://qiita-image-store.s3.amazonaws.com/0/102253/083e8f2a-a918-84bd-a9dc-f44b8d1e7c5c.png)
![スクリーンショット 2017-05-22 18.14.00.png](https://qiita-image-store.s3.amazonaws.com/0/102253/7be210a8-2b5a-7937-bbab-d4d2df1d91d4.png)


# bash

```tree.
$ tree
.
├── 1_rename.sh
├── 2_mk_merge_pdf.sh
├── 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
```

```1_rename.sh
#!/bin/sh

mkdir pdf
mkdir txt
mkdir txt_pdf
mkdir output_pdf

cd students/
sed -e '1d' *.csv > students.txt
cat students.txt | awk -F',| ' '{print $2"_"$1"_"substr($6,2,6)}' > students_edited.txt

cd ../file/
ls | awk '{a=$0; gsub(" ","_",a); print "mv \""$0"\" \""a"\""}' | sh
ls | sed 's/\./zzzz/g' | awk -F'_|zzzz| ' '{a=$0; gsub("zzzz",".",a); print "mv \""a"\" \""$1"_"$2"."$NF"\""}' | sh
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
```

```2_file2pdf.sh
#!/bin/sh
cd file/
for i in `ls | grep -v .pdf`; do soffice --headless --convert-to pdf:writer_pdf_Export $i; done
```

```3_mk_merge_pdf.sh
#!/bin/sh
cd file/

cp *.pdf ../pdf/

cd ../pdf/
ls | sed 's/\./_/g' | awk -F'_| ' '{print "echo",$1"_"$2,">",$1".txt"}' | sh
ls | grep .txt | awk '{print "mv",$0,"../txt"}' | sh

cd ../txt
ls | sed 's/\./_/g' | awk -F'_| ' '{print "pandoc",$1".txt -o",$1".pdf -V documentclass=ltjarticle --latex-engine=lualatex -V geometry:margin=0mm -V papersize:a4"}' | sh
ls | grep .pdf | awk '{print "mv",$0,"../txt_pdf"}' | sh

cd ../pdf
ls | awk -F'_| ' '{print "pdftk",$0,"stamp ../txt_pdf/"$1".pdf","output ../output_pdf/"$1".pdf"}' | sh

cd ../output_pdf
pdftk *.pdf output ../output.pdf
```

## usage

```
chmod +x 1_rename.sh
chmod +x 2_file2pdf.sh
chmod +x 3_mk_merge_pdf.sh 
./1_rename.sh

pdf化 by acrobat
or
./2_file2pdf.sh

./3_mk_merge_pdf.sh 
```

もしくは

```
./0_all
```

で全部一気にやるのもおkです. 

# video
https://youtu.be/AEVmqwD1Qkc
https://youtu.be/zL6Quz64Lh8

# ref
前記事
http://qiita.com/keitasumiya/items/7975b068f22ddb7ddb82
複数のpdfにheaderをつける. txt等からpdfを作成する. 
http://qiita.com/keitasumiya/items/8f4febe8d5a0b66a4b9a
make pdf with libreoffice
http://qiita.com/keitasumiya/items/4b2b1f6269413225b7cd