]> Git — Sourcephile - julm/books.git/blob - scripts/epub2pdf.sh
init
[julm/books.git] / scripts / epub2pdf.sh
1 #!/usr/bin/env nix
2 #! nix shell --impure --expr ``
3 #! nix with (builtins.getFlake "git+file://${toString ./.}/..").packages.${builtins.currentSystem};
4 #! nix let fonts = [
5 #! nix pkgs.comic-mono
6 #! nix pkgs.crimson
7 #! nix pkgs.dejavu_fonts
8 #! nix pkgs.fira-code
9 #! nix pkgs.freefont_ttf
10 #! nix pkgs.gyre-fonts # TrueType substitutes for standard PostScript fonts
11 #! nix pkgs.hack-font
12 #! nix pkgs.liberation_ttf
13 #! nix pkgs.lmodern
14 #! nix pkgs.noto-fonts-emoji
15 #! nix pkgs.tex-gyre.adventor
16 #! nix pkgs.tex-gyre.bonum
17 #! nix pkgs.tex-gyre.chorus
18 #! nix pkgs.tex-gyre.cursor
19 #! nix pkgs.tex-gyre.heros
20 #! nix pkgs.tex-gyre.pagella
21 #! nix pkgs.tex-gyre.schola
22 #! nix pkgs.tex-gyre.termes
23 #! nix pkgs.unifont
24 #! nix ]; in
25 #! nix [
26 #! nix (pkgs.writeShellScriptBin "interpreter" ''
27 #! nix # used by luaotfload (lualatex)
28 #! nix export OSFONTDIR="${lib.concatStringsSep "//:" (map toString fonts)}"
29 #! nix exec bash "$@"
30 #! nix '')
31 #! nix pandoc
32 #! nix pdftk
33 #! nix qpdf
34 #! nix (pkgs.texlive.combine {
35 #! nix inherit (pkgs.texlive)
36 #! nix collection-fontsrecommended
37 #! nix collection-langenglish
38 #! nix collection-langfrench
39 #! nix collection-latexrecommended
40 #! nix collection-luatex
41 #! nix pdfbook2
42 #! nix pdfcrop
43 #! nix pdfjam
44 #! nix scheme-basic
45 #! nix tex-gyre
46 #! nix ;
47 #! nix })
48 #! nix ]
49 #! nix ``
50 #! nix --command interpreter
51
52 epubPath=$1
53 epubDir=$(realpath -e "$(dirname "$epubPath")")
54 styleDir=$(realpath -e "$(dirname "$0")")/styles
55 outDir=$epubDir/pdf
56 pdfFile="$outDir"/out.pdf
57 rm -rf "$outDir"
58 mkdir -p "$outDir"
59
60 # Converting EPUB to PDF
61 set -eux
62 pandoc \
63 --embed-resources \
64 --include-in-header "$styleDir"/epub.header.tex \
65 --filter "$epubDir"/filter \
66 --number-sections \
67 --pdf-engine lualatex \
68 --standalone \
69 --toc \
70 --toc-depth 6 \
71 --top-level-division chapter \
72 -V documentclass=scrbook \
73 -V classoption=english \
74 -V mainfont="TeX Gyre Termes" \
75 -V fontsize=13pt \
76 -V papersize=a5 \
77 -i "$epubPath" \
78 -t pdf \
79 -o "$pdfFile"
80
81 # Slicing on chapters and splitting into chunks of at most 18 pages (for easy binding)
82 mkdir -p "$outDir"/sections
83 pdftk "$pdfFile" dump_data | grep '^BookmarkPageNumber:' |
84 {
85 sectionNum=1
86 sectionBegin=1
87 splitSection () {
88 qpdf \
89 "$pdfFile" \
90 "$outDir"/sections/sectionNum="$sectionNum".pages="$sectionPages".chunk=%d.pdf \
91 --pages "$pdfFile" "$sectionPages" -- \
92 --split-pages=18
93 }
94 while read -r _BookmarkPageNumber sectionEnd; do
95 test "$sectionEnd" != 1 || continue
96 sectionPages=$sectionBegin-$((sectionEnd-1))
97 splitSection
98 sectionNum=$((sectionNum+1))
99 sectionBegin=$sectionEnd
100 done
101 sectionPages="$sectionBegin"-r1
102 splitSection
103 }
104
105 # Converting to booklet
106 find "$outDir"/sections -type f |
107 sort -n |
108 while read -r pdfSection; do
109 # Margins have already been set correctly by the geometry package in header.tex
110 # pdfbook2 fails to move the .pdf over filesystems
111 pdfbook2 --no-crop \
112 --paper "${paper:-a4paper}" \
113 --outer-margin "${outerMargin:-80}" \
114 --inner-margin "${innerMargin:-150}" \
115 --top-margin "${topmargin:-30}" \
116 --bottom-margin "${bottomMargin:-30}" \
117 "$pdfSection"
118 rm "$pdfSection"
119
120 # Split odd and even pages to print in the same direction
121 pdftk "${pdfSection%.pdf}"-book.pdf cat oddSouth output "${pdfSection%.pdf}".book.odds.pdf
122 pdftk "${pdfSection%.pdf}"-book.pdf cat even output "${pdfSection%.pdf}".book.even.pdf
123 rm "${pdfSection%.pdf}"-book.pdf
124 done