2013-08-20 103 views
3

当打印没有边框(或页边距)的pdf时,打印机在纸张边缘切除大约1毫米的图像数据。因此,我正在寻找解决方案 缩放/在页面上稍微调整一个pdf页面的大小,以便在与打印机生成的边缘处的白色空间相对应的边缘添加白色边框。缩放pdf以添加边框以打印全尺寸页面

我已经尝试使用gs到目前为止..例如,假设我有一个A4大小的PDF 1.pdf,然后我用:

gs -sDEVICE=pdfwrite \ 
    -q -dBATCH -dNOPAUSE \ 
    -dPDFFitPage \ 
    -r300x300 \ 
    -g2232x3157 \ 
    -sOutputFile=1A.pdf \ 
    1.pdf 

在这里,一个完整的A4纸是由-g2480x3508给出我曾尝试将其乘以0.9来缩放,但是我没有看到这种效果。

回答

1

由于您没有指定您感兴趣的特定工具,我会使用iText来完成这样的任务。您可以使用Java或.NET编写简单的代码(iTextSharp)来轻松完成此任务。以此为灵感(n-up tool)。虽然它实际上是将多页文档放入单页中,但您可以采用此代码以相同方式轻微缩放单个页面。

2

好像在 http://ma.juii.net/blog/scale-page-content-of-pdf-files 提供的解决方案以及在这里工作..

基础上的解决方案,我写了下面的bash脚本(scaleA4Pdf)用于按比例缩放A4 PDF文件的网页内容。现在,您可以只写scaleA4Pdf 10缩放页面10%..

#! /bin/bash 

if [ $# -ne 1 ] ; then 
    echo "Bad arguments!" 
    exit 
fi 

# assume 0<=$1<=100 (no error checks!) 
xx="595" #width of A4 in post script points 
yy="842" #height of A4 in pps 

ss=$(echo "scale=4; $1/2" | bc) 
sx=$(echo "scale=4; ${xx}"'*'"(${ss}/ 100)" | bc) 
sy=$(echo "scale=4; ${yy}"'*'"(${ss}/ 100)" | bc) 
s=$(echo "scale=4; 1 - $1/100" | bc) 
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \ 
    -dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \ 
    -dSubsetFonts=true -dEmbedAllFonts=true \ 
    -sPAPERSIZE=a4 -sOutputFile="1A.pdf" \ 
    -c "<</BeginPage{${s} ${s} scale ${sx} ${sy} translate}>> setpagedevice" \ 
    -f 1.pdf 
2

尼斯哈康Hægland! 我做了一些改进,以便轻松选择输入。

所以,如果你运行

$ scaleA4PDF 10 yourfile.pdf

,您将收到一条yourfile_scaled.pdf文件。

#! /bin/bash 
input=$2 
output=$(echo $2 | sed s/.pdf/_scaled.pdf/) 
if [ $# -ne 2 ] ; then 
echo "Bad arguments!" 
exit 
fi 

# assume 0<=$1<=100 (no error checks!) 
xx="595" #width of A4 in post script points 
yy="842" #height of A4 in pps 

ss=$(echo "scale=4; $1/2" | bc) 
sx=$(echo "scale=4; ${xx}"'*'"(${ss}/ 100)" | bc) 
sy=$(echo "scale=4; ${yy}"'*'"(${ss}/ 100)" | bc) 
s=$(echo "scale=4; 1 - $1/100" | bc) 
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \ 
-dCompatibilityLevel="1.3" -dPDFSETTINGS="/printer" \ 
-dSubsetFonts=true -dEmbedAllFonts=true \ 
-sPAPERSIZE=a4 -sOutputFile="${output}" \ 
-c "<</BeginPage{${s} ${s} scale ${sx} ${sy} translate}>> setpagedevice" \ 
-f ${input} 
+0

做得好.... :) –

+0

我得到了运行时出现以下错误:'GPL Ghostscript 9.10:将UseDeviceIndependentColor的UseCIEColor设置为正常工作。 不可恢复的错误:在.setdistillerparams中出现堆栈溢出。我通过用'-dPDFSETTINGS =“/ screen”'替换'-dPDFSETTINGS =“/ printer”'来解决它。我也使它更加奇怪的文件名称,其中包含空格。看我的[gist](https://gist.github。com/cipri-tom/7d482b0f3c7df470691c) –

3

这里是建立在prev之上的bash脚本的要点。修复了颜色兼容性问题(可能是具体到我的PDF格式),并做了一些相关性检查:

#!/bin/bash 

# pdfScale.sh 
# 
# Scale PDF to specified percentage of original size. 
# Ref: http://ma.juii.net/blog/scale-page-content-of-pdf-files. 

echo "This script doesn't handle files with spaces in them." 

SCALE=0.95 # scaling factor (0.95 = 95%, e.g.) 

# Validate args. 
[ $# -eq 1 ] || { echo "***ERROR: Usage pdfScale.sh <inFile>.pdf"; exit 99; } 
INFILEPDF="$1" 
[[ "$INFILEPDF" =~ ^..*\.pdf$ ]] || { echo "***ERROR: Usage pdfScale.sh <inFile>.pdf"; exit 99; } 
OUTFILEPDF=$(echo "$INFILEPDF" | sed -e s/\.pdf$// -).SCALED.pdf 

# Dependencies 
command -v identify >/dev/null 2>&1 || { echo >&2 "Please install 'imagemagick' (sudo apt-get install imagemagick). Aborting."; exit 1; } 
command -v gs >/dev/null 2>&1 || { echo >&2 "Please install 'ghostscript' (sudo apt-get install ghostscript ?). Aborting."; exit 1; } 
command -v bc >/dev/null 2>&1 || { echo >&2 "Please install 'bc' arbitrary precision calculator language. Aborting."; exit 1; } 

# Get width/height in postscript points (1/72-inch), via ImageMagick identify command. 
# (Alternatively, could use Poppler pdfinfo command; or grep/sed the PDF by hand.) 
IDENTIFY=($(identify $INFILEPDF 2>/dev/null)) # bash array 
[ $? -ne 0 ] &GEOMETRY=($(echo ${IDENTIFY[2]} | tr "x" " ")) # bash array — $IDENTIFY[2] is of the form PGWIDTHxPGHEIGHT 
PGWIDTH=${GEOMETRY[0]}; PGHEIGHT=${GEOMETRY[1]} 

# Compute translation factors (to center page. 
XTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGWIDTH" | bc) 
YTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGHEIGHT" | bc) 

echo $PGWIDTH , $PGHEIGHT , $OUTFILEPDF , $SCALE , $XTRANS , $YTRANS , $INFILEPDF , $OUTFILEPDF 

# Do it. 
gs \ 
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \ 
-dCompatibilityLevel="1.5" -dPDFSETTINGS="/printer" \ 
-dColorConversionStrategy=/LeaveColorUnchanged \ 
-dSubsetFonts=true -dEmbedAllFonts=true \ 
-dDEVICEWIDTH=$PGWIDTH -dDEVICEHEIGHT=$PGHEIGHT \ 
-sOutputFile="$OUTFILEPDF" \ 
-c "<</BeginPage{$SCALE $SCALE scale $XTRANS $YTRANS translate}>> setpagedevice" \ 
-f "$INFILEPDF" 

https://gist.github.com/MichaelJCole/86e4968dbfc13256228a

这个方法,并且此要点的讨论更多的信息是这个博客帖子availanle:

+1

尼斯:) ....... –

+0

虽然这个链接可能回答这个问题,但最好包含答案的基本部分[这里](http://meta.stackoverflow .com/a/8259)并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 – bummi

+0

如果您发现了这个要点,请转至此处。 –