使用imagick使用PHP使用PHP将PDF转换为JPG可用于纯pdf,但在多层时会失败
作者:互联网
我有一个内部作业管理上传页面,用于pdf上传.该脚本将pdf副本保存到一个目录,然后使用imagick制作jpg副本,该副本用于显示在另一页上.使用该脚本上传的大多数PDF都可以正常运行(尽管速度稍慢且占用内存.但是仍然可以正常工作).我们从事服务业,很多上载内容都是pdf图纸集.从cad导出的工程图通常有很多层,这些文档失败并显示以下消息:
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
因此,如果我将这些有问题的pdf文件之一保存到我的桌面上,然后通过pdf打印机(在这种情况下为cutepdf)重新保存,然后尝试再次上传就可以了.因此,这当然就是为什么我假设它是这些图层…因为当我使用pdf打印机时它们被剥离并弄平了.
起初我以为文档的大小是相关的,但是后来我意识到我可以循环浏览几乎100页而不会失败(图纸集通常大约15页左右).
<?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");
?>
我尝试在“新imagick”下添加“ flattenImages”,但这没有任何帮助.我还尝试隔离该功能,并使用tmp文件为imagick功能创建.我也尝试了很多其他方法,现在整个周末我都在弄乱它,我只是被困住了,所以任何帮助将不胜感激.
如果它有助于说明导致问题的分层pdf的链接,如下所示:
https://www.dropbox.com/s/unauyb0rzpk0nup/drawing_with_layers.pdf?dl=0
然后这是通过可爱的pdf writer运行的成功上传的副本
https://www.dropbox.com/s/dv0bt7x222s93mi/no_layers.pdf?dl=0
解决方法:
Imagick调用ImageMagick库来完成图像的所有处理. Image Magick库本身并不处理所有图像,它可以将它们的渲染委托给另一个库来处理.对于PDF文件,通常将其委托给GhostScript库.
从命令行,如果运行convert -list configure,则应该能够在DELEGATES条目下查看ImageMagick正在使用哪些委托.
您似乎遇到了Ghostscript中的错误.通过使用以下命令直接调用GS版本8.70来尝试转换PDF:
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
给出错误:
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
升级到GhostScript 9.16不会显示此问题,并且PDF已成功转换.
(顺便说一下,您的PDF没有背景层,尽管其中一些文本被抗锯齿成白色.)
标签:imagick,pdf,php 来源: https://codeday.me/bug/20191028/1950477.html