home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / txpdf56.zip / contributed / pdfindex / pdfindex next >
Text File  |  2001-11-01  |  6KB  |  191 lines

  1. #!/usr/bin/perl
  2. # Require Perl5
  3. #
  4. # pdfindex -- PDFs to index.txt
  5. #
  6. # by SANFACE Software <sanface@sanface.com> 25 February 2001
  7. #
  8. #
  9. # This is version 3.0
  10. #
  11. use strict;
  12. use Getopt::Long;
  13. use File::Basename;
  14. use File::Find;
  15. use File::DosGlob 'glob';
  16. # You can find PDF.pm module at CPAN or at http://www.sanface.com/PDF-lib/
  17. use PDF;
  18.  
  19. my $version="3.0";
  20. my $producer="pdfindex";
  21. my $configure="pdfindex.cfg";
  22. my $help=0; my $verbose=0;
  23. my $match=""; my $recursive="";
  24. my $companyname="SANFACE Software";
  25. my $pdfindexHome="http://www.sanface.com/pdfindex.html";
  26. my $PDFinfo=""; my $PDFrules=""; my $output=""; my $i;
  27. my $SANFACEmail="mailto:sanface\@sanface.com";
  28. &GetOptions("configure=s"  => \$configure,
  29.             "help"         => \$help,
  30.             "output=s"     => \$output,
  31.             "recursive=s"  => \$recursive,
  32.             "match=s"      => \$match,
  33.             "verbose"      => \$verbose) || usage() ;
  34. my @elem=("tmpdir", "output");
  35. my %option=(tmpdir       => './',
  36.             output       => 'index.txt');
  37. my $elem=""; my $input="";
  38.  
  39. $help and usage();
  40.  
  41. open (CNF, "$configure") || die "pdfindex: couldn't open configuration file $configure\n";
  42. while (<CNF>) {
  43.   if(/#\+Begin Variables\+/../#\+End Variables\+/)
  44.     {
  45.     s/\t/ /g;        #replace tabs by space
  46.     next if /^ *\#/; #ignore comment lines
  47.     next if /^ *$/;  #ignore empty lines
  48.     foreach $elem (@elem) {if (/ *$elem *: *(.*)/i) {$option{$elem}=$1;}}
  49.     }
  50.   if(/#\+Begin PDF rules\+/../#\+End PDF rules\+/)
  51.     {
  52.     next if /^ *\#/; #ignore comment lines
  53.     $PDFrules.=$_;
  54.     }
  55.   if(/#\+Begin PDF information\+/../#\+End PDF information\+/)
  56.     {
  57.     next if /^ *\#/; #ignore comment lines
  58.     $PDFinfo.=$_;
  59.     }
  60.   }
  61. close(CNF);
  62.  
  63. $output and $option{'output'}=$output;
  64.  
  65. sub wanted {
  66.   if ($File::Find::name=~/$match/) {
  67.     push @ARGV,$File::Find::name;
  68.     }
  69.   }
  70.  
  71. if ($match && !$recursive) {
  72.    print "You can use -match option only with -recursive option\n";
  73.    exit;
  74.    }
  75.  
  76. if ($recursive) {
  77.   $match=~s/\./\\./g;
  78.   $match=~s/\*/.*/g;
  79.   $match=~s/\?/./g;
  80.   $match=~s/$/\$/;
  81.   find (\&wanted,"$recursive");
  82.   }
  83.  
  84. my $tmpfile = $option{'tmpdir'} . "pdfindex$$"; 
  85.  
  86. if (@ARGV) {
  87.   my @files;
  88.   if ($^O =~ /^MSWin32$/i && !$recursive) {
  89.     foreach $i (@ARGV) {
  90.       if($i=~/\*|\?/) {push @files,glob($i)}
  91.       else {push @files,$i}
  92.       }
  93.     }
  94.   else {@files = @ARGV}
  95.   open (OUT, ">$option{'output'}") || die "pdfinfo: couldn't open output file $option{'output'}\n"; 
  96.   foreach $input (@files) {
  97.     my $pdf_file= PDF->new($input);
  98.     if($pdf_file->IsaPDF) {
  99.       my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($input); 
  100.       my $title=$pdf_file->GetInfo("Title");
  101.       my $author=$pdf_file->GetInfo("Author");
  102.       my $creationdate=$pdf_file->GetInfo("CreationDate");
  103.       my $moddate=$pdf_file->GetInfo("ModDate");
  104.       my $subject=$pdf_file->GetInfo("Subject");
  105.       my $creator=$pdf_file->GetInfo("Creator");
  106.       my $producer=$pdf_file->GetInfo("Producer");
  107.       my $keywords=$pdf_file->GetInfo("Keywords");
  108.       my $filename=basename($input,"");
  109.       my $PDFinfocopy = $PDFinfo;
  110.       if ($PDFrules =~ /\+title=(.*)\+/) {
  111.         if ($title !~ /$1/) {next}
  112.         }
  113.       if ($PDFrules =~ /\+author=(.*)\+/) {
  114.         if ($author !~ /$1/) {next}
  115.         }
  116.       if ($PDFrules =~ /\+filename=(.*)\+/) {
  117.         if ($filename !~ /$1/) {next}
  118.         }
  119.       $PDFinfocopy =~ s/\+title\+/$title/g;
  120.       $PDFinfocopy =~ s/\+author\+/$author/g;
  121.       $PDFinfocopy =~ s/\+creationdate\+/$creationdate/g;
  122.       $PDFinfocopy =~ s/\+moddate\+/$moddate/g;
  123.       $PDFinfocopy =~ s/\+subject\+/$subject/g;
  124.       $PDFinfocopy =~ s/\+creator\+/$creator/g;
  125.       $PDFinfocopy =~ s/\+producer\+/$producer/g;
  126.       $PDFinfocopy =~ s/\+keywords\+/$keywords/g;
  127.       $PDFinfocopy =~ s/\+filename\+/$filename/g;
  128.       $PDFinfocopy =~ s/\+filepath\+/$input/g;
  129.       $PDFinfocopy =~ s/\+dev\+/$dev/g;
  130.       $PDFinfocopy =~ s/\+ino\+/$ino/g;
  131.       $PDFinfocopy =~ s/\+mode\+/$mode/g;
  132.       $PDFinfocopy =~ s/\+nlink\+/$nlink/g;
  133.       $PDFinfocopy =~ s/\+uid\+/$uid/g;
  134.       $PDFinfocopy =~ s/\+gid\+/$gid/g;
  135.       $PDFinfocopy =~ s/\+rdev\+/$rdev/g;
  136.       $PDFinfocopy =~ s/\+size\+/$size/g;
  137.       $PDFinfocopy =~ s/\+atime\+/$atime/g;
  138.       $PDFinfocopy =~ s/\+mtime\+/$mtime/g;
  139.       $PDFinfocopy =~ s/\+ctime\+/$ctime/g;
  140.       $PDFinfocopy =~ s/\+blksize\+/$blksize/g;
  141.       $PDFinfocopy =~ s/\+blocks\+/$blocks/g;
  142.       print OUT $PDFinfocopy;
  143.       }
  144.     else {print "$input is not a PDF\n"}
  145.     }
  146.   close (OUT)
  147.   &txt2pdf($option{'output'});
  148.   unlink($option{'output'});
  149.   } else {usage();}
  150.  
  151. sub usage {
  152.     print <<USAGEDESC;
  153.  
  154. usage:
  155.         pdfindex [-options ...] list
  156.  
  157. where options include:
  158.     -help                        print out this message
  159.     -configure file              default pdfindex.cfg
  160.     -output    file              default index.txt
  161.     -recursive directory         scan recursively the directory
  162.     -match     files             match different files ex. *.pdf, a?.*
  163.                                  (require -recursive option)
  164.     -verbose                     verbose
  165.  
  166. list:
  167.     with list you can use metacharacters and relative and absolute path name
  168.  
  169. example:
  170.     pdfindex *.pdf
  171.     pdfindex -m a*.pdf -r my_directory
  172.  
  173.  
  174. If you want to know more about this tool, you might want
  175. to read the docs. They came together with PDFindex!
  176.  
  177. Home: $pdfindexHome
  178.  
  179. USAGEDESC
  180.     exit(1);
  181. }
  182.  
  183.  
  184.  
  185. sub txt2pdf {
  186. my @ARGV=shift(@_);
  187.  
  188. ## Put here the txt2pdf perl code
  189.  
  190. }
  191.