#!/usr/bin/perl -w # # word2pdf: translate Word documents into PDF. # # Stephane Galland, 15 June 2009 # # $Id$ # use strict; use File::Basename; use File::Spec; use File::Path; use File::Copy; use Getopt::Long; my$JODVERSION = "2.2.2"; my $scriptdir = File::Spec->rel2abs(dirname("$0")); my $lock = File::Spec->catfile($ENV{'HOME'},".word2pdf.lock"); sub usage() { print "usage:\n"; print "\tword2pdf fichier1 [fichier2 ... ]\n"; print "\n\texemple: word2pdf foo1.doc foo2.doc foo3.doc\n\n"; exit(1); } sub mydie(@) { unlock(); die(@_); } sub lock() { system("lockfile-create '$lock'"); } sub unlock() { system("lockfile-remove '$lock'"); } sub launch(@) { (system(@_) == 0) or mydie("system ".join(' ',@_)." failed: $?"); } #if (!GetOptions()) { # usage(); #} if (!@ARGV) { usage(); } lock(); my @files = (); foreach my $file (@ARGV) { push @files, File::Spec->rel2abs("$file"); } # Open OOFFICE server print "Starting OpenOffice Server...\n"; system('ooffice -headless \'-accept=socket,host=127.0.0.1,port=8100;urp;\' -nofirststartwizard >/dev/null 2>/dev/null; sleep 5'); print "\tserver started\n"; # Convert as many documents as you want serially. my $jarDir = File::Spec->catfile("$scriptdir","jodconverter-$JODVERSION"); chdir("$jarDir") or mydie("$jarDir: $!\n"); foreach my $file (@files) { if ($file !~ /\.pdf$/) { if ($file !~ /\.doc$/) { rename("$file", "$file.doc") or mydie("$file:$!\n"); $file .= ".doc"; } my $pdffile = "$file"; $pdffile =~ s/\.[a-zA-Z0-9]+$//; if ($pdffile !~ /\.pdf$/) { $pdffile .= ".pdf"; } print "Converting ".basename("$file")." -> ".basename("$pdffile")."\n"; launch('java', '-jar', "jodconverter-cli-$JODVERSION.jar", "$file", "$pdffile"); print "\tdone\n"; } } print "Killing OpenOffice server...\n"; system('killall /usr/lib/openoffice/program/soffice.bin >/dev/null 2>/dev/null'); system('killall soffice >/dev/null 2>/dev/null'); system('killall ooffice >/dev/null 2>/dev/null'); print "\tserver killed\n"; unlock(); exit(0); __END__