pdf - 如何修改从 1992 年开始的纯文本到 PDF 转换 PostScript 以实际指定页面大小?
问题描述
我正在尝试将纯文本文档转换为 PDF。唯一接近实际工作的方法是安装“GhostScript”,然后使用由 SE 用户@RedGrittyBrick 挖掘的以下 PostScript 脚本(谢谢),该脚本采用纯文本文档(在脚本下方)并生成 PDF从中。
它在技术上是有效的,但在视觉上混淆了每一页的顶部和左侧边距,以至于顶部边距变得“太多”而左边距“有点太内”(与右边距相比)。至少在SumatraPDF中查看时,这是我唯一的 PDF 查看器。
该脚本指出:
/topmargin 1 inch def
/leftmargin 1 inch def
但是,从视觉上看,上边距可能是4英寸,而不是文件中所说的 1 英寸。如果我将其修改为 0,则完成的 PDF 在视觉上看起来有 1 英寸的上边距。另一方面,如果我将左边距修改为 0 英寸,它会一直到左边框。
它在我看来是正确的,在顶部/右侧/底部/左侧有适当的、均匀的边距,是:
/topmargin 0 inch def
/leftmargin 0.8 inch def
但我不能就这样保持下去,因为它很可能会在其他人的计算机/PDF 查看器上中断。即使它没有,我仍然真的很烦我,我不明白发生了什么。
有人告诉我,发生这种情况的原因是 PostScript 没有指定“页面大小”。但是,我不知道如何将其指定到文档中,也不知道脚本的作者怎么可能一开始就没有这样做。这似乎是一个基本的、重大的错误,但给我它的人声称已经在许多不同的环境中成功使用它多年,那是什么意思?SumatraPDF 有非常奇特的默认设置?那个人的标准很低吗?说我要疯了?我真的不知道该怎么做,或者如何解决它。
我认为 PDF 的全部意义在于始终创建一个 1:1 的副本,在尺寸和渲染方式上没有任何歧义......显然不是。这是脚本:
%!
%
% From: Jonathan Monsarrat (jgm@cs.brown.edu)
% Subject: PostScript -> ASCII *and* ASCII -> PostScript programs
% Newsgroups: comp.lang.postscript
% Date: 1992-10-01 04:45:38 PST
%
% "If anyone is interested, here is an interesting program written by
% Professor John Hughes here at Brown University that formats ASCII
% in PostScript without a machine generator of any kind."
%
%%%
%%% Plan:
%%% Start with an empty string.
%%% For each character in the input stream,
%%% check to see if it's a carriage return.
%%% if so, show the current string and reset it to empty
%%% if not, add it to the current string.
/Courier findfont 10 scalefont setfont %% Choose a fixed width font
/lineheight
currentfont /FontBBox get dup %% bbox bbox
0 2 getinterval %% bbox {xm ym}
exch %% {xm ym} bbox
2 2 getinterval %% {xm ym} {xM yM}
aload pop %% {xm ym} xM yM
3 2 roll %% xM yM {xm ym}
aload pop
currentfont /FontMatrix get %% xM yM xm ym MAT
transform %% xM yM xm' ym'
4 2 roll
currentfont /FontMatrix get %% xm' ym' xM yM MAT
transform %% xm' ym' xM' yM'
exch pop %% xm' ym' yM'
sub %% xm' ym'-yM'
exch pop %% dy
neg def
lineheight pstack pop
/str 500 string def %% Room to store a long string...
/empty 500 string def %% An empty string to work with
/stringindex 0 def %% How far we've filled the string
/inch {72 mul } def %% A useful tool...
/pageheight 11 inch def
/topmargin 1 inch def
/botmargin 1 inch def
/leftmargin 1 inch def
/linesperpage pageheight topmargin sub botmargin sub lineheight div cvi def
/linenumber 1 def %% the line we're about to write on
/newline { %% move to a new line; flush page if necessary
linenumber linesperpage gt {/linenumber 1 def showpage } if
leftmargin pageheight topmargin sub linenumber lineheight mul sub moveto
/linenumber linenumber 1 add def
} def
/cleanup { %% print out the last bit of whatever you had there...
str show showpage
} def
/startstring { %% empty the string and reset its counter.
str 0 empty putinterval
/stringindex 0 def
} def
/showstring { %% print the string on a new line and flush it
newline
str show
startstring
} def
pstack
/addtostring { %% put another character in the string, if there's room
dup 500 gt {pop}{str exch stringindex exch put
/stringindex stringindex 1 add def} ifelse
} def
%
% Main program: get characters and deal with them
%
{
currentfile read {}{cleanup exit} ifelse
dup 10 eq %% if it's a carriage return...
{pop showstring} %% write out this line of text and start over
{dup 0 eq %% if it's an end-of-file mark...
{exit} %% stop!
{addtostring} %% otherwise, add the character to current string
ifelse}
ifelse %% Sample data follows.
} loop
然后我运行:
ps2pdf in.ps out.pdf
解决方案
由于您有 ghostscript 并希望自动执行此操作:
#!/bin/sh
exec gs -q -sDEVICE=pdfwrite -sPAPERSIZE=letter -dNOSAFER -dNOPAUSE -sOutputFile=$1.pdf -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$@"
请参阅 gslp 手册页以获取一些适用的帮助。
编辑:当只转换一个文本文件时,这也适用于 gs 9.50 及更高版本的 -dNOSAFER:
#!/bin/sh
exec gs -q -sDEVICE=pdfwrite -sPAPERSIZE=letter --permit-file-read="$1" -dNOPAUSE -sOutputFile=$1.pdf -sPROGNAME=$0 -- gslp.ps --heading-center "`date`" "$1"
推荐阅读
- bash - 根据标准输出替换退出代码?
- reactjs - 反应语义 UI 菜单
- javascript - 如何使用 React 将点击事件分配给画布 Api 中的每个圆圈
- javascript - 如何使用更漂亮的配置改进代码格式
- mysql - 使用新列中的前一行值计算每周增长
- php - “while($row = mysqli_fetch_array($result))” 可能为空?
- javascript - 调用包含来自另一个函数的承诺的函数
- python - 如何使用 Pandas 根据另一列的值将两列相加
- splunk - Splunk:使用多个统计信息搜索 SPL
- java - 为什么这个 Java 函数总是返回 true?