I have an internal job management upload page for pdf uploads. The script saves a copy of the pdf to one directory, then using imagick makes a jpg copy that is used to display on another page. Most of the PDFs uploaded using the script work perfectly(albeit a little slow and memory consuming..but still working as intended). We are in a service business and a lot of the uploads are pdf drawing sets. Drawings exported from cad usually have a whole bunch of layers, and these documents fail with with following message:
Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `../../Dropbox/Job_Docs/15-0273 La Bella, Cassandra/15-0273 La Bella, Cassandra-layout 6-22-2015 55876fa96aa00.pdf': No such file or directory @ pdf.c/ReadPDFImage/611' in /home/solargai/public_html/dash3/upload.php:79 Stack trace: #0 /home/solargai/public_html/dash3/upload.php(79): Imagick->__construct('../../Dropbox/J...') #1 {main} thrown in /home/solargai/public_html/dash3/upload.php on line 79
So if i save one of these problem pdf's to my desktop and then re-save it through a pdf printer(cutepdf in this case) then try to upload it again it works fine. So that is of course why i am assuming that it is the layers...because they are stripped out and flattened when i use the pdf printer.
At first i thought the size of the document was related but then i realized i could cycle through almost 100 pages without failure(the drawing sets are usually like 15 pages or so).
<?php
set_time_limit(0);
ignore_user_abort(1);
session_start();
$uuid = uniqid();
$today = date("n-j-Y");
$jobname = $_POST['jobname'];
$_SESSION['jobname'] = $jobname;
$uploadType = $_POST['uploadType'];
$writeSTATE = $_POST['writeSTATE'];
$fileName = $jobname."-".$uploadType." ".$today." ".$uuid;
$fileNamePDF = $fileName.".pdf";
$path = "../dash2/jobinfoDOCS/".$jobname."/".$uploadType."/";
$DOCSpath = "../../Dropbox/Job_Docs/".$jobname."/";
//////remove directory
if($writeSTATE == "overwrite") {
$filesD = glob($path . '*', GLOB_MARK);
foreach ($filesD as $fileD) {
if (substr($fileD, -1) == '/')
delTree($fileD);
else
unlink($fileD);
}
rmdir($path);
}
if (!file_exists($path)) {
mkdir($path, 0777, true);
//echo "created folder for path: " . $path;
}
if (!file_exists($DOCSpath)) {
mkdir($DOCSpath, 0777, true);
//echo "created folder for path: " . $DOCSpath;
}
//echo "upload type: " . $_POST['uploadType'] . "<br>";
//echo "path: " . $path . "<br>";
//echo $_FILES['layout']['tmp_name'];
if (is_uploaded_file($_FILES['layout']['tmp_name'])) {
if ($_FILES['layout']['type'] != "application/pdf") {
//echo "<p>Class notes must be uploaded in PDF format.</p>";
} else {
$name = $_POST['name'];
$result = move_uploaded_file($_FILES['layout']['tmp_name'], $DOCSpath.$fileNamePDF);
///write pdf to jpg
// if ($result == 1) echo "<p>File successfully uploaded.</p>";
//else echo "<p>There was a problem uploading the file.</p>";
} #endIF
} #endif
$fi = new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS);
//printf("There were %d Files", iterator_count($fi));
$fileCOUNT = iterator_count($fi);
//echo $fileCOUNT;
$PDFpath = $DOCSpath.$fileNamePDF;
$img = new imagick($PDFpath);
$img = $img->flattenImages();
$img->setResolution(175,175);
$number = $img->getnumberimages();
for($i=0;$i<$number;++$i)
{
$count = $i + $fileCOUNT;
$JPGpath = $path.$count."--".$fileName.".jpg";
//echo $PDFpath."AND".$JPGpath;
$img->readImage("{$PDFpath}[".$i."]");
$img->writeImage("{$JPGpath}");
}
header("location:index.php");
?>
I tried adding in the "flattenImages" under the "new imagick" and that didn't help anything. i also tried isolating the function and using the files tmp to create for the imagick function. There was a bunch of other stuff i tried too and I've been messing with it all weekend at this point now and i am just plain stuck so any help would be greatly appreciated.
If it helps to illustrate the link for the layered pdf causing the problems is below: https://www.dropbox.com/s/unauyb0rzpk0nup/drawing_with_layers.pdf?dl=0
then here is the copy that successfully uploads that has been run through cute pdf writer https://www.dropbox.com/s/dv0bt7x222s93mi/no_layers.pdf?dl=0