Bash PDF合并错过文件

时间:2012-12-05 10:40:52

标签: bash pdf ghostscript

我正在尝试以3000个左右的文件块合并许多PDF文件。经过多次尝试,这个脚本似乎可以解决问题。 (当然我错了)

#!/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小时后,我得到的文件少于合并的一半。

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

1 个答案:

答案 0 :(得分:2)

您可能会更好地使用更适合该任务的工具。 pdfwrite(用于发出PDF文件的Ghostscript设备)在我看来不是正确的工具。

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

现在我不确定这是你的实际问题,或者如果你说'合并'文件后会丢失页面。这不应该发生。你也没有说你正在使用什么版本的Ghostscript。

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