Showing posts with label gs. Show all posts
Showing posts with label gs. Show all posts

Wednesday, June 6, 2012

[Bash] ps2png: convert ps to png format with high quality

previous solution using ImageMagicK works too, but may not produce the high quality expected.

#!/bin/bash
# convert a single-page ps file into png format using gs
# usage:
#            ps2png psfile.ps
# Note:
#           1: need gs installed
#           2: same name is used for the png file
#           http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '2,4p' `which ps2png`
   exit
else
  for psname in $*
  do
     if [ -e $psname ]; then
        pngname=`echo ${psname%%.*}`
          gs -r300 -dTextAlphaBits=4 -sDEVICE=png16m -sOutputFile=${pngname}.png -dBATCH -dNOPAUSE ${psname}
     else
        echo "${psname} does not exist!"
     fi
  done
fi

Sunday, March 18, 2012

[Bash] extract pages from a pdf file using gs

#!/bin/bash
# extract pages from a big pdf file using gs
# Usage:
#           pdfselect.sh pdffile startPageNumber endPageNumber
#           http://scriptdemo.blogspot.ca

function getPDFPages()
{
   if [ -e $1 ]; then
      pdfinfo $1 | grep "Pages" | awk '{print $2}'
   else
      echo 0;
   fi
}

if [ $# -lt 2 ]; then
   sed -n '3,4p' `which pdfselect.sh`
   exit
fi

oriPdf=$1
[ ! -e ${oriPdf} ] && echo "${oriPdf} does not exist! " && exit

numPage=`getPDFPages ${oriPdf}`
[ ${numPage} -eq 0 ] && "ZERO page is found in ${oriPdf}" && exit

sPage=$2
[ ${sPage} -gt ${numPage} ] && ${sPage}=${numPage}
if [ $# -eq 2 ]; then
   ePage=${sPage}
else
   ePage=$3;
fi
[ ${ePage} -gt ${numPage} ] && ${ePage}=${numPage}

if [ $# -lt 4 ]; then
   outPdf="p${sPage}_${ePage}_${oriPdf}"
else
   outPdf=$4
fi

eval "gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=${sPage} -dLastPage=${ePage} -sOutputFile=${outPdf} ${oriPdf}"

Monday, March 21, 2011

Reduce pdf file size using gs

#!/bin/bash
# From http://scriptdemo.blogspot.com
# to reduce pdf file size using gs with different optimization level
# Usage:
# pdfsmaller.sh in.pdf out.pdf [opt]
# option:
#            screen    (72dpi)
#            ebook     (150dpi)
#            printer    (300dpi)
#            prepress (color preserving, 300dpi)
# e.g.,
#            pdfsmaller.sh Big.pdf smaller.pdf ebook # save output to smaller.pdf
# or        pdfsmaller.sh Big.pdf ebook                  # overwrite original Big.pdf
# ref: http://milan.kupcevic.net/ghostscript-ps-pdf

isOverWrite=0
if [ $# -eq 1 ]; then
   inFile=$1
   outFile=smaller_$1
   opt="prepress"
   isOverWrite=1
elif [ $# -eq 2 ]; then
   inFile=$1
   nf=`echo $2 | awk -F. '{print NF}'`
   if [ ${nf} -eq 2 ]; then
      outFile=$2
      opt="prepress"
   else
      outFile=smaller_$1
      opt=$2
      isOverWrite=1
   fi
elif [ $# -eq 3 ]; then
   inFile=$1
   outFile=$2
   opt=$3
else
   sed -n '2,12p' pdfsmaller.sh
   exit
fi
if [ ! -e "${inFile}" ]; then
   echo "${inFile} not exist"
   echo "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${opt} -dNOPAUSE \
                  -dQUIET -dBATCH -sOutputFile=\"${outFile}\" \"${inFile}\""
   exit
else
   eval "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${opt} -dNOPAUSE  \
                 -dQUIET -dBATCH -sOutputFile=\"${outFile}\" \"${inFile}\""
fi
[ ${isOverWrite} -eq 1 ] && mv "${outFile}" "${inFile}"

ShowCalendar