I want to cut out one or more pages from a PDF and save them as PNGs. Ideally I'll use ghostscript.
Write me a bash script that takes the path of the PDF and the page number(s), and writes the PNG(s) prefixed by the original PDF name, suffixed by the page number.
Ask clarifying questions if anything is unclear.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To ensure I fully understand your requirements, here are a few clarifying questions:
Input Format for Page Numbers: Should the script accept a single page number (e.g., 5) or a range of pages (e.g., 1-3 or 1,3,5)? If ranges are allowed, how should they be formatted?
Output Directory: Should the PNG files be saved in the same directory as the input PDF, or do you want an option to specify a different output directory?
Resolution/DPI: What resolution (DPI) should the PNGs be rendered at? Ghostscript allows specifying this, and a common default is 300 DPI.
File Naming Convention: For example, if the input PDF is named example.pdf and you want to extract page 5, should the output file be named example_page_5.png?
Error Handling: Should the script handle cases where the input PDF does not exist, the specified page numbers are invalid, or Ghostscript is not installed?
Once these details are clarified, I can proceed with writing the script. Below is a draft of the script based on the assumptions that:
The script accepts a single page number or a comma-separated list of page numbers.
The output PNGs are saved in the same directory as the input PDF.
The resolution is set to 300 DPI by default.
The naming convention is <PDF_name>_page_<page_number>.png.
Let me know if any of these assumptions need adjustment.
Draft Bash Script
bash
#!/bin/bash
# Check if Ghostscript is installed
if ! command -v gs &> /dev/null; then
echo "Ghostscript is not installed. Please install it and try again."
exit 1
fi
# Validate input arguments
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <path_to_pdf> <page_numbers>"
echo "Example: $0 example.pdf 1,3,5"
exit 1
fi
PDF_PATH="$1"
PAGE_NUMBERS="$2"
# Check if the PDF file exists
if [ ! -f "$PDF_PATH" ]; then
echo "Error: File '$PDF_PATH' not found."
exit 1
fi
# Extract the base name of the PDF (without extension)
BASE_NAME=$(basename "$PDF_PATH" .pdf)
OUTPUT_DIR=$(dirname "$PDF_PATH")
# Loop through the page numbers and process each one
IFS=',' read -ra PAGES <<< "$PAGE_NUMBERS"
for PAGE in "${PAGES[@]}"; do
# Validate that the page number is numeric
if ! [[ "$PAGE" =~ ^[0-9]+$ ]]; then
echo "Error: Invalid page number '$PAGE'. Page numbers must be positive integers."
continue
fi
# Construct the output file name
OUTPUT_FILE="$OUTPUT_DIR/${BASE_NAME}_page_${PAGE}.png"
# Use Ghostscript to convert the specified page to PNG
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r300 \
-dFirstPage="$PAGE" -dLastPage="$PAGE" \
-sOutputFile="$OUTPUT_FILE" "$PDF_PATH" 2>/dev/null
if [ $? -eq 0 ]; then
echo "Page $PAGE saved as $OUTPUT_FILE"
else
echo "Error: Failed to process page $PAGE."
fi
done
Explanation of the Script
Ghostscript Installation Check: The script first checks if Ghostscript is installed using the command -v command. If not, it exits with an error message.
Input Validation: It ensures that the user provides at least two arguments: the path to the PDF and the page numbers.
File Existence Check: It verifies that the specified PDF file exists.
Page Number Parsing: The script splits the PAGE_NUMBERS argument by commas into an array and processes each page number individually.
Validation of Page Numbers: Each page number is checked to ensure it is a positive integer.
Ghostscript Command: Ghostscript is invoked with the following options:
-dSAFER: Enables safer mode to prevent malicious code execution.
-dBATCH: Ensures Ghostscript exits after processing.
-dNOPAUSE: Prevents Ghostscript from pausing between pages.
-sDEVICE=png16m: Specifies the output format as PNG with 24-bit color.
-r300: Sets the resolution to 300 DPI.
-dFirstPage and -dLastPage: Specify the range of pages to process (in this case, a single page).
-sOutputFile: Specifies the output file name.
Error Handling: If Ghostscript fails to process a page, an error message is displayed.
Example Usage
bash
./extract_pages.sh example.pdf 1,3,5
This will generate three files:
example_page_1.png
example_page_3.png
example_page_5.png
Please confirm the clarifying questions or provide additional details so I can refine the script further if needed.