2

I want to convert all pages of a PDF to JPEG. The PHP-script does what it should locally but fails on our live server.

$pdf = Zend_Pdf::load($file['tmp_name']);
echo 'Pages: '.sizeof($pdf->pages).'<br />';

for($pageCounter = 0; $pageCounter < $object->getNumberOfPages(); $pageCounter++) {
    $pagenumber = $pageCounter + 1;
    $currentfilename = $filename . "_$pagenumber.jpg";

    echo $thumbPath.$currentfilename.'<br />';

    echo $file['tmp_name']."[$pageCounter]<br />";

    $return; $out;
    exec("convert -density 250 '".$file['tmp_name']."[$pageCounter]' -quality 60 '$bigPath$currentfilename'", $out, $return);
    var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';

    exec("/usr/bin/convert -density 16 '/home/data/websites/www/meonline.be/public_html/images/magazines/".$file['name']."[$pageCounter]' -quality 60 '$thumbPath$currentfilename'", $out, $return);
    var_dump($return); echo '<br />';
}

I tried to refer to the convert command by explicitly pointing to /usr/bin/convert & /usr/local/bin/convert. I always get 1 (or 127 if i'm pointing to the wrong folder) and the output array is always empty. All folder as shown exist and have mode 777 set.

Here's a summary of all echoed output:

Full exec command:

convert -density 250 '/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf[0]' -quality 60 '/home/data/websites/www/meonline.be/public_html/images/magazines/big/pLjB9_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399449783_1.jpg'

First echo echo 'Pages: '.sizeof($pdf->pages).'<br />';

pages: 1

Second echo echo $thumbPath.$currentfilename.'<br />';

/home/data/websites/www/meonline.be/public_html/images/magazines/thumb/pLjB9_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399449783_1.jpg

Third echo echo $file['tmp_name']."[$pageCounter]<br />";

/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf[0]

Fourth echo var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';

int(1) 
array(0) { }  

UPDATE

Someone executed my command in the terminal and got the following response:

convert: Postscript delegate failed `/home/data/websites/www/meonline.be/public_html/images/magazines/HogeschoolPromotorenP-studenten.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/713.
convert: no images defined `/home/data/websites/www/meonline.be/public_html/images/magazines/big/fs5Jd_HogeschoolPromotorenP-studenten.pdf/hogeschoolpromotorenp-studenten_1399455323_1.jpg' @ error/convert.c/ConvertImageCommand/3150.

I'm 100% sure that the PDF file exists and the second line complains about the image not being defined, but this command should create them for me.

9
  • Just an idea, but you may have problems using [0] in a filename. You may not, but no-one else has any ideas yet so maybe try _0 in place of [0]. Commented May 7, 2014 at 8:28
  • 1
    @MarkSetchell The '[0]' does have it's purpose. It tells Imagick (convert) what page to select from the PDF. The name of the file actually stops after .pdf as far as the command is concerned. Commented May 7, 2014 at 8:31
  • Are you sure you have the right path to convert? Do which convert via SSH if you can. If not I believe you can do $convertPath = 'which convert'; in PHP (but use backticks rather than apostrophes to run it as a console command - backticks won't show up in comments). Commented May 7, 2014 at 8:36
  • 1
    So try the backticks trick in PHP, then :). Just tried it on Ubuntu, works fine. Commented May 7, 2014 at 8:44
  • 1
    Great! Do a web search now for "convert Postscript delegate failed" (a good strategy when following up an error is to perform searches of the form '<system/language> <error message>'). Commented May 7, 2014 at 12:34

1 Answer 1

1

In case anyone is looking for an answer, the problem was actually quite simpel.

The local server had Ghostscript installed, the live server didn't...

Download it here and follow the instructions here.

In my case, I didn't have access to the commandline on the live server. In that case, check if /usr/bin/gs is available (in which case it is installed) with this command from PHP:

$return; $out;
exec("/usr/bin/gs 2>&1", $out, $return);
var_dump($return); echo '<br />'; echo var_dump($out); echo '<br />';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.