flowchart TD
A[Read target directory] --> B[Discover md files]
B --> C[For each markdown file]
C --> D[Invoke converter toolchain]
D --> E{Conversion success}
E -- Yes --> F[Write PDF output]
E -- No --> G[Log failure and continue exit]
F --> H{More files}
G --> H
H -- Yes --> C
H -- No --> I[Summary and finish]
sequenceDiagram
participant User
participant Script as Shell Script
participant FS as File System
participant Conv as PDF Converter
User->>Script: run convert md to pdf sh
Script->>FS: enumerate markdown files
loop for each file
Script->>Conv: convert md pdf
Conv-->>Script: success failure
Script->>FS: write output log
end
#!/bin/bash# convert_md_to_pdf.sh# Convert all Markdown files to PDF using pandoc with xelatex# Usage: ./convert_md_to_pdf.sh [directory]set-euopipefail
# ConfigurationDIRECTORY="${1:-.}"PDF_ENGINE="xelatex"MAIN_FONT="NanumGothic"MAX_JOBS=4# Colors for outputRED='\033[0;31m'GREEN='\033[0;32m'YELLOW='\033[1;33m'NC='\033[0m'# No Color# Function to convert a single fileconvert_file(){localinput_file="$1"localoutput_file="${input_file%.md}.pdf"echo-e"${YELLOW}Converting: $input_file -> $output_file${NC}"ifpandoc"$input_file"\-o"$output_file"\--pdf-engine="$PDF_ENGINE"\-Vmainfont="$MAIN_FONT"2>/dev/null;thenecho-e"${GREEN}Done: $input_file${NC}"return0elseecho-e"${RED}Failed: $input_file${NC}">&2return1fi}export-fconvert_file
exportPDF_ENGINEMAIN_FONTREDGREENYELLOWNC
# Check if pandoc is installedif!command-vpandoc&>/dev/null;thenecho-e"${RED}Error: pandoc is not installed${NC}">&2exit1fi# Check if xelatex is installedif!command-vxelatex&>/dev/null;thenecho-e"${RED}Error: xelatex is not installed${NC}">&2exit1fi# Count total filestotal=$(find"$DIRECTORY"-typef-name"*.md"|wc-l)if["$total"-eq0];thenecho-e"${YELLOW}No .md files found in $DIRECTORY${NC}"exit0fiecho"Found $total markdown files. Converting with $MAX_JOBS parallel jobs..."echo""# Export to use in subshellexporttotal
# Find and convert files in parallel using GNU parallel or xargsdry_run(){whileIFS=read-rfile;doconvert_file"$file"done}ifcommand-vparallel&>/dev/null;then# Use GNU parallel for better job controlfind"$DIRECTORY"-typef-name"*.md"-print0|\parallel-0-j"$MAX_JOBS"convert_file
else# Fallback to xargs with proper syntax (no -I with -P)# Use a wrapper script approachfind"$DIRECTORY"-typef-name"*.md"-print0|\xargs-0-P"$MAX_JOBS"-n1bash-c'convert_file "$@"'_
fiecho""echo-e"${GREEN}Conversion complete!${NC}"