【发布时间】:2016-11-19 11:58:04
【问题描述】:
我正在使用 ghostscript 压缩我的 pdf 文件,这会在我必须处理的受密码保护的情况下引发错误。
Shell 脚本
GS_RES=`gs -sDEVICE=pdfwrite -sOutputFile=$gsoutputfile -dNOPAUSE -dBATCH $2 2>&1`
if [ "$GS_RES" != "" ]
then
gspassmsg="This file requires a password for access"
echo "Error message is :::::: "$GS_RES
gspassworddoc=`awk -v a="$GS_RES" -v b="$gspassmsg" 'BEGIN{print index(a,b)}'`
if [ $gspassworddoc -ne 0 ]
then
exit 3 #error code - password protected pdf
fi
fi
而我的GS_RES执行命令后的值如下
错误信息 1:
GPL Ghostscript 9.19 (2016-03-23) Copyright (C) 2016 Artifex Software, Inc. All
rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for d
etails. Error: /syntaxerror in -file- Operand stack: Execution stack: %interp_ex
it .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --n
ostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1967 1 3 %opa
rray_pop 1966 1 3 %oparray_pop 1950 1 3 %oparray_pop 1836 1 3 %oparray_pop --nos
tringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringva
l-- 2 %stopped_push Dictionary stack: --dict:1196/1684(ro)(G)-- --dict:0/20(G)--
--dict:78/200(L)-- Current allocation mode is local Current file position is 1
错误消息 2:
GPL Ghostscript 9.19 (2016-03-23) Copyright (C) 2016 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Error: Cannot find a 'startxref' anywhere in the file. Output may be incorrect. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Error: An error occurred while reading an XREF table. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html The file has been damaged. This may have been caused gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html by a problem while converting or transfering the file. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Ghostscript will attempt to recover the data. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html However, the output may be incorrect. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Error: Trailer dictionary not found. Output may be incorrect. No pages will be processed (FirstPage > LastPage). gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html This file had errors that were repaired or ignored. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html Please notify the author of the software that produced this gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html file that it does not conform to Adobe's published PDF gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html specification. gs.pdf gsempty.pdf new_sathishks_protected.html sathishks_protected.html The rendered output from this file may be incorrect.
在错误消息 2 上运行 awk
gspassmsg="This file requires a password for access"
gspassworddoc=`awk -v a="$GS_RES" -v b="$gspassmsg" 'BEGIN{print index(a,b)}'`
它会抛出以下错误
错误:awk: newline in string GPL Ghostscript 9.19... at source line 1
错误信息 3
**** Error: Cannot find a 'startxref' anywhere in the file.
**** Warning: An error occurred while reading an XREF table.
**** The file has been damaged. This may have been caused
**** by a problem while converting or transfering the file.
**** Ghostscript will attempt to recover the data.
**** Error: Trailer is not found.
**** This file had errors that were repaired or ignored.
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
我无法使用以下答案中的 sn-p 捕获此错误
if ! gs_res=$(gs -sDEVICE=pdfwrite -sOutputFile="$gsoutputfile" -dNOPAUSE -dBATCH "$2" 2>&1 1>/dev/null); then
echo "Error message is :::::: $gs_res" >&2
gspassmsg='This file requires a password for access'
[[ $gs_res == *"$gspassmsg"* ]] && exit 3 # password protected pdf
echo "Some other error !"
fi
请澄清以下内容
- 为什么
awk在这里表现得很奇怪?我错过了什么? - 如何在包含特殊字符的字符串中查找模式?
- Ghostscript 是否有任何类似的预定义错误消息?如果可能,请建议一些文档以供参考..
- 是否可以使用 ghostscript 压缩受密码保护的 pdf?
- 在上述情况下如何确保 gs 压缩成功?由于我可能不知道 Ghostscript 可能抛出的不同可能错误,因此我可以与我执行的命令结果进行交叉检查。
我对这个 shell 脚本很陌生。有人请帮我解决这个问题。
PS:我已经用其他详细信息编辑了我的问题。请调查一下。如果有什么需要补充的,我会补充的。
【问题讨论】:
-
@mklement0 从命令输出中搜索字符串(例如“此文件需要密码才能访问”) - 将此过程称为
grep。 -
当我的命令输出中包含一些特殊字符时,我不确定我猜
awk会播放有线。 -
命令输出中的特殊字符类似于 (`, ") - 我已在错误消息 2 中添加。请查看。
-
你是对的@mklement0。听起来不错!!超级:+1:
-
@subramanianrasapan:很高兴听到这个消息。我已经意识到这不仅仅是
index():当您尝试将多行字符串作为变量值传递时,BSD Awk 从根本上失败,除非您\-转义换行符。我的回答中有详细信息和替代解决方案。
标签: linux bash shell ghostscript