0

With this code, I try to convert an uploaded pdf to a jpg.

$newname = date("U");

$target = "../uploads/";
$temp = "../temp/";

if($_FILES["fileToUpload"]["type"] == "application/pdf") {
print "pdf";
if(move_uploaded_file( $_FILES["fileToUpload"]["tmp_name"], $temp.$_FILES['fileToUpload']['name'])) {
    print "pdf saved<br>";
} else {
    print "pdf not saved<br>";
}
$filename = basename( $_FILES['fileToUpload']['name'], ".pdf");
print "<br>".$filename."<br>";
if(file_exists($temp.$_FILES['fileToUpload']['name'])) {
    print "temp-file exist<br>";
    $konv = "/usr/local/bin/convert -debug 'All' ".$temp.$_FILES['fileToUpload']['name']." ".$target.$newname.".jpg 2>&1";
    print $konv."<br>";
    exec("convert -debug 'All' ".$temp.$_FILES['fileToUpload']['name']." ".$target.$newname.".jpg 2>&1", $output); 
    if ($return == "0") { echo "<br>Image generation sucssesful<br>"; } 
    else { echo "<br>Image generation failed<br>"; } 
    print_r($output);
    foreach ( $output as $file ) 
    print "$file<br>";
} else {
    print "temp-file doesn't exist";
}


}

PDF is saved in temp folder, but the image convertion fail and the only error message I get is a 1.

How do I get more informational error messages?

Jeppe Donslund
  • 469
  • 9
  • 25

1 Answers1

0
<?php
$image = new imagick('file.pdf[0]');
$image->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $image;
?>

Where [0] is the page number.

pdfinfo will give you the page numbers..

exec('/path/to/pdfinfo '.$afilename.' | awk \'/Pages/ {print $2}\'', $output);
Louis
  • 302
  • 2
  • 4
  • Then you can merge with http://php.net/manual/en/function.imagecopymerge.php if you don't want to page through the images. – Louis Nov 17 '14 at 08:15
  • Thanks for your answer but Class 'imagick' not found. Thats why I use the Imagemagick directly – Jeppe Donslund Nov 17 '14 at 08:24
  • you must have installed php5-imagick for the above to work. There are a few similar questions: http://stackoverflow.com/questions/9227014/convert-pdf-to-jpeg-with-php-and-imagemagick – Louis Nov 17 '14 at 08:34
  • I am on a shared server, so I am not sure if I can get them to install Imagisk. It would be nice if I could get more error messages. I think it shoul be possible to do the convert without Imagick. – Jeppe Donslund Nov 17 '14 at 08:37