Bash PDF 合并丢失文件

标签 bash pdf ghostscript

我正在尝试将许多 PDF 文件合并为 3000 个左右的文件 block 。经过多次尝试,这个脚本似乎成功了。 (当然我错了)

#!/bin/bash

basepath='/home/lemonidas/pdfstuff';
datename=`date "+%Y%m%d%H%M.%S"`;
start=`date "+%s"`;
echo "parsing pdf list to file..."
find $basepath/input/ -name "*.pdf" | xargs -I {} ls {} >> $basepath/tmp/biglist$datename.txt

split -l 3000 $basepath/tmp/biglist$datename.txt $basepath/tmp/splitfile
rm $basepath/tmp/biglist$datename.txt
echo "deleting big file..."
echo "done splitting!"

declare -i x 
x=1

for f in $basepath/tmp/splitfile*
do
linenum=`cat $f | wc -l`;
echo "Processing $f ($linenum lines)..."

# merge to one big PDF
cat $f | xargs  gs -q -sstdout=$basepath/error.log -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$basepath/output/$x.big.pdf  2>$basepath/error.log
echo "Completed PDF $x"
 (( x++ ))

# delete the list file
rm $f
echo "Deleted processed file $f"
done
end=`date "+%s"`;
echo "Started: $start"
echo "Finished: $end"

问题是,我有 22000 个 2 页的 PDF,每个输出文件(最后一个除外)应该是 6000 页(因为每个合并列表中有 3000 个 PDF,在解析之前通过“wc -l”进行验证) ),我只得到了大约 658 页左右。

除 gs 之外没有报告任何错误:

Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
Warning: Embedded symbolic TT fonts must contain a cmap for Platform=1 Encoding=0.
This file had errors that were repaired or ignored.
The file was produced by: >>>> Powered By Crystal Please notify the author of the software that produced this file that it does not conform to Adobe's published PDF specification.

一遍又一遍(但不是 22000 次)

当我尝试处理 300-400 个文件时,它运行顺利,但是当我在 2.5 小时后尝试完整运行时,我合并的文件远少于一半。

我的下一个想法是将每个 2 页 PDF 转换为 .pgm 文件,但我不知道如何将它们重新制作为 PDF(这样就不会出现字体嵌入问题)。 我错过了什么吗? (可能)

最佳答案

您可能会更好地使用更适合该任务的工具。我认为 pdfwrite(用于生成 PDF 文件的 Ghostscript 设备)不是合适的工具。

为了“合并”PDF 文件,Ghostscript 将输入完全解释为标记操作,然后将标记操作重写为 PDF 文件。在创建操作列表时,需要保存大量信息(字体、图像、其他内容),并与新输入进行比较,看看我们是否已经有了副本。随着输入变大,扫描该列表需要更长的时间,当然内存消耗也会增加。您可能会发现 Ghostscript 已经在交换内存。

现在我不确定这是您的实际问题,或者您是否说“合并”文件后缺少页面。那不应该发生。您也没有说明您使用的 Ghostscript 版本。

尽管如此,我认为像 pdftk 这样的工具在进行这种合并时会更快,尽管最终的 PDF 文件很可能比 pdfwrite 更大/效率更低。

关于Bash PDF 合并丢失文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13721326/

相关文章:

image - 如何使程序在 PostScript 文件的顶部覆盖文本?

Python Elaphe - 条码生成问题

python - 使用 awk 系统将特殊字符输入 python sys.argv

python - 无法使用 PyPDF2 打开 PDF 文件

php - Zend 框架 : How can I tap into the final view output before it's sent to the browser?

java - 如何使用java中的iText在pdf中添加页眉和页脚?

pdf - 将 pdf 正确转换为 ps,反之亦然

linux - 连续打印方式

bash - 通过 ssh 运行交互式 Bash 命令

linux - "can' t 读取文件夹”调用 sed 时出错