I'm trying to convert a multipage pdf to jpg with ghostscript in php. The command right now looks something like this:
gs -q -dBATCH -sDEVICE=jpeg -dNOPAUSE -dSAFER -dJPEGQ=100 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -r72 -sOutputFile=- some.pdf
What I want is to find a way to input the pdf as string wich looks something like this: '%PDF-1.4 %���� 1 0 obj <> endobj 2 0 obj <> endobj, etc.' and output all pages to stdout. Providing an actual pdf file to the command works pretty fine, but it returns a single page. If opting for file writing there is an option p%03d.jpg to get all the pages, but I need it to be dumped to temp/memory. From what I understand you need to use pipes to get this to work. I made something with proc_open() but without any success because I don't know how to pass the string to the pipe.
$args = [
'-dBATCH',
'-sDEVICE=jpeg',
'-dNOPAUSE',
'-dSAFER',
'-dJPEGQ=100',
'-dGraphicsAlphaBits=4',
'-dTextAlphaBits=4',
'-r72',
'-sOutputFile=-',
$path . '/some.pdf'// this shouold be passed as string stdin
];
$descr = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe','w']
];
$pipes = array();
$args = implode(' ', $args);
$commd = "gs -q $args";
$process = proc_open($commd, $descr, $pipes);
$response = '';
if (is_resource($process)) {
fputs($pipes[0], $pdf);
fclose($pipes[0]);
while ($f = fgets($pipes[1])) {
$response .= $f;
}
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
echo '<img src="data:image/png;base64, ' . base64_encode($response) . '" />';
Update: Found the solution for the input. It'a a dash instead of the last argument representing the input file. The multipage output still remains an issue.