gpt4 book ai didi

linux - 缩放 pdf 以添加边框以打印全尺寸页面

转载 作者:IT王子 更新时间:2023-10-29 01:13:44 24 4
gpt4 key购买 nike

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

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

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

这里,-g2480x3508 给出了一张完整的 a4 纸,我试图乘以 0.9 来缩放,但我没有看到任何效果..

最佳答案

这是建立在上一个基础上的 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

有关此方法的更多信息以及对此要点的讨论可在这篇博文中找到:

参见 tavinus/pdfScale ,它是一个添加了一些其他功能的分支。

关于linux - 缩放 pdf 以添加边框以打印全尺寸页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18343813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com