# The GS package contains a wrapper scripts to call the Ghostscript # program from within perl. # package GS; # define these locations $tmpdir = '/tmp'; $gsbin = '/usr/local/bin/gs'; # PS2PDF - # usage: $PDFdata = GS::PS2PDF($PSdata); # given a scalar containing postscript data, # return a scalar containing PDF data. # returns nothing ("") on failure sub PS2PDF { my $ps = shift; my $pdf = ""; my $infile = "$tmpdir/$$.ps"; my $outfile = "$tmpdir/$$.pdf"; # first we try to write a file containing the PDF data if (open(PS,">$infile")) { print PS $ps; close(PS); } else { return(""); } # then run ghostscript my $command = "$gsbin -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite " . "-sOutputFile=$outfile -c save pop -f $infile " . "> /dev/null 2>&1"; if (system($command)) { # non-zero means execution failed unlink $infile, $outfile; return ""; } # then read in results if (open(PDF,$outfile)) { my $buf; while (read PDF, $buf, 10240) { $pdf .= $buf; } close(PDF); } else { unlink $infile, $outfile; return ""; } # remove temp files and return unlink $infile, $outfile; return $pdf; } 1;