---
name: pdf-engine-fallback-b02a6ea01706
description: Retry PDF generation with alternative engines when pandoc fails
---

# PDF Engine Fallback

This skill provides a diagnostic-retry workflow for PDF generation when Pandoc's default PDF engine fails. It systematically checks available engines and retries with alternatives optimized for different content types (e.g., xelatex for Unicode).

## When to Use

- Pandoc PDF generation fails with errors
- Document contains non-ASCII characters (Unicode, CJK, etc.)
- Need robust PDF generation across different system configurations
- Working in CI/CD or automated document pipelines

## Steps

### 1. Attempt Initial PDF Generation

First, try generating with pandoc's default engine:

```bash
pandoc input.md -o output.pdf
```

### 2. Diagnose on Failure

If the initial attempt fails, check which PDF engines are available:

```bash
# Check for pdflatex
which pdflatex && echo "pdflatex: available" || echo "pdflatex: not found"

# Check for xelatex (better Unicode support)
which xelatex && echo "xelatex: available" || echo "xelatex: not found"

# Check for wkhtmltopdf (HTML-based rendering)
which wkhtmltopdf && echo "wkhtmltopdf: available" || echo "wkhtmltopdf: not found"
```

### 3. Retry with Alternative Engine

Based on available engines and document content, retry with an appropriate engine:

```bash
# For documents with Unicode/special characters - prefer xelatex
pandoc input.md -o output.pdf --pdf-engine=xelatex

# Alternative: use pdflatex for simple Latin documents
pandoc input.md -o output.pdf --pdf-engine=pdflatex

# For HTML-heavy content or complex layouts
pandoc input.md -o output.pdf --pdf-engine=wkhtmltopdf
```

### 4. Handle Missing Engines

If no PDF engines are available, install one:

```bash
# Ubuntu/Debian - install texlive with xelatex
sudo apt-get install texlive-xetex

# Or install minimal teTeX
sudo apt-get install texlive-latex-base texlive-latex-extra

# macOS with Homebrew
brew install basictex
```

## Complete Fallback Script

```bash
#!/bin/bash
# pdf-fallback.sh - Robust PDF generation with engine fallback

INPUT_FILE="${1:-input.md}"
OUTPUT_FILE="${2:-output.pdf}"

echo "Attempting PDF generation: $INPUT_FILE -> $OUTPUT_FILE"

# Try default engine first
if pandoc "$INPUT_FILE" -o "$OUTPUT_FILE" 2>/dev/null; then
    echo "SUCCESS: PDF generated with default engine"
    exit 0
fi

echo "Default engine failed. Checking available engines..."

# Check and try engines in priority order
if command -v xelatex &>/dev/null; then
    echo "Retrying with xelatex (Unicode support)..."
    if pandoc "$INPUT_FILE" -o "$OUTPUT_FILE" --pdf-engine=xelatex; then
        echo "SUCCESS: PDF generated with xelatex"
        exit 0
    fi
fi

if command -v pdflatex &>/dev/null; then
    echo "Retrying with pdflatex..."
    if pandoc "$INPUT_FILE" -o "$OUTPUT_FILE" --pdf-engine=pdflatex; then
        echo "SUCCESS: PDF generated with pdflatex"
        exit 0
    fi
fi

if command -v wkhtmltopdf &>/dev/null; then
    echo "Retrying with wkhtmltopdf..."
    if pandoc "$INPUT_FILE" -o "$OUTPUT_FILE" --pdf-engine=wkhtmltopdf; then
        echo "SUCCESS: PDF generated with wkhtmltopdf"
        exit 0
    fi
fi

echo "FAILURE: No available PDF engine succeeded"
echo "Install a PDF engine (texlive-xetex recommended) and retry"
exit 1
```

## Usage

```bash
# Make executable
chmod +x pdf-fallback.sh

# Run with defaults (input.md -> output.pdf)
./pdf-fallback.sh

# Run with custom files
./pdf-fallback.sh report.md report.pdf
```

## Engine Selection Guide

| Engine | Best For | Unicode | Speed |
|--------|----------|---------|-------|
| xelatex | Non-Latin scripts, modern fonts | Excellent | Moderate |
| pdflatex | Simple Latin documents | Limited | Fast |
| wkhtmltopdf | HTML/CSS styling | Good | Fast |

## Troubleshooting

- **Font errors**: Install required fonts or use xelatex with system fonts
- **Package errors**: Install missing LaTeX packages via `tlmgr`
- **Large documents**: Try pdflatex for speed, xelatex for compatibility
- **Citation issues**: Ensure pandoc-citeproc or citeproc is installed

---
Source attribution: AG064
Original project: https://github.com/AG064/OpenSpace/blob/ff671ef61ed6587c49bd54c81cdc751c0b99e363/gdpval_bench/skills/pdf-engine-fallback/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2026 ✨Data Intelligence Lab@HKU✨

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: AG064
Original project: https://github.com/AG064/OpenSpace/blob/ff671ef61ed6587c49bd54c81cdc751c0b99e363/gdpval_bench/skills/pdf-engine-fallback/SKILL.md
Attribution basis: Source repository record

Fields of work: Content & Media, Development & Engineering
Roles: Backend Developer, DevOps / SRE, Technical Writer
Specialties: Backend
Tools: Pandoc, Texlive, Wkhtmltopdf
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: f85df023b9fd1a6c009430c820101a0bf686e8de7fee16881a5cb4efeb5e2efd
