Skip to content

Instantly share code, notes, and snippets.

@nurrony
Last active March 18, 2016 06:44
Show Gist options
  • Select an option

  • Save nurrony/eeb86f2c4c66cc558f5d to your computer and use it in GitHub Desktop.

Select an option

Save nurrony/eeb86f2c4c66cc558f5d to your computer and use it in GitHub Desktop.
Convert MS office doc[x], xls[x], ppt[x] and Openoffice Files to JPEG image using GhostScript and Headless Libreoffice
#!/usr/bin/env bash
read -p 'Document filename (with path): ' DOC_NAME_AND_PATH
read -p 'Image Destination Path (directory): ' DOC_DESTINATION
#Variables
DOC_NAME_AND_EXT=$(basename $DOC_NAME_AND_PATH)
DOC_EXT=${DOC_NAME_AND_EXT##*.}
DOC_NAME=${DOC_NAME_AND_EXT%.*}
PDF_FILENAME="$DOC_NAME.pdf"
PDF_FILENAME_AND_PATH="$DOC_DESTINATION/$DOC_NAME"
#check if destination directory
if [ ! -d "$DOC_DESTINATION" ]; then
mkdir -p $PDF_FILENAME_AND_PATH;
fi
#convert to pdf
echo "Converting document from $DOC_EXT to PDF..."
PDF_GEN_START=$(date +%s)
libreoffice --invisible --headless --convert-to pdf:writer_pdf_Export $DOC_NAME_AND_PATH --outdir "$PDF_FILENAME_AND_PATH/" >/dev/null 2>&1
PDF_GEN_END=$(date +%s)
PDF_CONV_TIME=$(( $PDF_GEN_END - $PDF_GEN_START ))
echo "Document converted to PDF successfully in $PDF_CONV_TIME second(s)"
PDF_PAGE_NUMBER=`gs -q -dNODISPLAY -c "($PDF_FILENAME_AND_PATH/$PDF_FILENAME) (r) file runpdfbegin pdfpagecount = quit"`
read -p 'Start page number(default = 1): ' PDF_OFFSET
read -p "Last page number (default = $PDF_PAGE_NUMBER): " PDF_LIMIT
if [ -z "$PDF_OFFSET" ] || [ "$PDF_OFFSET" -eq 0 ]; then
PDF_OFFSET=1
fi
if [ -z "$PDF_LIMIT" ] || [ "$PDF_LIMIT" -eq 0 ] || [ "$PDF_OFFSET" -gt "$PDF_LIMIT" ]; then
PDF_LIMIT=$PDF_PAGE_NUMBER
fi
#gs -dNumRenderingThreads=4 -dNOPAUSE -sDEVICE=pngalpha -dFirstPage=1 -dLastPage=44-sOutputFile=../test/image%d.png -r300 -q ../TraceProvisioningDeploymentLogManagementandLoadTesting.pdf -c quit
IMG_GEN_START=$(date +%s)
gs -q -sDEVICE=jpeg -dBATCH -dNOPAUSE -dFirstPage=$PDF_OFFSET -dLastPage=$PDF_LIMIT -dUseCropBox -r150x150 -sOutputFile="$PDF_FILENAME_AND_PATH/$DOC_NAME-%03d.jpeg" "$PDF_FILENAME_AND_PATH/$PDF_FILENAME" >/dev/null 2>&1
IMG_GEN_END=$(date +%s)
DIFF=$(( $IMG_GEN_END - $IMG_GEN_START ))
echo "Converted PDF to Image successfully in $DIFF second(s)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment