打印没有边框(或页边距)的PDF时,打印机将在纸张的边缘切除大约1毫米的图像数据。 因此,我正在寻找一种解决scheme来在页面上稍微缩放/调整一个pdf页面的大小,以便在与打印机生成的边缘上的空白对应的边缘处添加一个白色边框。
我已经尝试使用gs
到目前为止..例如,假设我有一个A4大小的pdf 1.pdf
,然后我用:
gs -sDEVICE=pdfwrite \ -q -dBATCH -dNOPAUSE \ -dPDFFitPage \ -r300x300 \ -g2232x3157 \ -sOutputFile=1A.pdf \ 1.pdf
在这里,一个完整的A4文件是由-g2480x3508
给出的,我试图乘以0.9乘以比例,但是我没有看到这个效果。
这里是建立在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%, eg) # 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 -es/\.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
有关此方法的更多信息以及关于此主题的讨论可在以下博客文章中找到:
似乎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
NiceHåkonHæ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}
由于您没有指定您感兴趣的特定工具,我会使用iText来完成这样的任务。 您可以使用Java或.NET编写简单的代码(iTextSharp)来轻松完成此任务。 用这个作为灵感( n-up工具 )。 虽然实际上是将多个文档页面放在单个页面中,但您可以采用此代码以相同的方式稍微缩放单个页面。