pdf - Ghostscript 无法将 PDF 转换为 PNG:nostringval
问题描述
stdout
尝试将 PDF 转换为 PNG 时出现以下错误。有人知道PDF文件有什么问题吗?
此问题仅影响某些 PDF 文件,并且此特定 PDF 实际上是从更大的 PDF 中拆分出来的,并由 GhostScript 调整大小和压缩。
该命令运行,不返回错误,但标准输出有以下警告,输出 PNG 文件存在,但尺寸为 0 和 0。
标准输出:
**** Error reading a content stream. The page may be incomplete.
Output may be incorrect.
Error: /typecheck in --run--
Operand stack:
--dict:3/3(L)-- 4 9463495 4 0 --nostringval-- MediaBox --nostringval-- Parent --nostringval-- Type Page Contents --nostringval-- Resources --nostringval-- ColorSpace --nostringval-- ExtGState --nostringval-- Font --nostringval-- ProcSet --nostringval-- XObject --nostringval-- R1000 --nostringval-- R10000 --nostringval-- R10001 --nostringval-- R10002 --nostringval-- R10003 --nostringval-- R10004 --nostringval-- R10005 --nostringval-- R10006 --nostringval-- R10007 --nostringval-- R10008 --nostringval-- R10009 --nostringval-- R1001 --nostringval-- R10010 --nostringval-- R10011 --nostringval-- R10012 --nostringval-- R10013 --nostringval-- R10014 --nostringval-- R10015 --nostringval-- R10016 --nostringval-- R10017 --nostringval-- R10018 --nostringval-- R10019 --nostringval-- R1002 --nostringval-- R10020 --nostringval-- R10021 --nostringval-- R10022 --nostringval-- R10023 --nostringval-- R10024 --nostringval-- R10025 --nostringval-- R10026 --nostringval-- R10027 --nostringval-- R10028 --nostringval-- R10029 --nostringval-- R1003 --nostringval-- R10030 --nostringval-- R10031 --nostringval-- R10032 --nostringval-- R10033 --nostringval-- R10034 --nostringval-- R10035 --nostringval-- R10036 --nostringval-- R10037 --nostringval-- R10038 --nostringval-- R10039 --nostringval-- R1004 --nostringval-- R10040 --nostringval-- R10041 --nostringval-- R10042 --nostringval-- R10043 --nostringval-- R10044 --nostringval-- R10045 --nostringval-- R10046 --nostringval-- R10047 --nostringval-- R10048 --nostringval-- R10049 --nostringval-- R1005 --nostringval-- R10050 --nostringval-- R10051 --nostringval-- R10052 --nostringval-- R10053 --nostringval-- R10054 --nostringval-- R10055 --nostringval-- R10056 --nostringval-- R10057 --nostringval-- R10058 --nostringval-- R10059 --nostringval-- R1006 --nostringval-- R10060 --nostringval-- R10061 --nostringval-- R10062 --nostringval-- R10063 --nostringval-- R10064 --nostringval-- R10065 --nostringval-- R10066 --nostringval-- R10067 --nostringval-- R10068 --nostringval-- R10069 --nostringval-- R1007 --nostringval-- R10070 --nostringval-- R10071 --nostringval-- R10072 --nostringval-- R10073 --nostringval-- R10074 --nostringval-- R10075 --nostringval-- R10076 --nostringval-- R10077 --nostringval-- R10078 --nostringval-- R10079 --nostringval-- R1008 --nostringval-- R10080 --nostringval-- R10081 --nostringval-- R10082 --nostringval-- R10083 --nostringval-- R10084 --nostringval-- R10085 --nostringval-- R10086 --nostringval-- R10087 --nostringval-- R10088 --nostringval-- R10089 --nostringval-- R1009 --nostringval-- R10090 --nostringval-- R10091 --nostringval-- R10092 --nostringval-- R10093 --nostringval-- R10094 --nostringval-- R10095 --nostringval-- R10096 --nostringval-- R10097 --nostringval-- R10098 --nostringval-- R10099 --nostringval-- R1010 --nostringval-- R10100 --nostringval-- R10101 --nostringval-- R10102 --nostringval-- R10103 --nostringval-- R10104 --nostringval-- R10105 --nostringval-- R10106 --nostringval-- R10107 --nostringval-- R10108 --nostringval-- R10109 --nostringval-- R1011 --nostringval-- R10110 --nostringval-- R10111 --nostringval-- R10112 --nostringval-- R10113 --nostringval-- R10114 --nostringval-- R10115 --nostringval-- R10116 --nostringval-- R10117 --nostringval-- R10118 --nostringval-- R10119 --nostringval-- R1012 --nostringval-- R10120 --nostringval-- R10121 --nostringval-- R10122 --nostringval-- R10123 --nostringval-- R10124 --nostringval-- R10125 --nostringval-- R10126 --nostringval-- R10127 --nostringval-- R10128 --nostringval-- R10129 --nostringval-- R1013 --nostringval-- R10130 --nostringval-- R10131 --nostringval-- R10132 --nostringval-- R10133 --nostringval-- R10134 --nostringval-- R10135 --nostringval-- R10136 --nostringval-- R10137 --nostringval-- R10138 --nostringval-- R10139 --nostringval-- R1014 --nostringval-- R10140 --nostringval-- R10141 --nostringval-- R10142 --nostringval-- R10143 --nostringval-- R10144 --nostringval-- R10145 --nostringval-- R10146
以下是原始 PDF 的详细信息(拆分/调整大小之前):
Content Creator: Bluebeam Revu x64
Encoding Software: macOS Version 10.14.6 (Build 18G95) Quartz PDFContext
代码(Node.js):
exports.convert = function(pdfPath){
return new Promise( (resolve, reject) => {
//Convert pdf
console.log('pdf2Image: Version', 0.42);
//Remove extension
let path = pdfPath.split('.')[0];
let splitPathTest = path.split('/');
let outputFileName = splitPathTest[splitPathTest.length - 1] + '.png';
let outputDirectory = splitPathTest.slice(0, splitPathTest.length - 1).join('/') + '/';
let outputPath = outputDirectory + outputFileName;
console.log('pdfPath', pdfPath);
console.log('outputPath', outputPath);
console.log('output Filename ', outputFileName);
if (fs.existsSync(pdfPath)) {
console.log('pdfPath exists. PDF found.');
}else{
console.warn('pdfPath does not exist. PDF not found.');
}
//============== Ghostscript
//Conver PDF -> PNG
console.log('gs - starting');
gs()
.batch()
.nopause()
.option('-r' + 50 * 2)
.option('-dDownScaleFactor=2')
.executablePath('lambda-ghostscript/bin/./gs')
.device('png16m')
.output(outputPath)
.input(pdfPath)
.exec(function (err, stdout, stderr) {
if (!err) {
console.log('gs executed w/o error');
console.log('stdout',stdout);
console.log('stderr',stderr);
console.log('output saved to: ', outputPath);
resolve(outputPath);
} else {
console.log('gs error:', err);
reject();
}
});
})
}
解决方案
该错误确实不言自明,PDF文件以某种方式损坏。Ghostscript 试图恢复但无法恢复,因此它发出错误并停止(这就是您的 PNG 为空的原因)。请注意,这不是警告,而是错误。
错误是类型检查;所以 Ghostscript 遇到了一个类型不适合该操作的对象。例如,算术运算的字符串操作数,或期望字符串的数字等。
在没有看到文件的情况下,我不能说更多。你没有说 Ghostscript 的版本是什么,我也不知道 'lmbda-ghostscript' 是什么,但它听起来不像是它的规范版本。如果我是你,我会做的第一件事就是获取 Ghostscript 源并构建它,然后从 shell 中尝试。如果失败,那么您可以打开一个错误报告,有人可以告诉您原因。
啊,显然“lmbda-ghostscript”是某种 AWS 东西。不仅如此,它还很老。最新版本似乎是三年前的 9.20,并且有一些众所周知的和公开的安全漏洞。我仍然建议您使用当前版本(9.50)尝试从本地开始到结束的整个流程表单,如果可行,那么您真的需要弄清楚如何使用它。如果没有,那么您可以报告错误。
推荐阅读
- c++ - 安装 R 库:未定义符号
- react-native - 如何使用 FlatList 获取每个 View y 位置?
- java - 在Java中将一个字符串重新排列为另一个
- performance - Angular2 +每个表格行都是带有onPush的单独组件,它会重新渲染它吗?
- python - 将项目随机播放到列表 python
- docker - Kubernetes Docker 版本升级 | 修复错误“意外的EOF”
- apache - 重新启动时Apache服务器时间更改
- javascript - socket.io SSL / HTTPS 在客户端出现 404 错误 / 无法连接到服务器
- c# - 如何使用在本地系统下运行的 Windows 服务以不同的用户身份调用控制台应用程序?
- asp.net - 为所有页面使用一个到 API 的连接