user200340
user200340

Reputation: 3371

How to use CGI request to Ghostscript

I need to call Ghostscript in one of our cgi to convert a PDF file to a PNG image. This cgi has the encoded PDF stream in the request then decodes it, like

my $PDF_ENCODED = $q->param('PDF_ENCODED');
my $PDF_DECODED = decode_base64($PDF_ENCODED);

and it will generate a PNG image on the disk by calling 'gs' command.

My problem is I do not know how to pipe the $PDF_DECODED to gs command line. I have tried

system("$PDF_DECODED | gs -dNOPAUSE -q -r300 -sDEVICE=png16m -dBATCH -sOutputFile=/tmp/ghostscript/new-test.png-")

But it is not working.

Thanks.

Thanks again, golimar and simbabque. It is working with

$SIG{PIPE} = 'IGNORE';
open(FH, "| gs -dNOPAUSE -q -r300 -sDEVICE=png16m -dBATCH -sOutputFile=/tmp/ghostscript/new-test.png -")  or die "can't fork: $!";
print FH "$PDF_DECODED\n"   or die "can't write: $!";
close FH            or die "can't close: status=$?";

Upvotes: 1

Views: 383

Answers (1)

golimar
golimar

Reputation: 2548

In Perl you can open processes as if they were files, and when you write to the "file" handle, instead of writing data to a file, you are piping data to a process, see Using open() for IPC in perlipc.

Upvotes: 0

Related Questions