Skip to content

Instantly share code, notes, and snippets.

@yonixw
Created January 27, 2025 02:04
Show Gist options
  • Save yonixw/8ff759e7fd9f2db6532ead6672a4c729 to your computer and use it in GitHub Desktop.
Save yonixw/8ff759e7fd9f2db6532ead6672a4c729 to your computer and use it in GitHub Desktop.
Unpack PDFs + Create TOC
Create BLANK.png with white background and some gray "filler / fill page" text
A4 I had had 3308 x 4678 pixels
#!/bin/bash
# Install necessary packages
sudo apt install -y imagemagick
# Font list
convert -list font | grep .ttf
# Get the list of filenames from a file (e.g., filelist.txt)
# Assuming each line in the file contains one filename
while IFS= read -r filename; do
# Create an image with the filename as text
convert BLANK.png -gravity center \
-font /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf -fill black \
-size 2646x2339 -gravity Center caption:"$filename" \
-gravity Center -geometry 2646x2339+0+0 -composite \
"${filename}_TOC01.png"
cp BLANK.png "${filename}_TOC02.png"
done < filelist.txt
Some title or Some Filename
End with empty row and only \n. Not \r
#!/bin/bash
# Install necessary packages
sudo apt install -y poppler-utils imagemagick
# Loop through all PDF files in the current directory
find . -maxdepth 1 -name "*.pdf" -print0 | while IFS= read -r -d $'\0' pdf; do
# Get the base filename without extension
filename=$(basename "$pdf" .pdf)
# Convert each page of the PDF to a PNG image with 400 DPI
pdftoppm -png -r 400 "$pdf" "${filename}_%03d"
# Convert PNG images to JPG if desired
# mogrify -format jpg "${filename}_*.png"
rm "$pdf"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment