Register
It is currently Wed Jul 23, 2014 1:32 pm

Create ONE pdf file from a directory of multiple png scans.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon Mar 27, 2006 12:15 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
Create ONE pdf file from a directory of multiple png scans.

Using kooka, I"ve been scanning some articles into my computer, these usually require several pages of scans, and I save the images as .png files. The goal was to get ONE pdf document that was cross platform compatible created from the multiple scans... all with minimal effort on my part other than the scanning ;)

In my .bashrc file ...
alias makepdf='sh /home/crouse/Scripts/makepdf.sh'

Now, I just go into the directory where the multiple scans are, and type makepdf
Cool eh ;) Gotta love the power of bash. :D

Code:
#!/bin/bash
# FILE : makepdf.sh
# Function: Creates ONE pdf file from a directory of multiple png scans.
# Copyright (C) 2006 Dave Crouse <dave NOSPAMat usalug.org>
# ------------------------------------------------------------------------ #
#
# NOTES:
# Scans must be in .png format, or change the imagetype variable.
# Converts each png image file to a pdf file.
#

#######################
# VARIABLE: Change this to jpg, bmp, , or whatever image file type your converting from.
imagetype="png"
#######################

# Test to make sure required programs are installed.
if [[ -z $( type -p convert ) ]]; then echo -e "ImageMagick -- NOT INSTALLED !";exit ;fi
if [[ -z $( type -p pdftk ) ]]; then echo -e "pdftk -- NOT INSTALLED !";exit ;fi

read -p "What would you like to name your new pdf file ? :" newname;
for i in *.${imagetype} ; do convert $i $i.pdf;done;
pdftk *.pdf cat output $newname.pdf;
for i in *.${imagetype} ; do rm -f $i.pdf;done;
echo "Conversion complete. PDF file ${newname}.pdf created.";

exit


Top
 Profile WWW  
 PostPosted: Mon Apr 24, 2006 10:37 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
RPM files for SUSE:

REQUIRES: libgcj.so.4 (3.7MB)
ftp://rpmfind.net/linux/SuSE-Linux/i386 ... 5.i586.rpm

SuSe 9.2 RPM for pdftk (987 KB)
http://www.pdfhacks.com/pdftk/SuSE-9.2/ ... 0.i586.rpm


Top
 Profile WWW  
 PostPosted: Sun Jan 27, 2008 6:58 pm   

Joined: Sun Jan 27, 2008 6:53 pm
Posts: 2
Location: USA
I just made a couple modifications to support input/output files with spaces in them (windows scanner and camera wizard insists on putting spaces in filenames).

Code:
#!/bin/bash
# FILE : makepdf.sh
# Function: Creates ONE pdf file from a directory of multiple png scans.
# Copyright (C) 2006 Dave Crouse <dave NOSPAMat usalug.org>
# ------------------------------------------------------------------------ #
#
# NOTES:
# Scans must be in .png format, or change the imagetype variable.
# Converts each png image file to a pdf file.
#

#######################
# VARIABLE: Change this to jpg, bmp, or whatever image file type your converting from.
imagetype="png"
#######################

# Test to make sure required programs are installed.
if [[ -z $( type -p convert ) ]]; then echo -e "ImageMagick -- NOT INSTALLED !";exit ;fi
if [[ -z $( type -p pdftk ) ]]; then echo -e "pdftk -- NOT INSTALLED !";exit ;fi

read -p "What would you like to name your new pdf file ? :" newname;
for i in *.${imagetype} ; do convert "$i" "$i.pdf";done;
pdftk *.pdf cat output "$newname.pdf";
for i in *.${imagetype} ; do rm -f "$i.pdf";done;
echo "Conversion complete. PDF file ${newname}.pdf created.";

exit


thanks for the original script. scanned all of last semesters notes / hw and converted them to PDF with it!


Top
 Profile WWW  
 PostPosted: Tue Jan 29, 2008 10:32 pm   

Joined: Sun Jan 27, 2008 6:53 pm
Posts: 2
Location: USA
for those who don't have a scanner that can do two-sided scans, but still want to make PDFs:

1) put your even pages in one directory, and your odd pages in another.
2) say ./collate

Depends on the makepdf script above (my modded one). I called mine makepdf (no .sh) and added it to my path. You'll probably have to do the same, or edit this script to give it the full path to your makepdf script.

Code:
#!/bin/bash
# FILE : collate
# Function: takes two directories full of odd and even pages and makes one
# PDF out of them.
# I assume the following file naming convention: P xxx.ext, where xxx is a
# 3-digit number.
# Chris Reitz
#


# ask for some user input
read -p "Please enter location of odd-numbered pages [Odd] : " odd_dir;
read -p "Please enter location of even-numbered pages [Even] : " even_dir;
read -p "image file extension [jpg] : " imagetype;
# provide defaults for UI

if [[ `expr length "$odd"` = 0 ]]
then
    odd_dir=Odd ;
fi;

if [[ `expr length "$even"` = 0 ]]
then
    even_dir=Even ;
fi;

if [[ `expr length "$imagetype"` = 0 ]]
then
    imagetype=jpg ;
fi;

mkdir Collated ;

# Make the odd pages odd.
#
# The set of odd numbers is given by
#
# SIGMA(0, Inf, n, 2*n-1)
#

echo "collating odd pages..." ;

cd $odd_dir ;

for i in *.${imagetype} ;
do
    str="$i" ;
    index_num=10#${str:2:3} ;
    let odd_num=2*index_num-1 ;

    while [[ `expr length "$odd_num"` -lt 3 ]]
        do
            odd_num='0'$odd_num ;
        done ;   
       
    cp "$i" ../Collated/"$odd_num".${imagetype} ;
   
done;

# Make the even pages even.
#
# The set of even numbers is given by
#
# SIGMA(0, Inf, n, 2*n)
#

echo "collating even pages..." ;

cd ../$even_dir ;

for i in *.${imagetype} ;
do
    str="$i" ;
    index_num=10#${str:2:3} ;
    let even_num=2*index_num ;

    while [[ `expr length "$even_num"` -lt 3 ]]
        do
            even_num='0'$even_num ;
        done ;   
       
    cp "$i" ../Collated/"$even_num".${imagetype} ;
   
done;

# Use the makepdf script to turn the whole lot into one ginormous
# not-so-portable document.

cd .. ;

read -p "Make images into a PDF? (Y/n) [y] : " ask;

if [[ "$ask" = "n" ]]
then
    echo "Nothing left to do. Exiting." ;
else
    cd Collated ;
    makepdf ;
    mv *.pdf .. ;
fi;


read -p "Delete original images? (Y/n) [n] : " ask;

if [[ "$ask" = "y" ]]
then
    read -p "Are you sure? (Y/n) [n] : " askagain;

    if [[ "$askagain" = "y" ]]
    then
        rm -rf "$odd_dir" ;
        rm -rf "$even_dir" ;
    else
        echo "Nothing left to do. Exiting." ;
    fi;
else
    echo "Nothing left to do. Exiting." ;
fi;

read -p "Delete collated images? (Y/n) [n] : " ask;

if [[ "$ask" = "y" ]]
then
    read -p "Are you sure? (Y/n) [n] : " askagain;

    if [[ "$askagain" = "y" ]]
    then
        rm -rf "./Collated" ;
    else
        echo "Nothing left to do. Exiting." ;
    fi;
else
    echo "Nothing left to do. Exiting." ;
fi;

exit


I know it's not the world's most efficient code, but hey! it works. And it's my first working bash script, after one that parroted back my CPU temperature at me every second.


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP