缩放pdf以添加边框以打印完整尺寸的页面 [英] Scale pdf to add border for printing full size pages
问题描述
在打印无边界(或空白)的pdf时,打印机会在纸张边缘切掉大约1mm的图像数据.因此,我正在寻找解决方案 在页面上略微缩放/调整pdf页面的大小,以在边缘添加白色边框,该边框与打印机产生的边缘的空白相对应.
When printing a pdf with no border (or margins), the printer choppes off around 1mm of the image data at the edges of the paper. I am therefore looking for a solution to scale/resize a pdf page slightly on the page to add a white border at the edges that will correspond with the white space at the edges produced by the printer.
到目前为止,我已经尝试使用gs
.例如,假设我有一个A4尺寸的pdf 1.pdf
,那么我使用了:
I have tried using gs
so far.. For instance, suppose i have an A4 size pdf 1.pdf
, then I used:
gs -sDEVICE=pdfwrite \
-q -dBATCH -dNOPAUSE \
-dPDFFitPage \
-r300x300 \
-g2232x3157 \
-sOutputFile=1A.pdf \
1.pdf
在这里,-g2480x3508
给出了一份完整的a4纸,我尝试将其乘以0.9来缩放,但是我看不到任何效果.
Here, a full a4 paper is given by -g2480x3508
and I have tried to multiply by 0.9 to scale, but I do not see any effect of this..
推荐答案
下面是基于prev构建的bash脚本的要点.修复了颜色兼容性问题(可能特定于我的pdf),并进行了一些依赖检查:
Here's a Gist of a bash script that builds on the prev. Fixes a color compatibility problem (possibly specific to my pdf), and does some dependency checking:
#!/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
有关此方法的更多信息以及对此要点的讨论,请参见这篇博客文章:
More information about this method and a discussion of this gist is availanle on this blog post:
- 如何缩放PDF文件的页面内容?(2008年8月;作者:马特)
- How to scale the page content of PDF files? (Aug 2008; by Matt)
请参见 tavinus/pdfScale ,它是一个带有其他功能的叉子.
See tavinus/pdfScale, it's a fork with some other features added to it.
这篇关于缩放pdf以添加边框以打印完整尺寸的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!