Skip to content
Visibility internal Owner _ Approver _ Created _ Updated _

md2pdf.sh



FieldValue
TypeShell
SourceForge/Skills/Markdown_To_PDF/scripts/md2pdf.sh
ParentForge
GitHubForge/Skills/Markdown_To_PDF/scripts/md2pdf.sh

This page is auto-generated. Edit the source to change the content.



#!/usr/bin/env bash
set -euo pipefail

###############################################################################
# md2pdf.sh — Convert any .md file to .pdf using the Uvilo theme
#
# Usage:
#   bash md2pdf.sh /path/to/document.md
#
# Output:
#   /path/to/document.pdf (same folder as source)
###############################################################################

# --- Constants ---------------------------------------------------------------
SKILL_DIR="/workspace/erik/uvilo-os/Forge/Skills/Markdown_To_PDF"
CSS_FILE="${SKILL_DIR}/Assets/uvilo.css"
FONTS_DIR="/workspace/erik/uvilo-os/Uvilo/Assets/Fonts"

# --- Arguments ---------------------------------------------------------------
if [[ $# -lt 1 ]]; then
  echo "❌ Usage: bash md2pdf.sh <input.md>"
  exit 1
fi

INPUT_FILE="$1"

if [[ ! -f "${INPUT_FILE}" ]]; then
  echo "❌ File not found: ${INPUT_FILE}"
  exit 1
fi

if [[ "${INPUT_FILE}" != *.md ]]; then
  echo "❌ Input must be a .md file: ${INPUT_FILE}"
  exit 1
fi

# --- Derive output path ------------------------------------------------------
# Output PDF goes in the same directory as the source .md
INPUT_DIR="$(cd "$(dirname "${INPUT_FILE}")" && pwd)"
INPUT_BASENAME="$(basename "${INPUT_FILE}" .md)"
OUTPUT_FILE="${INPUT_DIR}/${INPUT_BASENAME}.pdf"

echo "📄 Input:  ${INPUT_FILE}"
echo "📄 Output: ${OUTPUT_FILE}"

# --- Validation --------------------------------------------------------------
if ! command -v pandoc &>/dev/null; then
  echo "❌ pandoc not found. Install with: pip install pypandoc-binary"
  exit 1
fi

if ! command -v weasyprint &>/dev/null; then
  echo "❌ weasyprint not found. Install with: pip install weasyprint"
  exit 1
fi

if [[ ! -f "${CSS_FILE}" ]]; then
  echo "❌ CSS theme not found: ${CSS_FILE}"
  exit 1
fi

if [[ ! -d "${FONTS_DIR}" ]]; then
  echo "❌ Fonts directory not found: ${FONTS_DIR}"
  exit 1
fi

# --- Generate PDF ------------------------------------------------------------
echo "⚙️  Generating PDF..."

# Use absolute path for input since we're changing working directory
ABS_INPUT="$(cd "$(dirname "${INPUT_FILE}")" && pwd)/$(basename "${INPUT_FILE}")"
ABS_OUTPUT="$(cd "$(dirname "${OUTPUT_FILE}")" && pwd)/$(basename "${OUTPUT_FILE}")"

# Run from /tmp so pandoc can write temp files (workspace root may be read-only for temp)
pushd /tmp > /dev/null
pandoc --pdf-engine weasyprint --css "${CSS_FILE}" \
  -o "${ABS_OUTPUT}" "${ABS_INPUT}"
popd > /dev/null

echo "✅ Done: ${OUTPUT_FILE}"