在 php 脚本中使用 ghostscript
Using ghostscript in a php script
将 Laravel 用于我正在使用 https://github.com/clegginabox/pdf-merger 合并 pdf 文件和 运行 的 Web 应用程序,当我尝试将两个 pdf 合并到一起时出现以下错误,其中一个超过了版本1.4:
Exception in pdf_parser.php line 133:
This document (C:\path-to-doc\file.pdf) probably uses a compression technique
which is not supported by the free parser shipped with FPDI.
(See https://www.setasign.com/fpdi-pdf-parser for more details)
in pdf_parser.php line 133
我按照以下答案中的建议使用 ghostscript 将任何超过 1.4 的 pdf 转换为 1.4,以便合并工作。
FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI
我已经成功安装了 ghostscript 并将其添加到我的路径中。为了进行测试,我有一个名为 test.pdf
的 1.5 版示例 pdf 文件,我 运行 来自 windows 终端的以下命令:
gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf
我的 new-file.pdf
和 1.4 版一样好。
现在我有以下 php 脚本,当从网络浏览器 运行 时,它会持续加载。如果我让它 运行 有时会创建一个新文件,但它的大小大约为 3kb 并且是空白的,而且原始 pdf 文件有时也会变成空白?!
<?php
shell_exec( "gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf");
echo 'done';
知道我做错了什么吗?
正要 post 这个问题并弄明白了,所以为了其他人的利益继续回答。
我在超级用户 https://superuser.com/questions/200188/reading-a-pdf-file-for-testing-in-ghostscript#answer-200784
上阅读了这个答案
这部分是灯泡
If you use Ghostscript on Windows, you'll have two executables:
gswin32c.exe
gswin32.exe
The first one is to be run from inside a 'DOS
box' (i.e. cmd.exe window) -- either interactively or not. It prints
all stderr/stdout messages into the cmd.exe window and also expects
any input commands to be typed in there.
The second one opens a separate Window for "interactivity': prints
stderr/stdout to separate window, and expects commands there.
所以我更改了 php 脚本以使用 gswin64c
并从命令的响应中输出文本:
$output = shell_exec( "gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test.pdf");
echo "<pre>$output</pre>";
这表明我引用了错误的文件名,即 test-file.pdf
而不是 test.pdf
。
Error: /undefinedfilename in (test-file.pdf)
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
--dict:1192/1684(ro)(G)-- --dict:0/20(G)-- --dict:78/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
更新文件名后一切正常!
更新
回应 Marsha 在问题中的评论...
What do you mean by "added it to my path"? Thanks in advance.
当您将一个可执行文件或一组可执行文件的位置添加到您的 PATH 环境变量时,它允许从任何位置从命令行调用可执行文件。例如,您可以在将 ghostscript 添加到您的路径后从 C:\youruser\Desktop
调用 ghostscript,而不是在 ghostscript 文件所在的文件夹 C:\gs\bin
或其他文件夹中。
有时您安装的软件会自动为您执行此操作,有时您需要自己手动执行(例如 ghostscript)。
如何将可执行文件添加到 PATH 环境变量:
- 我的电脑
- 属性
- 高级系统设置
- 环境变量
- 然后将可执行文件附加到用户 variables/PATH 或系统 variables/Path 中现有值的末尾 - 如果您使用用户变量,它将仅适用于您的用户,如果您使用系统变量它将适用于所有用户
提示:
- 如果按
windows
键+pause
可以跳到第3步
- 您必须用分号分隔路径变量中的新条目
;
如果您 google 类似 "add exe to my path windows" 我相信您会找到大量这方面的信息。希望这对您有所帮助!
出于某种原因,Apache 没有获取 windows PATH 环境变量,我找到的解决方案是将 gs 可执行文件的完整路径写在双引号之间(如果其中有空格)。
exec('"C:\Program Files\gs\gs9.27\bin\gs.exe" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=test.png test.pdf ');
将 Laravel 用于我正在使用 https://github.com/clegginabox/pdf-merger 合并 pdf 文件和 运行 的 Web 应用程序,当我尝试将两个 pdf 合并到一起时出现以下错误,其中一个超过了版本1.4:
Exception in pdf_parser.php line 133:
This document (C:\path-to-doc\file.pdf) probably uses a compression technique
which is not supported by the free parser shipped with FPDI.
(See https://www.setasign.com/fpdi-pdf-parser for more details)
in pdf_parser.php line 133
我按照以下答案中的建议使用 ghostscript 将任何超过 1.4 的 pdf 转换为 1.4,以便合并工作。
FPDF error: This document (testcopy.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI
我已经成功安装了 ghostscript 并将其添加到我的路径中。为了进行测试,我有一个名为 test.pdf
的 1.5 版示例 pdf 文件,我 运行 来自 windows 终端的以下命令:
gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf
我的 new-file.pdf
和 1.4 版一样好。
现在我有以下 php 脚本,当从网络浏览器 运行 时,它会持续加载。如果我让它 运行 有时会创建一个新文件,但它的大小大约为 3kb 并且是空白的,而且原始 pdf 文件有时也会变成空白?!
<?php
shell_exec( "gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf");
echo 'done';
知道我做错了什么吗?
正要 post 这个问题并弄明白了,所以为了其他人的利益继续回答。
我在超级用户 https://superuser.com/questions/200188/reading-a-pdf-file-for-testing-in-ghostscript#answer-200784
上阅读了这个答案这部分是灯泡
If you use Ghostscript on Windows, you'll have two executables:
gswin32c.exe
gswin32.exe
The first one is to be run from inside a 'DOS box' (i.e. cmd.exe window) -- either interactively or not. It prints all stderr/stdout messages into the cmd.exe window and also expects any input commands to be typed in there.
The second one opens a separate Window for "interactivity': prints stderr/stdout to separate window, and expects commands there.
所以我更改了 php 脚本以使用 gswin64c
并从命令的响应中输出文本:
$output = shell_exec( "gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test.pdf");
echo "<pre>$output</pre>";
这表明我引用了错误的文件名,即 test-file.pdf
而不是 test.pdf
。
Error: /undefinedfilename in (test-file.pdf)
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push
Dictionary stack:
--dict:1192/1684(ro)(G)-- --dict:0/20(G)-- --dict:78/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
更新文件名后一切正常!
更新
回应 Marsha 在问题中的评论...
What do you mean by "added it to my path"? Thanks in advance.
当您将一个可执行文件或一组可执行文件的位置添加到您的 PATH 环境变量时,它允许从任何位置从命令行调用可执行文件。例如,您可以在将 ghostscript 添加到您的路径后从 C:\youruser\Desktop
调用 ghostscript,而不是在 ghostscript 文件所在的文件夹 C:\gs\bin
或其他文件夹中。
有时您安装的软件会自动为您执行此操作,有时您需要自己手动执行(例如 ghostscript)。
如何将可执行文件添加到 PATH 环境变量:
- 我的电脑
- 属性
- 高级系统设置
- 环境变量
- 然后将可执行文件附加到用户 variables/PATH 或系统 variables/Path 中现有值的末尾 - 如果您使用用户变量,它将仅适用于您的用户,如果您使用系统变量它将适用于所有用户
提示:
- 如果按
windows
键+pause
可以跳到第3步
- 您必须用分号分隔路径变量中的新条目
;
如果您 google 类似 "add exe to my path windows" 我相信您会找到大量这方面的信息。希望这对您有所帮助!
出于某种原因,Apache 没有获取 windows PATH 环境变量,我找到的解决方案是将 gs 可执行文件的完整路径写在双引号之间(如果其中有空格)。
exec('"C:\Program Files\gs\gs9.27\bin\gs.exe" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dGraphicsAlphaBits=4 -sOutputFile=test.png test.pdf ');