#!/usr/bin/perl -w #********************************************************************** # barcodify - Copyright (C) 2004 - Cameron Morland # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. #********************************************************************** if (@ARGV < 1) { die "usage: barcodify \n"; } # application and constant switches (use code39 and eps) my $gnubarcode = "barcode -e code39 -E "; my $usemd5 = 0; my $filename = $ARGV[0]; $filename =~ s/"/\\"/; my $isnumeric = 1; my $PMID = readlink $filename; if ($PMID) { # the file is a symbolic link, so we track down what it points to. $PMID =~ s|.*/||g; # trim leading stuff if ($PMID =~ m|^[0-9]*\.pdf$|) { # it's numbers only, so assume a PMID file $PMID =~ s/\.pdf$//; $PMID = "P$PMID"; } elsif ($PMID =~ m|^M[0-9]*\.pdf$|) { # it's 'M' followed by numbers, so assume a manual ID (MID) file $PMID =~ s/\.pdf$//; } else { # not .pdf, so not a PMID file. $PMID = ''; $isnumeric = 0; } } else { # not a symbolic link $PMID = $filename; $PMID =~ s|.*/||g; # trim leading stuff if ($PMID =~ m|^[0-9]*\.pdf$|) { # the file is .pdf, but it's not a symbolic link. $PMID =~ s/\.pdf$//; $PMID = "P$PMID"; } elsif ($PMID =~ m|^M[0-9]*\.pdf$|) { # it's 'M' followed by numbers, so assume a manual ID (MID) file $PMID =~ s/\.pdf$//; } else { warn "No barcode for this document.\n"; #die "'$PMID' is not numeric.\n"; $isnumeric = 0; } } if ($isnumeric && ($filename =~ m|\.pdf$|)) { # translate the PDF, but only if we'll be able to add a barcode open(MAIN, "pdftops \"$filename\" - |") or die "Can't run pdftops on $filename: $!\n"; } else { # pass it through unchanged, because it's not a pubmed PDF. open(MAIN, "<$filename") or die "Can't read $filename: $!\n"; } while(
) { # print the postscript headers, up to the end of comments print $_; if (m/^%%EndComments/) { last; } } my $md5; if ($usemd5) { $md5 = `md5sum "$filename"`; # get md5 sum for file $md5 =~ s/ .*//; # chop off filename $md5 = uc($md5); chomp($md5); } if ($isnumeric) { # Entropy makes everything unique. print "\n% ", rand, "\n"; # print postscript redefinition headers, courtesy of Isaac. print <> begin /Encoding ISOLatin1Encoding 256 array copy def Encoding 45 /hyphen put currentdict end definefont pop /codeFont /Helvetica-Bold findfont 48 scalefont def /regFont /ISO-Times-Roman findfont 12 scalefont def /bigFont /ISO-Times-Roman findfont 24 scalefont def /userFont /Courier-Bold findfont 40 scalefont def /cshow { dup stringwidth pop 2 div neg 0 rmoveto show } bind def /rshow { dup stringwidth pop neg 0 rmoveto show } bind def /showHeader { gsave initgraphics EOF ; if ($usemd5) { print <) { print; } close(GNUBARCODE); print "grestore\n"; print "%%% end MD5 barcode\n"; } if ($PMID && $isnumeric) { #warn "[$PMID]\n"; print <) { print; } close(GNUBARCODE); print "grestore\n"; print "%%% end PMID barcode\n"; } print <) { # print the rest of the file print $_; } close(MAIN); close(STDOUT);