我有bash脚本,采取小册子格式的PDF,并将其转换为单独的页面。该脚本由运行在nginx下的php调用。
我使用的是pdfcrop,它调用pdfTex,这是故障点。
该脚本以root用户身份从命令行运行良好。但是,当由nginx运行(脚本通过php调用)时,当pdfcrop调用pdfTex时,它会失败。
下面是故障点的行:
pdfcrop --ini --verbose --bbox "0 0 1000 600" --margins "-490 10 10 10" ${tempDir}$1 ${tempDir}right.pdf我记录了详细的输出,并获得了以下内容:
nginx
PDFCROP 1.40, 2020/06/06 - Copyright (c) 2002-2020 by Heiko Oberdiek, Oberdiek Package Support Group.
* PDF header: %PDF-1.5
* Running ghostscript for BoundingBox calculation ...
GPL Ghostscript 9.25 (2018-09-13)
Copyright (C) 2018 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 2.
Page 1
%%BoundingBox: 90 83 972 571
* Page 1: 0 0 1000 600
%%HiResBoundingBox: 90.674997 83.069997 971.999970 570.725983
Page 2
%%BoundingBox: 33 23 969 572
* Page 2: 0 0 1000 600
%%HiResBoundingBox: 33.731999 23.939999 968.147970 571.697983
* Running pdfTeX ...第一行'nginx‘是因为我记录了whomami的结果,以确认哪个用户正在运行脚本。请注意,该脚本仅在pdfTex调用时停止。
同样,当从命令行以root用户身份运行时,该脚本可以正常工作。pdfTex似乎对nginx用户不可用。如果是这种情况,我该如何修复它呢?
提亚
编辑:考虑到问题可能是权限问题,pdfTex无法写入其临时文件,我将运行脚本的目录的所有者和组更改为nginx。结果是一样的。
编辑2:下面是对我的脚本的PHP调用:
chdir($scriptDir);
$result = shell_exec('./friedensBulletin.sh' . ' ' . $bulletinName . ' ' . $bulletinName);
chdir($cwd);$scriptDir是我的脚本所在的位置。将$cwd设置为当前工作目录,然后在此处重置。
编辑3:整个bash脚本
#!/bin/bash
#################################################
# takes a PDF, crops, exports as html #
# req. pdfcrop for cropping #
# req. poppler (pdftohtml) for file conversion #
# $1 input file #
# $2 output file #
# author: roger@rogercreasy.com #
# 01.09.2021 #
#################################################
tempDir="tmp/"
# handle pages 1 and 3
pdfcrop --ini --bbox "0 0 1000 600" --margins "-490 10 10 10" ${tempDir}$1 ${tempDir}right.pdf
pdfseparate ${tempDir}right.pdf ${tempDir}right%d.pdf
#handle pages 2 and 4
pdfcrop --ini --bbox "0 0 1000 600" --margins "10 10 -490 10" ${tempDir}$1 ${tempDir}left.pdf
pdfseparate ${tempDir}left.pdf ${tempDir}left%d.pdf
#recombine in the correct order
pdfunite ${tempDir}right1.pdf ${tempDir}left2.pdf ${tempDir}right2.pdf ${tempDir}left1.pdf ${tempDir}tmp.pdf
mv ${tempDir}tmp.pdf $2
# clean up uneeded files
rm ${tempDir}*.pdf发布于 2021-02-08 07:02:48
我最初的理论是pdfTex对nginx用户不可用,这是正确的。
在我的脚本中,我记录了which pdftex的结果。此命令返回未找到。解决方案是创建一个指向pdftex脚本的符号链接。我通过将以下代码添加到我的脚本中实现了这一点。
if ! [[ -L "pdftex" ]]; then
ln -s /bin/pdftex pdftex
fi这将检查链接是否存在,如果不存在,则创建它。当然,假设pdftex总是安装在相同的位置,这种方法允许我的脚本在移动到另一台服务器时工作。我通过在命令行上以root用户身份运行‘`which pdftex’找到了pdftex的位置。
感谢pdfcrop的作者Heiko Oberdiek帮助解决了这个问题。
https://stackoverflow.com/questions/66044811
复制相似问题