title: Invoking Ghostscript from PHP: Unable to open the initial device
body: <p>I have installed Ghostscript from MacPorts, and am trying to invoke it from a PHP script to extract single page from a multi-page PDF file and then output it. My code looks like this:</p>
  
  <pre><code>&lt;?php
  
  $cmd = '/opt/local/bin/gs -sDEVICE=pdfwrite -dNOPAUSE \
      -dBATCH -dSAFER -dFirstPage=20 -dLastPage=20
      -sOutputFile=%stdout  "/path/to/input/pdf.pdf"';
  
  // Will uncomment this when it works
  // header("Content-Type: application/pdf");
  
  putenv("GS_LIB=/opt/local/share/ghostscript/9.02/lib");
  putenv("GS_FONTPATH=/opt/local/share/ghostscript/fonts");
  putenv("TMPDIR=/tmp");
  
  passthru($cmd);
  </code></pre>
  
  <p>When I hit this script in a web browser, I see:</p>
  
  <pre><code>GPL Ghostscript 9.02 (2011-03-30) Copyright (C) 2010 Artifex Software, Inc.
  All rights reserved. This software comes with NO WARRANTY: see the file
  PUBLIC for details. **** Unable to open the initial device, quitting.
  </code></pre>
  
  <p>The same command works from a shell.</p>