dongqianwei6664 2015-06-22 05:07
浏览 156

使用imagick使用PHP将PDF转换为JPG可以使用flat pdf,但是当有多个图层时会失败

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

  • 写回答

2条回答 默认 最新

  • dongzhoulong1797 2015-06-22 09:50
    关注

    Imagick calls the ImageMagick library to do all it's processing of images. The Image Magick library does not handle all images itself, it can delegate the rendering of them off to another library to process them. For PDF files it usually delegates to the GhostScript library.

    From the command line, if you run convert -list configure you should be able to see what delegates are being used by ImageMagick, under the DELEGATES entry.

    It looks like you are encountering a bug in Ghostscript. Trying to convert the PDF by directly calling GS version 8.70 with the command:

    gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=1 -sDEVICE=pngalpha -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r150 -sOutputFile=foo-%d.png drawing_with_layers.pdf

    gives the error:

    GPL Ghostscript 8.70: Some glyphs of the font ArialNarrow-Bold requires a patented True Type interpreter. GPL Ghostscript 8.70: Some glyphs of the font ArialNarrow requires a patented True Type interpreter. Error: /invalidaccess in --run-- Operand stack:
    --dict:8/17(L)-- F4 56.954 --dict:5/5(L)-- --dict:5/5(L)-- ArialMT --dict:11/12(ro)(G)-- --nostringval-- CIDFontObject
    --dict:6/6(L)-- --dict:6/6(L)-- 178279 --dict:6/6(L)-- --nostringval-- PDFCIDFontName ArialMT Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval--
    --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1862 1 3 %oparray_pop 1861 1 3 %oparray_pop 1845 1 3
    %oparray_pop --nostringval-- --nostringval-- 2 1 13
    --nostringval-- %for_pos_int_continue --nostringval-- --nostringval-- --nostringval-- --nostringval-- %array_continue --nostringval-- false 1 %stopped_push --nostringval-- %loop_continue --nostringval-- --nostringval-- --nostringval--
    --nostringval-- --nostringval-- --nostringval-- %array_continue --nostringval-- --nostringval-- --nostringval-- --nostringval-- --nostringval-- Dictionary stack: --dict:1154/1684(ro)(G)-- --dict:1/20(G)-- --dict:75/200(L)-- --dict:75/200(L)-- --dict:106/127(ro)(G)-- --dict:286/300(ro)(G)-- --dict:22/25(L)-- --dict:4/6(L)-- --dict:21/40(L)-- --dict:1/1(ro)(G)-- Current allocation mode is local GPL Ghostscript 8.70: Unrecoverable error, exit code 1

    Upgrading to GhostScript 9.16 does not show this problem and the PDF is converted successfully.

    (btw your PDF does not have a background layer, despite some of the text being anti-aliased to white.)

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败