Return to Snippet

Revision: 10927
at January 18, 2009 17:31 by iblis


Updated Code
#!/usr/bin/env perl
# updatepdf.pl
# parses a bibtex file and updates linked pdf file's metadata with bibtex data

use strict;
use warnings;

use Getopt::Std;
use Text::BibTeX;
use PDF::API2;

# option -x forces overwriting existing metadata
# option -f provides the field name which contains the linked PDF file's path
# default PDF file field name to "local-url"
my %options;
getopts('xf:', \%options);
my $field = $options{'f'} || 'local-url';

# get input bibtex file's name from cl argument or read stdin 
my $in_file = shift || "<&STDIN";
my $bib = Text::BibTeX::File->new($in_file); 

# loops thorugh bibtex entries
while (my  $entry = new Text::BibTeX::Entry $bib) {
	# skip non-regular entries
 	next unless $entry->parse_ok && $entry->metatype == BTE_REGULAR;
	# read local file field from the bibtex entry
	if ( $entry->exists($field) ) {
		my $pdf_file = $entry->get($field);
		# skip file if doesn't exist or not a .pdf
		if ($pdf_file !~ m{\.pdf$}i || ! -e $pdf_file) {
			warn "Skipping $pdf_file\n";
			next;
		}
		# get PDF's info
		my $pdf = PDF::API2->open($pdf_file);
		my %info = $pdf->info();
		# set authour and title fields
		$info{'Author'} = $entry->get('author') 
			if $entry->exists('author') && ( $options{'x'} || !defined $info{'Author'} ) ;
		$info{'Title'} = $entry->get('title') 
			if $entry->exists('title') and ( $options{'x'} || !defined $info{'Title'} );
		# write
		$pdf->info(%info);
		$pdf->update();
	} 
}

Revision: 10926
at January 18, 2009 17:28 by iblis


Initial Code
#!/usr/bin/env perl
use strict;
use warnings;

use Getopt::Std;
use Text::BibTeX;
use PDF::API2;

# option -x forces overwriting existing metadata
# option -f provides the field name which contains the linked PDF file's path
# default PDF file field name to "local-url"
my %options;
getopts('xf:', \%options);
my $field = $options{'f'} || 'local-url';

# get input bibtex file's name from cl argument or read stdin 
my $in_file = shift || "<&STDIN";
my $bib = Text::BibTeX::File->new($in_file); 

# loops thorugh bibtex entries
while (my  $entry = new Text::BibTeX::Entry $bib) {
	# skip non-regular entries
 	next unless $entry->parse_ok && $entry->metatype == BTE_REGULAR;
	# read local file field from the bibtex entry
	if ( $entry->exists($field) ) {
		my $pdf_file = $entry->get($field);
		# skip file if doesn't exist or not a .pdf
		if ($pdf_file !~ m{\.pdf$}i || ! -e $pdf_file) {
			warn "Skipping $pdf_file\n";
			next;
		}
		# get PDF's info
		my $pdf = PDF::API2->open($pdf_file);
		my %info = $pdf->info();
		# set authour and title fields
		$info{'Author'} = $entry->get('author') 
			if $entry->exists('author') && ( $options{'x'} || !defined $info{'Author'} ) ;
		$info{'Title'} = $entry->get('title') 
			if $entry->exists('title') and ( $options{'x'} || !defined $info{'Title'} );
		# write
		$pdf->info(%info);
		$pdf->update();
	} 
}

Initial URL

                                

Initial Description

                                

Initial Title
Update PDF metadata from BibTeX data file

Initial Tags

                                

Initial Language
Perl