#!/usr/bin/env nix #! nix shell --impure --expr `` #! nix with (builtins.getFlake "git+file://${toString ./.}/..").packages.${builtins.currentSystem}; #! nix let fonts = [ #! nix pkgs.comic-mono #! nix pkgs.crimson #! nix pkgs.dejavu_fonts #! nix pkgs.fira-code #! nix pkgs.freefont_ttf #! nix pkgs.gyre-fonts # TrueType substitutes for standard PostScript fonts #! nix pkgs.hack-font #! nix pkgs.liberation_ttf #! nix pkgs.lmodern #! nix pkgs.noto-fonts-emoji #! nix pkgs.tex-gyre.adventor #! nix pkgs.tex-gyre.bonum #! nix pkgs.tex-gyre.chorus #! nix pkgs.tex-gyre.cursor #! nix pkgs.tex-gyre.heros #! nix pkgs.tex-gyre.pagella #! nix pkgs.tex-gyre.schola #! nix pkgs.tex-gyre.termes #! nix pkgs.unifont #! nix ]; in #! nix [ #! nix (pkgs.writeShellScriptBin "interpreter" '' #! nix # used by luaotfload (lualatex) #! nix export OSFONTDIR="${lib.concatStringsSep "//:" (map toString fonts)}" #! nix exec bash "$@" #! nix '') #! nix pandoc #! nix pdftk #! nix qpdf #! nix (pkgs.texlive.combine { #! nix inherit (pkgs.texlive) #! nix collection-fontsrecommended #! nix collection-langenglish #! nix collection-langfrench #! nix collection-latexrecommended #! nix collection-luatex #! nix pdfbook2 #! nix pdfcrop #! nix pdfjam #! nix scheme-basic #! nix tex-gyre #! nix ; #! nix }) #! nix ] #! nix `` #! nix --command interpreter epubPath=$1 epubDir=$(realpath -e "$(dirname "$epubPath")") styleDir=$(realpath -e "$(dirname "$0")")/styles outDir=$epubDir/pdf pdfFile="$outDir"/out.pdf rm -rf "$outDir" mkdir -p "$outDir" # Converting EPUB to PDF set -eux pandoc \ --embed-resources \ --include-in-header "$styleDir"/epub.header.tex \ --filter "$epubDir"/filter \ --number-sections \ --pdf-engine lualatex \ --standalone \ --toc \ --toc-depth 6 \ --top-level-division chapter \ -V documentclass=scrbook \ -V classoption=english \ -V mainfont="TeX Gyre Termes" \ -V fontsize=13pt \ -V papersize=a5 \ -i "$epubPath" \ -t pdf \ -o "$pdfFile" # Slicing on chapters and splitting into chunks of at most 18 pages (for easy binding) mkdir -p "$outDir"/sections pdftk "$pdfFile" dump_data | grep '^BookmarkPageNumber:' | { sectionNum=1 sectionBegin=1 splitSection () { qpdf \ "$pdfFile" \ "$outDir"/sections/sectionNum="$sectionNum".pages="$sectionPages".chunk=%d.pdf \ --pages "$pdfFile" "$sectionPages" -- \ --split-pages=18 } while read -r _BookmarkPageNumber sectionEnd; do test "$sectionEnd" != 1 || continue sectionPages=$sectionBegin-$((sectionEnd-1)) splitSection sectionNum=$((sectionNum+1)) sectionBegin=$sectionEnd done sectionPages="$sectionBegin"-r1 splitSection } # Converting to booklet find "$outDir"/sections -type f | sort -n | while read -r pdfSection; do # Margins have already been set correctly by the geometry package in header.tex # pdfbook2 fails to move the .pdf over filesystems pdfbook2 --no-crop \ --paper "${paper:-a4paper}" \ --outer-margin "${outerMargin:-80}" \ --inner-margin "${innerMargin:-150}" \ --top-margin "${topmargin:-30}" \ --bottom-margin "${bottomMargin:-30}" \ "$pdfSection" rm "$pdfSection" # Split odd and even pages to print in the same direction pdftk "${pdfSection%.pdf}"-book.pdf cat oddSouth output "${pdfSection%.pdf}".book.odds.pdf pdftk "${pdfSection%.pdf}"-book.pdf cat even output "${pdfSection%.pdf}".book.even.pdf rm "${pdfSection%.pdf}"-book.pdf done