Skip to content

Instantly share code, notes, and snippets.

@dreamorosi
Last active March 9, 2022 18:02
Show Gist options
  • Save dreamorosi/4afafa7edb102a132b4d1bfbda51765b to your computer and use it in GitHub Desktop.
Save dreamorosi/4afafa7edb102a132b4d1bfbda51765b to your computer and use it in GitHub Desktop.
Convert Markdown to PDF with Asciidoctor

Use the script above by making it executable (chmod +x convert.sh) and running it (./convert.sh).

If you want to add a page break use <<<, if you want to add syntax highlighting add :source-highlighter: rouge at the top of the Markdown file, it won't be rendered in Markdown but will be used by asciidoctor.

#!/bin/bash
set -eux
INPUT_MD_FILE=tr.md
OUT_ADOC_FILE=tmpOut.adoc
OUT_TMP_PDF_FILE=tmpOut.pdf
OUT_PDF_FILE=documentation.pdf
# Clear any temporary files from previous executions
(rm -f "${OUT_ADOC_FILE}" "${OUT_PDF_FILE}" "${OUT_TMP_PDF_FILE}" || true)
# Convert Markdown to Asciidoctor
docker run --rm -v "$(pwd)":/data pandoc/core -f markdown -t asciidoc -i "${INPUT_MD_FILE}" -o "${OUT_ADOC_FILE}"
# Convert Asciidoctor to PDF
docker run --rm -v "$(pwd)":/documents/ asciidoctor/docker-asciidoctor asciidoctor-pdf "${OUT_ADOC_FILE}"
# Rename the output PDF to a more proper one
mv "${OUT_TMP_PDF_FILE}" "${OUT_PDF_FILE}"
# Clean up
(rm -f "${OUT_ADOC_FILE}" "${OUT_TMP_PDF_FILE}" || true)
@dreamorosi
Copy link
Author

A very nice script for the conversion of markdown files. One minor comment, could you add quotes "$(pwd)" to the second docker command?

Thanks for pointing it out, I have updated the gist 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment