问题描述
我正在本地ubuntu 20.04机器上运行laravel项目,该机器在另一台机器上运行正常。该项目使用Image Magic将PDF转换为PNG。当我调用将PDF转换为PNG的函数时,出现以下错误:
ImagickException 尝试执行安全策略“ PDF”不允许的操作@ error / constitute.c / IsCoderAuthorized / 408
该函数的外观如下:
public function show($id)
{
$book = Book::all()->first();
$file = public_path('' . $book->pdf);
$count = 0;
$im = new \Imagick($file);
$blobList=[];
do {
$page = new \Imagick();
$page->setResolution(300,300);
$page->readImage($file . '['.$count.']');
$page->setimageFormat("jpeg");
header("Content-Type: image/jpeg");
$image = $page->getimageBlob();
array_push($blobList,$image);
$count++;
} while (5> $count);
// $im->resizeImage(200,200,1,0);
return view('notice/showbook',compact('book','blobList'));
}
到目前为止,我已经安装了image magic和gs并参考了this question的可接受答案在policymap.xml
中进行了更改
但是,错误仍然存在。请帮忙!
谢谢
解决方法
当我尝试使用 pyocr 解析 pdf 时,我也遇到了同样的问题
wand.exceptions.PolicyError: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408
为了解决这个问题,我编辑了 ImageMagick-6 的 policy.xml。在该文件中,将策略权限更改为读/写
<policy domain="coder" rights="none" pattern="PDF" />
改为
<policy domain="coder" rights="read|write" pattern="PDF" />
如果它不能解决您的问题,您还可以在同一文件中启用以下行。
<!-- <policy domain="module" rights="none" pattern="{PS,PDF,XPS}" /> -->
改为
<policy domain="module" rights="read|write" pattern="{PS,XPS}" />