BASH Post Services

Viewing: 1753193030_imgwpdf.sh

Try wget https://bash.commongrounds.cc/uploads/1753193030_imgwpdf.sh from the console

Raw File Link

#!/bin/bash

# Check if required arguments are provided
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 input_pdf image_file output_pdf"
    exit 1
fi

INPUT_PDF="$1"
IMAGE_FILE="$2"
OUTPUT_PDF="$3"

# Check if input files exist
if [ ! -f "$INPUT_PDF" ]; then
    echo "Error: Input PDF file '$INPUT_PDF' does not exist"
    exit 1
fi

if [ ! -f "$IMAGE_FILE" ]; then
    echo "Error: Image file '$IMAGE_FILE' does not exist"
    exit 1
fi

# Check if required tools are installed
if ! command -v pdfinfo &> /dev/null; then
    echo "Error: pdfinfo (poppler-utils) is not installed"
    exit 1
fi

if ! command -v convert &> /dev/null; then
    echo "Error: convert (ImageMagick) is not installed"
    exit 1
fi

if ! command -v pdftk &> /dev/null; then
    echo "Error: pdftk is not installed"
    exit 1
fi

# Get PDF dimensions
PAGE_SIZE=$(pdfinfo "$INPUT_PDF" | grep "Page size" | awk '{print $3 "x" $5}')

# Convert image to PDF with matching dimensions
TEMP_IMAGE_PDF="temp_image_$(date +%s).pdf"
convert "$IMAGE_FILE" -resize "$PAGE_SIZE" -gravity center -extent "$PAGE_SIZE" "$TEMP_IMAGE_PDF"

# Check if image conversion was successful
if [ $? -ne 0 ]; then
    echo "Error: Failed to convert image to PDF"
    rm -f "$TEMP_IMAGE_PDF"
    exit 1
fi

# Combine original PDF with image PDF
pdftk "$INPUT_PDF" "$TEMP_IMAGE_PDF" cat output "$OUTPUT_PDF"

# Check if PDF combination was successful
if [ $? -ne 0 ]; then
    echo "Error: Failed to combine PDF files"
    rm -f "$TEMP_IMAGE_PDF"
    exit 1
fi

# Clean up temporary file
rm -f "$TEMP_IMAGE_PDF"

echo "Successfully created $OUTPUT_PDF"
BASH to Home