【问题标题】:pdftex fails in a bash scriptpdftex 在 bash 脚本中失败
【发布时间】:2021-02-04 11:36:43
【问题描述】:

我有一个 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' 是因为我记录 whoami 的结果以确认哪个用户正在运行脚本。请注意,脚本仅在 pdfTex 调用处停止。

同样,当从命令行以 root 身份运行时,脚本会正常工作。 nginx 用户似乎无法使用 pdfTex。如果是这种情况,我该如何解决?

TIA

编辑:认为问题可能是权限,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

【问题讨论】:

  • root 运行此程序简直是疯了。
  • @tripleee 用于测试?为什么?
  • 我猜周围的 PHP 代码做错了什么。你能检查一下它究竟最终执行的是什么命令吗?
  • 测试为root 去除了大量可能的失败场景,显然也引入了一些安全问题。你不希望 TeX 能够擦除你的硬盘驱动器,即使你相信它暂时不去尝试。 (如果你通过它输入你没有完全控制权,你不应该。)
  • 更一般地尝试shellcheck.net,这至少会抱怨未引用的变量。

标签: bash pdf ghostscript tex-live pdftex


【解决方案1】:

我最初认为 nginx 用户无法使用 pdfTex 的理论是正确的。

在我的脚本中,我记录了which pdftex 的结果。此命令返回未找到。解决方案是创建一个指向 pdftex 脚本的符号链接。我通过将以下内容添加到我的脚本中来做到这一点。

if ! [[ -L "pdftex" ]]; then
    ln -s /bin/pdftex pdftex
fi

这会检查链接是否存在,如果不存在则创建它。如果移动到另一台服务器,这种方法允许我的脚本工作,当然假设 pdftex 总是安装在同一位置。我通过在命令行上以 root 身份运行 `which pdftex' 找到了 pdftex 的位置。

感谢 pdfcrop 的作者 Heiko Oberdiek 帮助解决这个问题。

【讨论】:

    猜你喜欢
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2020-08-11
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多