Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash script to generate books from markdown in printable pdf, ebook pdf, epub, mobi and pdf preview formats
#!/bin/bash
hash pandoc 2>/dev/null || { echo >&2 "Please install pandoc. Aborting."; exit 1; }
hash pdftk 2>/dev/null || { echo >&2 "Please install pdftk. Aborting."; exit 1; }
hash ebook-convert 2>/dev/null || { echo >&2 "Please install ebook-convert. Aborting."; exit 1; }
SOURCE=Usable-Software-Design.md
PRINT_DESTINATION=Usable-Software-Design-print.pdf
PDF_DESTINATION=Usable-Software-Design.pdf
PREVIEW_DESTINATION=Usable-Software-Design-Preview.pdf
EPUB_DESTINATION=Usable-Software-Design.epub
MOBI_DESTINATION=Usable-Software-Design.mobi
BACKUP_DESTINATION="~/Dropbox/Book/Usable-Software-Design"
PRINT_COVER=cover.pdf
PRINT_BACK_COVER=back-cover.pdf
EBOOK_COVER=cover-ebook.pdf
EBOOK_BACK_COVER=back-cover-simple.pdf
MAIN_FONT="FreeSerif"
SANS_FONT="DejaVuSans"
MONO_FONT="Inconsolata"
FONT_SIZE=12pt
EPUB_TITLE=title.txt
EPUB_COVER=cover-ebook.png
PREVIEW_PAGE_RANGE=1-8
# Print version
echo "++++++++ Generate print version"
pandoc $SOURCE --toc --variable mainfont=$MAIN_FONT --variable sansfont=$SANS_FONT --variable monofont=$MONO_FONT --variable fontsize=$FONT_SIZE --smart --chapters --highlight-style=monochrome -V papersize=a4paper -V documentclass=book --normalize -o temp_print_book.pdf
if [ $? -ne 0 ]; then
echo "Pdf generation failed. Please make sure the fonts you use are installed and available to your latex engine (eg. packages texlive-fonts-*). Aborting.";
exit 1;
fi
pdftk $PRINT_COVER temp_print_book.pdf $PRINT_BACK_COVER cat output $PRINT_DESTINATION
cp $PRINT_DESTINATION $BACKUP_DESTINATION
rm temp_print_book.pdf
# PDF Ebook version
echo "++++++++ Generate pdf ebook version"
pandoc $SOURCE --toc --variable mainfont=$MAIN_FONT --variable sansfont=$SANS_FONT --variable monofont=$MONO_FONT --variable fontsize=$FONT_SIZE --smart --chapters --highlight-style=monochrome -V papersize=a4paper -V documentclass=book -V geometry:margin=1in --normalize -o temp_ebook.pdf
pdftk $EBOOK_COVER temp_ebook.pdf $EBOOK_BACK_COVER cat output $PDF_DESTINATION
cp $PDF_DESTINATION $BACKUP_DESTINATION
rm temp_ebook.pdf
# EPUB version
echo "++++++++Generate epub version"
pandoc -S --toc $SOURCE $EPUB_TITLE --epub-cover-image=$EPUB_COVER -o $EPUB_DESTINATION
cp $EPUB_DESTINATION $BACKUP_DESTINATION
# Mobi version
echo "++++++++Generate mobi version"
ebook-convert $EPUB_DESTINATION $MOBI_DESTINATION --output-profile=kindle
cp $MOBI_DESTINATION $BACKUP_DESTINATION
# Extracted PDF preview version
echo "++++++++Generate pdf preview version"
pdftk $PDF_DESTINATION cat $PREVIEW_PAGE_RANGE output $PREVIEW_DESTINATION
cp $PREVIEW_DESTINATION $BACKUP_DESTINATION
# Backup source
echo "++++++++Backing up"
cp $SOURCE $BACKUP_DESTINATION/$DESTINATION
echo "++++++++Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.