php - 使用 imagick 去除 pdf 文件中的注释
问题描述
我使用imagick将pdf翻译成jpg;
但由于 pdf 文件中存在注释,它返回黑色区域,当我将图片悬停在 pdf 文件中时,会出现注释
图像文件:
原始pdf示例:
我的代码:
$img = new Imagick('./pige.pdf[1]');
$img->setImageBackgroundColor('white');
$img = $img->flattenImages();
$img->setImageFormat('jpg');
$img->writeImage('image.jpg');
解决方案
所以我的解决方案是使用 imagick 更改默认安装的 gs :
这似乎是默认情况下与 ImageMagick 一起安装的 ghostscript 版本中的一个错误。Ghostscript 是实际执行从 pdf 到渲染位图的转换的程序。
我尝试了从以下网址下载的最新版本的 Ghostscript:https ://www.ghostscript.com/download/gsdnld.html
使用命令行转换 PDF:
gs-9540-linux-x86_64 -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' '-sOutputFile=/var/app/bugs/bug389/gs_output.png' '-f/var/app/bugs/bug389/pige.pdf'
它似乎工作正常。
我想如果你:
- 通过运行 which gs 找到 ghost 脚本在您的机器上的位置
- 然后只需用从https://www.ghostscript.com/download/gsdnld.html下载的文件替换该文件(在某处备份原始文件后), 这可能会使您的问题消失。
推荐阅读
- c - 在 C 结构数组中存储字符串时遇到问题
- mysql - 将 Blob 转换为 CSV - 日期格式
- javascript - 在反应中不重定向到同一个组件
- flutter - 如何在 Flutter 中查看同一页面中的动态数据?
- node.js - 如何在mongodb中查询多个集合?
- sql - 使用结构检索数组中的值
- php - 如何在 php 中使内部联接查询可重新使用?
- ansible - 在 ansible.builtin.shell 中使用 delta 在 loop_control 标签
- powershell - Invoke-RestMethod 未正确传递有效负载中的 System.Object[] 字段
- php - 如何在for循环中存储多个值?php