实现字符串 -> PDF 图章工具

实现字符串 -> PDF 图章工具

我编写了一个小 shell 函数,它使用字符串从字符串创建固定大小的 PDF 文件(A4)gs:

make_stamp() {
file=$1
string=$2
tmp=${file}-small.pdf
gs -o "$tmp" -sDEVICE=pdfwrite -g500x200 \
  -c "/Helvetica findfont 12 scalefont setfont" \
  -c "0 1 0 0 setcmykcolor" \
  -c "0 5 moveto" \
  -c "(${string}) show" \
  -c "showpage"
gs -o "$file" -sDEVICE=pdfwrite -g5950x8420 \
  -c "<</PageOffset [510 20]>> setpagedevice" \
  -f "$tmp"
}

不过,有几点我想改进:

  1. 创建时$tmp如何设置纯色背景?
  2. 创建时$tmp,是否可以自动计算大小以紧密围绕文本,也许有一些pt作为填充?
  3. 是否可以重写此函数以仅调用gs一次?

或者

  • 还有另一种不直接使用的方法吗gs?印记文件必须是文本的,渲染的图像是不好的。

$stamp对于任何感兴趣的人,我在调用中使用此函数的输出,pdftk如下所示:

pdftk original.pdf stamp $stamp output stamped.pdf

答案1

我最近卷入了一个法律事务,为此我编写了一个 PDF“Bates-stamping”脚本,pdfBatesStamp.sh.

用法摘录

# "Bates-stamp" a PDF file with text (only; images aren't supported).  Uses
# ghostscript (ps2pdf) and pdftk.
#
# The output (Bates-stamped) file is put in the same directory, with "_BATES"
# appended to its name, thusly:
#     pdfBatesStamp.sh <FILE>.pdf ==> <FILE>_BATES.pdf
#
# Usage:
#     pdfBatesStamp.sh <FILE>.pdf [PREFIX(def=<FILE>)] [STARTNUM(def=1)]
#     pdfBatesStamp.sh <FILE>.pdf BATESCONFIG=<bates_config_filename>
#
# The <FILE>.pdf name must end in ".pdf".  You can make many more settings
# inline below (you can also set PREFIX and STARTNUM there too if you want).
# The first invocation format above is for the most common case (e.g., for legal
# use).  In the second invocation format, the <bates_config_filename> file can
# contain any of the inline user-settings (below, from PREFIX to EXTRAS,
# inclusive), and they will overwrite the inline settings.  In this way, you can
# write/store special config file settings for special PDFs, without needing to
# modify the inline settings each time.  Runs at ~3 pages/sec.

完整脚本可从pastebin下载,pdfBatesStamp.sh

相关内容