Created
March 18, 2026 14:40
-
-
Save pHo9UBenaA/0ee5b62caea71b3bd4347444485b72b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -Eeuo pipefail | |
| # Usage: | |
| # ./md-to-pdf.sh <input_dir> <output_dir> [config_js] | |
| # | |
| # Example: | |
| # ./md-to-pdf.sh ./docs ./out ./config.js | |
| if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then | |
| echo "Usage: $0 <input_dir> <output_dir> [config_js]" >&2 | |
| exit 1 | |
| fi | |
| INPUT_DIR="${1%/}" | |
| OUTPUT_DIR="${2%/}" | |
| CONFIG_JS="${3:-}" | |
| if [ ! -d "$INPUT_DIR" ]; then | |
| echo "Input directory not found: $INPUT_DIR" >&2 | |
| exit 1 | |
| fi | |
| if ! command -v md-to-pdf >/dev/null 2>&1; then | |
| echo "md-to-pdf command not found" >&2 | |
| exit 1 | |
| fi | |
| while IFS= read -r -d '' src; do | |
| rel="${src#"$INPUT_DIR"/}" | |
| rel_no_ext="${rel%.md}" | |
| dst="$OUTPUT_DIR/$rel_no_ext.pdf" | |
| temp_pdf="${src%.md}.pdf" | |
| mkdir -p "$(dirname "$dst")" | |
| echo "Converting: $src -> $dst" | |
| if [ -n "$CONFIG_JS" ]; then | |
| md-to-pdf "$src" \ | |
| --config-file "$CONFIG_JS" \ | |
| --pdf-options '{"format":"A4","margin":{"top":"20mm","right":"20mm","bottom":"20mm","left":"20mm"}}' \ | |
| < /dev/null \ | |
| >/dev/null | |
| else | |
| md-to-pdf "$src" \ | |
| --pdf-options '{"format":"A4","margin":{"top":"20mm","right":"20mm","bottom":"20mm","left":"20mm"}}' \ | |
| < /dev/null \ | |
| >/dev/null | |
| fi | |
| if [ ! -f "$temp_pdf" ]; then | |
| echo "PDF not created: $src" >&2 | |
| exit 1 | |
| fi | |
| mv "$temp_pdf" "$dst" | |
| done < <(find "$INPUT_DIR" -type f -name '*.md' -print0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment