2012-04-19 71 views
20

一个PDF我尽量拆分使用Ghostscript多页PDF,我发现同样的解决方案上更多的网站,甚至ghostscript.com,即:分割使用Ghostscript

gs -sDEVICE=pdfwrite -dSAFER -o outname.%d.pdf input.pdf 

但它似乎不是为我工作,因为它会生成一个文件,所有页面均为 ,并且名称为outname.1.pdf

当我添加开始和结束页面时,它工作正常,但我 希望它在不知道这些参数的情况下工作。

在GS-devel的档案,我发现了一个解决方案: http://ghostscript.com/pipermail/gs-devel/2009-April/008310.html - ,但我觉得这样做没有pdf_info

当我使用不同的设备,例如pswrite,但同样 参数,它工作正常,生产尽可能多的PS文件,作为我 input.pdf包含。

使用pdfwrite时是否正常?难道我做错了什么?

回答

8

您看到的是“正常”行为:当前版本的Ghostscript的pdfwrite输出设备不支持此功能。这也是(无可否认,不知何故隐约)记录在Use.htm

“但请注意,每个文件功能的一个页面可能并非所有设备的支持......”

我似乎记得,在IRC上提到的Ghostscript开发者之一,他们可能将此功能添加到pdfwrite在未来的版本中,但它似乎需要一些主要的代码重写,这就是为什么他们还没有做到这一点...


更新:作为戈登的评论已经暗示,作为version 9.06(于2012年7月31日发布),Ghostscript的现在支持命令行中的问题也引述了pdfwrite。 (Gordon必须在9.05中已经发现了非官方的支持,或者他从尚未被标记为9.06的预发布源编译自己的可执行文件)。

+0

呀,我读这条线,但我的那句“正常行为”希望意味着“是pdfwrite那些谁可能不支持的一个此功能?”你对这个IRC的记忆对我来说没问题,谢谢。 – zseder 2012-04-19 15:42:22

+3

对于在搜索中找到此答案的人:从9.05开始,每个文件一页就可以使用OP的命令工作。 – Grod 2012-07-04 19:05:51

+1

@Gordon:支持'-o out_%d.pdf'语法(将多页PDF分成单个文件每页)在9.06中正式发布。我在其他答案中已经暗示了这一点(f.e. * [将多页PDF文件分割为单页](http://stackoverflow.com/a/12744923/359307)*)。我忘了更新这个答案。感谢提示。 – 2012-11-26 14:21:18

15

我发现这个剧本由魏玛先生超好用wriiten:

#!/bin/sh 
# 
# pdfsplit [input.pdf] [first_page] [last_page] [output.pdf] 
# 
# Example: pdfsplit big_file.pdf 10 20 pages_ten_to_twenty.pdf 
# 
# written by: Westley Weimer, Wed Mar 19 17:58:09 EDT 2008 
# 
# The trick: ghostscript (gs) will do PDF splitting for you, it's just not 
# obvious and the required defines are not listed in the manual page. 

if [ $# -lt 4 ] 
then 
     echo "Usage: pdfsplit input.pdf first_page last_page output.pdf" 
     exit 1 
fi 
yes | gs -dBATCH -sOutputFile="$4" -dFirstPage=$2 -dLastPage=$3 -sDEVICE=pdfwrite "$1" >& /dev/null 

产地来源:http://www.cs.virginia.edu/~weimer/pdfsplit/pdfsplit

保存为pdfsplit.sh,看到奇迹发生。

PDFSAM也可以做这项工作。在Windows和Mac上可用。

+0

令人惊叹。我没有pdftk和psselect会失去一些pdf质量,但不是这样。 – Wok 2013-01-04 13:01:07

4
#!/bin/bash 
#where $1 is the input filename 

ournum=`gs -q -dNODISPLAY -c "("$1") (r) file runpdfbegin pdfpagecount = quit" 2>/dev/null` 
echo "Processing $ournum pages" 
counter=1 
while [ $counter -le $ournum ] ; do 
    newname=`echo $1 | sed -e s/\.pdf//g` 
    reallynewname=$newname-$counter.pdf 
    counterplus=$((counter+1)) 
    # make the individual pdf page 
    yes | gs -dBATCH -sOutputFile="$reallynewname" -dFirstPage=$counter -dLastPage=$counter -sDEVICE=pdfwrite "$1" >& /dev/null 
    counter=$counterplus 
done 
1

这里有一个简单的Python脚本,做它:

#!/usr/bin/python3 

import os 

number_of_pages = 68 
input_pdf = "abstracts_rev09.pdf" 

for i in range(1, number_of_pages +1): 
    os.system("gs -q -dBATCH -dNOPAUSE -sOutputFile=page{page:04d}.pdf" 
       " -dFirstPage={page} -dLastPage={page}" 
       " -sDEVICE=pdfwrite {input_pdf}" 
       .format(page=i, input_pdf=input_pdf))