我有一个脚本,它将 ImageMagick 命令行命令放在一起以对 PDF 文档进行处理。这些最终会非常长,但这是一个此类命令的代表性示例(为便于阅读,添加了行返回:
magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
-pointsize 12 -draw "text 535,515 'This is my test text'") "c:\IMtemp\final.pdf"
该命令工作正常。但是,文本是动态的并且来自用户输入。如果用户要包含撇号,命令行最终将是:
magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
-pointsize 12 -draw "text 535,515 'This is my test text; it's just great'") "c:\IMtemp\final.pdf"
当然,这会失败,因为撇号过早地结束了文本块。我的第一个想法是像这样逃避撇号:
magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
-pointsize 12 -draw "text 535,515 'This is my test text; it\'s just great'") "c:\IMtemp\final.pdf"
但这不起作用。相反,撇号被忽略,并且出现文本“这是我的测试文本;它非常棒”。所以然后我想也许我可以使用替代字符并尝试了这个:
magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
-pointsize 12 -draw "text 535,515 'This is my test text; it’s just great'") "c:\IMtemp\final.pdf"
这导致文本“这是我的测试文本;它非常棒”,我假设是因为 IM 没有默认为 UTF-8。我在文档中读到,这可以通过向 IM 提供代码来规避并尝试过:
magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
-pointsize 12 -draw "text 535,515 'This is my test text; it\u2019s just great'") "c:\IMtemp\final.pdf"
magick.exe -density 300 -compress ZIP ( "c:\IMtemp\32.pdf[0]" -fill "#000" -stroke "#062430" -font Arial
-pointsize 12 -draw "text 535,515 'This is my test text; it\x{2019}s just great'") "c:\IMtemp\final.pdf"
但是这些只是将反斜杠之后的所有内容显示为纯文本。
我不在乎如何保留撇号,只要其中的任何内容看起来都足够正确以供人类阅读和专业。我如何实现这一目标?
我在 Lucee 服务器上通过 cfExecute 运行 IM(在 Windows / IIS 上运行)。