Advertisement
Guest User

Untitled

a guest
Feb 16th, 2016
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. #!/bin/bash
  2. # --------------------------------------------
  3. # Generate a PDF document from a given list of documents
  4. # Documents are added in final document following
  5. # alphabetical order
  6. #
  7. # Setup procedure : http://bernaerts.dyndns.org/linux/74-ubuntu/338-ubuntu-generate-pdf-from-documents
  8. #
  9. # Depends on :
  10. # * imagemagick
  11. # * unoconv
  12. # * ghostscript
  13. # * mimetype
  14. #
  15. # Parameters : full path of documents
  16. #
  17. # Revision history :
  18. # 05/07/2015, V1.0 - Creation by N. Bernaerts
  19. # 15/08/2015, V1.1 - Force jpeg quality to 95
  20. # 25/09/2015, V1.2 - Add configuration file
  21. # 02/10/2015, V2.0 - Code rewrite to handle progress and notification
  22. # ---------------------------------------------------
  23.  
  24. # -------------------------------------------------------
  25. # check tools availability
  26. # -------------------------------------------------------
  27.  
  28. command -v bc >/dev/null 2>&1 || { zenity --error --text="Please install bc"; exit 1; }
  29. command -v mimetype >/dev/null 2>&1 || { zenity --error --text="Please install mimetype"; exit 1; }
  30. command -v convert >/dev/null 2>&1 || { zenity --error --text="Please install convert [imagemagick]"; exit 1; }
  31. command -v unoconv >/dev/null 2>&1 || { zenity --error --text="Please install unoconv"; exit 1; }
  32. command -v gs >/dev/null 2>&1 || { zenity --error --text="Please install gs utility [ghostscript]"; exit 1; }
  33.  
  34. # ---------------------------------------------------------
  35. # Read and calculate parameters from configuration file
  36. # ---------------------------------------------------------
  37.  
  38. # Configuration file : ~/.config/pdf-generate.conf
  39. FILE_CONF="$HOME/.config/pdf-generate.conf"
  40.  
  41. # check configuration file
  42. [ -f "$FILE_CONF" ] || { zenity --error --text="Please create and configure ${FILE_CONF}"; exit 1; }
  43.  
  44. # Load configuration file
  45. COMPRESSION=$(cat "${FILE_CONF}" | grep "compression" | cut -d'=' -f2)
  46. DENSITY=$(cat "${FILE_CONF}" | grep "density" | cut -d'=' -f2)
  47.  
  48. # calculate page size
  49. PAGE_WIDTH=$(echo "${DENSITY} * 8.27" | bc)
  50. PAGE_HEIGHT=$(echo "${DENSITY} * 11.7" | bc)
  51.  
  52. # -------------------------------------------------------
  53. # Retrieve or select input files
  54. # -------------------------------------------------------
  55.  
  56. # set separator as carriage return
  57. IFS=$'\n'
  58.  
  59. # loop thru arguments to load candidate files
  60. for ARGUMENT
  61. do
  62. [ -f "${ARGUMENT}" ] && ARR_FILE=("${ARR_FILE[@]}" "${ARGUMENT}")
  63. [ -d "${ARGUMENT}" ] && ARR_FILE=("${ARR_FILE[@]}" $(find "${ARGUMENT}" -maxdepth 1 -type f) )
  64. done
  65.  
  66. # if there is no candidate files, open selection dialog
  67. if [ ${#ARR_FILE[@]} -eq 0 ]
  68. then
  69. # open multiple files selection dialog box
  70. LST_FILE=$(zenity --file --multiple --title="Select file to merge as PDF")
  71.  
  72. # generate video files array
  73. ARR_FILE=($(echo "${LST_FILE}" | tr "|" "\n"))
  74. fi
  75.  
  76. # -------------------------------------------------------
  77. # loop thru selected files to check convertibility
  78. # -------------------------------------------------------
  79.  
  80. for FILE in "${ARR_FILE[@]}"
  81. do
  82. # document type undefined
  83. DOCTYPE=""
  84.  
  85. # get the file mime type (application/msword, ...)
  86. MIMETYPE=$(mimetype -b "${FILE}")
  87.  
  88. # check if file is a image file (.jpg, .png, .tiff, ...)
  89. CHECKTYPE=$(echo "${MIMETYPE}" | grep "image/")
  90. [ "${CHECKTYPE}" != "" ] && DOCTYPE="image"
  91.  
  92. # check if file is a libreoffice file (.odt, .ods, ...)
  93. CHECKTYPE=$(echo "${MIMETYPE}" | grep ".opendocument.")
  94. [ "${CHECKTYPE}" != "" ] && DOCTYPE="libreoffice"
  95.  
  96. # check if file is a microsoft file 2007+ file (.docx, .xlsx, .pptx, ...)
  97. CHECKTYPE=$(echo "${MIMETYPE}" | grep "vnd.openxmlformats-officedocument.")
  98. [ "${CHECKTYPE}" != "" ] && DOCTYPE="ms-office"
  99.  
  100. # check some specific document types
  101. case $MIMETYPE in
  102. # ms-office document (.doc, .xls, .ppt, ...)
  103. "application/msword" | "application/vnd.ms-word" | "application/vnd.oasis.opendocument.text" | \
  104. "application/vnd.ms-excel" | "application/vnd.ms-powerpoint" )
  105. DOCTYPE="ms-office"
  106. ;;
  107.  
  108. # PDF document (.pdf)
  109. "application/pdf" | "application/x-pdf" | "application/x-bzpdf" | "application/x-gzpdf" )
  110. DOCTYPE="pdf"
  111. ;;
  112.  
  113. # plain text file (.txt)
  114. "text/plain" | "application/x-shellscript" )
  115. DOCTYPE="text"
  116. ;;
  117.  
  118. * )
  119. ;;
  120. esac
  121.  
  122. # if document type is compatible, add current file as candidate
  123. [ "${DOCTYPE}" != "" ] && ARR_CANDIDATE=("${ARR_CANDIDATE[@]}" "${FILE}|${DOCTYPE}")
  124.  
  125. done
  126.  
  127. # -------------------------------------------------------
  128. # Confirmation dialog box
  129. # -------------------------------------------------------
  130.  
  131. # calculate number of files to convert
  132. NBR_FILE=${#ARR_FILE[@]}
  133. NBR_CANDIDATE=${#ARR_CANDIDATE[@]}
  134.  
  135. # if some candidate file exist, order them and display confirmation dialog
  136. if [ ${NBR_CANDIDATE} -gt 0 ]
  137. then
  138. # order generated PDF files in alphabetical order
  139. ARR_CANDIDATE=($(sort <<<"${ARR_CANDIDATE[*]}"))
  140.  
  141. # generate final file name
  142. FILE=$(echo "${ARR_CANDIDATE[0]}" | cut -d'|' -f1)
  143. FILE_FINAL="$(echo "${FILE}" | sed 's/^\(.*\)\..*$/\1/')-merged.pdf"
  144.  
  145. # display confirmation dialog box
  146. RESULT=$(zenity --question --title="Merge to PDF" --text="${NBR_CANDIDATE} file(s) out of ${NBR_FILE} will be merged to a single PDF file (${DENSITY} DPI)\n\nDo you want to generate ${FILE_FINAL} ?" )
  147. ACTION=$?
  148.  
  149. # else display error dialog
  150. else
  151. # display confirmation dialog box
  152. RESULT=$(zenity --error --title="Merge to PDF" --text="There is no file compatible with PDF format." )
  153. ACTION=""
  154.  
  155. fi
  156.  
  157. # if action canceled or error, exit
  158. [ "${ACTION}" != "0" ] && exit 1
  159.  
  160. (
  161.  
  162. # -------------------------------------------------------
  163. # loop thru candidate files to convert them to PDF
  164. # -------------------------------------------------------
  165.  
  166. for CANDIDATE in "${ARR_CANDIDATE[@]}"
  167. do
  168. # retrieve document type and filename
  169. FILE=$(echo "${CANDIDATE}" | cut -d'|' -f1)
  170. DOCTYPE=$(echo "${CANDIDATE}" | cut -d'|' -f2)
  171.  
  172. # progress display
  173. echo "# Conversion of ${FILE}"
  174.  
  175. # get file name without extension & generate resulting PDF file name
  176. FILE_BASE="$(echo "${FILE}" | sed 's/^\(.*\)\..*$/\1/')"
  177. FILE_PDF="${FILE_BASE}.pdf"
  178.  
  179. # convert file according to its type
  180. case $DOCTYPE in
  181. # PDF files
  182. "pdf" )
  183. ARR_PDF=("${ARR_PDF[@]}" "${FILE}")
  184. ;;
  185.  
  186. # image files
  187. "image" )
  188. convert "${FILE}" -compress jpeg -quality ${COMPRESSION} -resize ${PAGE_WIDTH}x${PAGE_HEIGHT} -gravity center -extent ${PAGE_WIDTH}x${PAGE_HEIGHT} -units PixelsPerInch -density ${DENSITY}x${DENSITY} "${FILE_PDF}"
  189. ARR_TMP=("${ARR_TMP[@]}" "${FILE_PDF}")
  190. ARR_PDF=("${ARR_PDF[@]}" "${FILE_PDF}")
  191. ;;
  192.  
  193. # office files
  194. "libreoffice" | "ms-office" | "text" )
  195. unoconv -f pdf -o "${FILE_PDF}" "${FILE}"
  196. ARR_TMP=("${ARR_TMP[@]}" "${FILE_PDF}")
  197. ARR_PDF=("${ARR_PDF[@]}" "${FILE_PDF}")
  198. ;;
  199.  
  200. # other formats, not handled
  201. * )
  202. ;;
  203. esac
  204.  
  205. done
  206.  
  207. # -------------------------------------------------------
  208. # Final merge
  209. # -------------------------------------------------------
  210.  
  211. if [ ${#ARR_PDF[@]} -gt 0 ]
  212. then
  213. # progress display
  214. echo "# Final assembly of ${FILE_FINAL}"
  215.  
  216. # generate resulting PDF
  217. gs -q -dNOPAUSE -dBATCH -dSAFER -sPAPERSIZE=a4 -dPDFFitPage -dCompatibilityLevel=1.4 -sDEVICE=pdfwrite -sOutputFile="${FILE_FINAL}" ${ARR_PDF[@]}
  218. fi
  219.  
  220. # -------------------------------------------------------
  221. # Temporary files clean-up
  222. # -------------------------------------------------------
  223.  
  224. # loop to remove temporary files
  225. for TMP_FILE in "${ARR_TMP[@]}"
  226. do
  227. rm "${TMP_FILE}"
  228. done
  229.  
  230. ) | zenity --width=500 --height=25 --progress --pulsate --auto-close --title "Merge to PDF"
  231.  
  232. # -------------------------------------------------------
  233. # End of job notification
  234. # -------------------------------------------------------
  235.  
  236. [ ${#ARR_CANDIDATE[@]} -gt 0 ] && zenity --notification --window-icon="evince" --text="${FILE_FINAL} generated." \
  237. || zenity --notification --window-icon="error" --text="Document type was not compatible for PDF generation."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement