|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Convert a PDF to a self-contained HTML presentation. |
| 3 | +
|
| 4 | +Each page is rendered as a PNG image (via pdftoppm) and base64-embedded |
| 5 | +into a single HTML file with slide navigation (arrows, swipe, click). |
| 6 | +
|
| 7 | +Requirements: poppler-utils (pdftoppm) |
| 8 | +Usage: python3 convert-pdf.py input.pdf [output.html] |
| 9 | +""" |
| 10 | + |
| 11 | +import base64 |
| 12 | +import glob |
| 13 | +import os |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | + |
| 20 | +def convert(pdf_path: str, output_path: str | None = None, dpi: int = 150): |
| 21 | + pdf_path = str(Path(pdf_path).resolve()) |
| 22 | + if not Path(pdf_path).exists(): |
| 23 | + print(f"Error: {pdf_path} not found") |
| 24 | + sys.exit(1) |
| 25 | + |
| 26 | + # Check for pdftoppm |
| 27 | + if subprocess.run(["which", "pdftoppm"], capture_output=True).returncode != 0: |
| 28 | + print("Error: pdftoppm not found. Install poppler-utils:") |
| 29 | + print(" apt install poppler-utils # Debian/Ubuntu") |
| 30 | + print(" brew install poppler # macOS") |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 34 | + prefix = os.path.join(tmpdir, "page") |
| 35 | + result = subprocess.run( |
| 36 | + ["pdftoppm", "-png", "-r", str(dpi), pdf_path, prefix], |
| 37 | + capture_output=True, text=True |
| 38 | + ) |
| 39 | + if result.returncode != 0: |
| 40 | + print(f"Error converting PDF: {result.stderr}") |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | + pages = sorted(glob.glob(f"{prefix}-*.png")) |
| 44 | + if not pages: |
| 45 | + print("Error: No pages rendered from PDF") |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + slides_html = [] |
| 49 | + for i, page_path in enumerate(pages, 1): |
| 50 | + with open(page_path, "rb") as f: |
| 51 | + b64 = base64.b64encode(f.read()).decode() |
| 52 | + slides_html.append( |
| 53 | + f'<section class="slide">' |
| 54 | + f'<div class="slide-inner">' |
| 55 | + f'<img src="data:image/png;base64,{b64}" alt="Page {i}">' |
| 56 | + f'</div></section>' |
| 57 | + ) |
| 58 | + |
| 59 | + # Try to extract title from filename |
| 60 | + title = Path(pdf_path).stem.replace("-", " ").replace("_", " ") |
| 61 | + |
| 62 | + html = f'''<!DOCTYPE html> |
| 63 | +<html lang="en"> |
| 64 | +<head> |
| 65 | +<meta charset="UTF-8"> |
| 66 | +<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 67 | +<title>{title}</title> |
| 68 | +<style> |
| 69 | +* {{ margin: 0; padding: 0; box-sizing: border-box; }} |
| 70 | +html, body {{ height: 100%; overflow: hidden; background: #000; }} |
| 71 | +.slide {{ width: 100vw; height: 100vh; display: none; align-items: center; justify-content: center; }} |
| 72 | +.slide.active {{ display: flex; }} |
| 73 | +.slide-inner {{ display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; }} |
| 74 | +.slide-inner img {{ max-width: 100%; max-height: 100%; object-fit: contain; }} |
| 75 | +.progress {{ position: fixed; bottom: 0; left: 0; height: 4px; background: #0366d6; transition: width 0.3s; z-index: 100; }} |
| 76 | +.counter {{ position: fixed; bottom: 12px; right: 20px; font-size: 14px; color: rgba(255,255,255,0.4); z-index: 100; }} |
| 77 | +</style> |
| 78 | +</head> |
| 79 | +<body> |
| 80 | +{chr(10).join(slides_html)} |
| 81 | +<div class="progress" id="progress"></div> |
| 82 | +<div class="counter" id="counter"></div> |
| 83 | +<script> |
| 84 | +const slides = document.querySelectorAll('.slide'); |
| 85 | +let current = 0; |
| 86 | +function show(n) {{ |
| 87 | + slides.forEach(s => s.classList.remove('active')); |
| 88 | + current = Math.max(0, Math.min(n, slides.length - 1)); |
| 89 | + slides[current].classList.add('active'); |
| 90 | + document.getElementById('progress').style.width = ((current + 1) / slides.length * 100) + '%'; |
| 91 | + document.getElementById('counter').textContent = (current + 1) + ' / ' + slides.length; |
| 92 | +}} |
| 93 | +document.addEventListener('keydown', e => {{ |
| 94 | + if (e.key === 'ArrowRight' || e.key === ' ') {{ e.preventDefault(); show(current + 1); }} |
| 95 | + if (e.key === 'ArrowLeft') {{ e.preventDefault(); show(current - 1); }} |
| 96 | +}}); |
| 97 | +let touchStartX = 0; |
| 98 | +document.addEventListener('touchstart', e => {{ touchStartX = e.changedTouches[0].screenX; }}); |
| 99 | +document.addEventListener('touchend', e => {{ |
| 100 | + const diff = e.changedTouches[0].screenX - touchStartX; |
| 101 | + if (Math.abs(diff) > 50) {{ diff > 0 ? show(current - 1) : show(current + 1); }} |
| 102 | +}}); |
| 103 | +document.addEventListener('click', e => {{ |
| 104 | + if (e.clientX > window.innerWidth / 2) show(current + 1); |
| 105 | + else show(current - 1); |
| 106 | +}}); |
| 107 | +show(0); |
| 108 | +</script> |
| 109 | +</body></html>''' |
| 110 | + |
| 111 | + output = output_path or str(Path(pdf_path).with_suffix('.html')) |
| 112 | + Path(output).write_text(html, encoding='utf-8') |
| 113 | + print(f"Converted to: {output}") |
| 114 | + print(f"Pages: {len(slides_html)}") |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + if len(sys.argv) < 2: |
| 119 | + print("Usage: convert-pdf.py <file.pdf> [output.html]") |
| 120 | + sys.exit(1) |
| 121 | + convert(sys.argv[1], sys.argv[2] if len(sys.argv) > 2 else None) |
0 commit comments