The PNG to PDF Script / Un script para convertir PNG a PDF

The origin of the idea: my students gave me reports of near 20 pages for me to check. I take a felt tip pen (that writes fast), and then I scan with the marvelous Canon iR4570 [↗] [nice linux drivers for Cups from Aussie people]. Then I submit the results on the web server.
However, when at home, I employ a humble HP Deskjet 4180 [↗] [nice HPLIP interface for linux], but no PDF conversion option is available.

The sources for a solution:
Creating pdf from many image files on linux [↗]
How to make a PDF out of scanned pictures [↗]
Bash - Make PDF - (Novell Cool Solutions: Cool Tool) [↗]

My solution : method + script

Scan the pages in 200 dpi gray scale with Kooka [↗]
Run the script with a quality level between 100 [Almost no compression] - 95 [Very good quality] - ... - 65 [readable and small] - less renders ugly

Just add some tricks to have small PDF: first move to jpg with desired quality level and the create the pdf. Use Image Magick [↗] and pdftk [↗]


#!/bin/bash
#  Takes png files in the directory, converts to jpg with desired quality level
#  and then create one pdf file
# Test to make sure required programs are installed.
if [[ -z $( type -p convert ) ]]; then echo -e "Warning - ImageMagick in not installed."; exit; fi
if [[ -z $( type -p pdftk ) ]]; then echo -e "Warning - pdftk is not installed."; exit; fi
# Ask user for file name and quality level
read -p "Output file name: " pdffile;
read -p "Quality level (100-0)?: " level;
# create jpg files
for fichero in *.png ; do convert -quality $level $fichero $fichero.jpg; done;
# create pdf pages
for fichero in *.jpg ; do convert $fichero $fichero.pdf; done;
# join pages into one pdf
pdftk *.pdf cat output $pdffile.pdf;
# remove pdf pages
for fichero in *.png ; do rm -f $fichero.jpg.pdf; done;
# remove jpg images
for fichero in *.png ; do rm -f $fichero.jpg; done;
# end
exit

Comentarios