|
1 | | -#!/bin/bash -x |
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# pdftogif.sh |
| 4 | +# Convert a multi-page PDF into: |
| 5 | +# - an animated GIF (all pages) |
| 6 | +# - a PNG preview (first page) |
| 7 | +# |
| 8 | +# Requires: ghostscript (gs), ImageMagick (convert) |
| 9 | +# |
| 10 | +# Example: |
| 11 | +# ./pdftogif.sh slides.pdf MyPresentation |
| 12 | +# |
2 | 13 |
|
3 | | -if [[ $# -lt 2 ]] ; then |
4 | | - echo 'Please pass the path to the pdf to convert and the name of the output file.' |
5 | | - echo 'Run something like cd images/pubpic; ../../_scripts/pdftogif.sh ../../assets/presentations/B_Kundu-PyHEP23_Cppyy_CppInterOp.pdf BKPyHEPDev2023' |
6 | | - exit 1 |
7 | | -fi |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +# ----------------------- |
| 17 | +# configuration defaults |
| 18 | +# ----------------------- |
| 19 | +DPI=48 |
| 20 | +GIF_DELAY=100 # centiseconds (100 = 1s per frame) |
| 21 | + |
| 22 | +# ----------------------- |
| 23 | +# helpers |
| 24 | +# ----------------------- |
| 25 | +usage() { |
| 26 | + cat <<EOF |
| 27 | +Usage: |
| 28 | + $(basename "$0") <input.pdf> <output_name> |
| 29 | +
|
| 30 | +Creates: |
| 31 | + <output_name>.gif Animated GIF of all pages |
| 32 | + <output_name>.png PNG preview of the first page |
| 33 | +
|
| 34 | +Example: |
| 35 | + cd images/pubpic |
| 36 | + ../../_scripts/pdftogif.sh \\ |
| 37 | + ../../assets/presentations/B_Kundu-PyHEP23_Cppyy_CppInterOp.pdf \\ |
| 38 | + BKPyHEPDev2023 |
| 39 | +
|
| 40 | +Requirements: |
| 41 | + - ghostscript (gs) |
| 42 | + - ImageMagick (convert) |
| 43 | +EOF |
| 44 | +} |
8 | 45 |
|
9 | | -if ! [ -x "$(command -v gs)" ]; then |
10 | | - echo 'Error: gs is not installed. This script depends on it' >&2 |
| 46 | +die() { |
| 47 | + echo "Error: $*" >&2 |
11 | 48 | exit 1 |
12 | | -fi |
| 49 | +} |
13 | 50 |
|
14 | | -if ! [ -x "$(command -v convert2)" ]; then |
15 | | - echo 'Error: convert is not installed. This script depends on it' >&2 |
| 51 | +check_cmd() { |
| 52 | + command -v "$1" >/dev/null 2>&1 || die "'$1' is not installed or not in PATH" |
| 53 | +} |
| 54 | + |
| 55 | +# ----------------------- |
| 56 | +# argument parsing |
| 57 | +# ----------------------- |
| 58 | +if [[ $# -ne 2 ]]; then |
| 59 | + usage |
16 | 60 | exit 1 |
17 | 61 | fi |
18 | 62 |
|
19 | | -SLIDES_PDF=$1 |
20 | | -PRES_ID=$2 |
21 | | -WORK_DIR=$(mktemp -d) |
| 63 | +SLIDES_PDF="$1" |
| 64 | +PRES_ID="$2" |
| 65 | + |
| 66 | +[[ -f "$SLIDES_PDF" ]] || die "Input PDF not found: $SLIDES_PDF" |
22 | 67 |
|
23 | | -gs -dSAFER -dQUIET -dNOPLATFONTS -dNOPAUSE -dBATCH \ |
24 | | - -sOutputFile="$WORK_DIR/%d.png" \ |
| 68 | +# ----------------------- |
| 69 | +# dependency checks |
| 70 | +# ----------------------- |
| 71 | +check_cmd gs |
| 72 | +check_cmd convert |
| 73 | + |
| 74 | +# ----------------------- |
| 75 | +# temp working directory |
| 76 | +# ----------------------- |
| 77 | +WORK_DIR="$(mktemp -d "${TMPDIR:-/tmp}/pdftogif.XXXXXX")" |
| 78 | + |
| 79 | +cleanup() { |
| 80 | + rm -rf "$WORK_DIR" |
| 81 | +} |
| 82 | +trap cleanup EXIT |
| 83 | + |
| 84 | +# ----------------------- |
| 85 | +# PDF → PNGs |
| 86 | +# ----------------------- |
| 87 | +echo "Rendering PDF pages to PNGs…" |
| 88 | + |
| 89 | +gs -dSAFER -dQUIET -dNOPAUSE -dBATCH \ |
25 | 90 | -sDEVICE=pngalpha \ |
26 | | - -r48 \ |
| 91 | + -sOutputFile="$WORK_DIR/%03d.png" \ |
| 92 | + -r"$DPI" \ |
27 | 93 | -dTextAlphaBits=4 \ |
28 | 94 | -dGraphicsAlphaBits=4 \ |
29 | 95 | -dUseCIEColor \ |
30 | 96 | -dUseTrimBox \ |
31 | | - $SLIDES_PDF |
| 97 | + "$SLIDES_PDF" |
32 | 98 |
|
33 | | -convert -delay 100 $(ls $WORK_DIR/*.png | sort -V) $PRES_ID.gif |
34 | | -mv $WORK_DIR/1.png $PRES_ID.png |
| 99 | +# ----------------------- |
| 100 | +# PNGs → GIF |
| 101 | +# ----------------------- |
| 102 | +echo "Creating animated GIF: ${PRES_ID}.gif" |
35 | 103 |
|
36 | | -# deletes the temp directory |
37 | | -function cleanup { |
38 | | - rm -rf "$WORK_DIR" |
39 | | - echo "Deleted temp working directory $WORK_DIR" |
40 | | -} |
| 104 | +convert -delay "$GIF_DELAY" \ |
| 105 | + "$WORK_DIR"/*.png \ |
| 106 | + "${PRES_ID}.gif" |
41 | 107 |
|
42 | | -# register the cleanup function to be called on the EXIT signal |
43 | | -trap cleanup EXIT |
| 108 | +# ----------------------- |
| 109 | +# first page preview |
| 110 | +# ----------------------- |
| 111 | +cp "$WORK_DIR/001.png" "${PRES_ID}.png" |
| 112 | + |
| 113 | +# ----------------------- |
| 114 | +# done |
| 115 | +# ----------------------- |
| 116 | +echo "Done." |
| 117 | +echo " → ${PRES_ID}.gif" |
| 118 | +echo " → ${PRES_ID}.png" |
0 commit comments