Pdf 为什么此PostScript/PS文件创建的上边距比指定的多?

Pdf 为什么此PostScript/PS文件创建的上边距比指定的多?,pdf,ghostscript,postscript,plaintext,Pdf,Ghostscript,Postscript,Plaintext,PS脚本获取一个纯文本文档并从中生成PDF。非常感谢@RedGrittyBrick挖掘了这段代码: %! % % 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 a

PS脚本获取一个纯文本文档并从中生成PDF。非常感谢@RedGrittyBrick挖掘了这段代码:

%!
%
% 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
它有:

/topmargin 1 inch def
/leftmargin 1 inch def
但它在视觉上看起来像文件中所说的顶部边距是4英寸,而不是1英寸。如果我将其修改为0,则完成的PDF在视觉上似乎有1英寸的上边距。另一方面,如果我将leftmargin修改为0英寸,它将一直延伸到左边框。这对我来说毫无意义

我使用SamutraPDF打开PDF文件

在我看来,它的视觉效果是正确的,在上/右/下/左上有适当的、均匀的边距,是:

/topmargin 0 inch def
/leftmargin 0.8 inch def
根据代码,这看起来应该是错误的,但它看起来是正确的

这让我很担心。其他人是否会看到我的文档中有混乱的页边距?苏门答腊岛是否在做一些非标准的事情?它有bug吗?发生了什么事?默认情况下,PDF是否为所有文档添加了不可见的上边距


我必须承认我不懂那个PostScript文件中的任何语言。在某一点上,它提到了“500”,这似乎奇怪地具体。但我的问题实际上是关于“看不见的最高利润率”。为什么会这样?我做错了什么?根据给我剧本的人的说法,剧本为什么不能产生完美的边距?他声称已经在各种环境中使用它很长时间了,所以我不知道该怎么做。

在我的Linux上使用ps2pdf和ghostscript 9.52时,这个1992年的postscript程序可以正常工作。页面高度为11英寸,上边距为1英寸,因此第一行的起始高度为10英寸。我已经用字母大小测试了这一点,除了文本应该在10英寸位置开始,即使纸张大小不同。也许苏门答腊PDF真的与众不同,或者尝试一下:


后记开始阅读“循环”后第一行的文本。那一行的内容应该在第10行“身高。如果文本在第一行之后开始,或者如果有许多空行,则pdf在打印之前将有那么多空行。边距与postscript指定的相同,因此您的输入必须是在您自己的文本开始之前打印的空行。确保循环和要打印的文本之间没有空行。

在我的Linux上使用ps2pdf和ghostscript 9.52时,这个1992年的postscript程序可以正常工作。页面高度为11英寸,上边距为1英寸,因此第一行的起始高度为10英寸。我已经用字母大小测试了这一点,除了文本应该在10英寸位置开始,即使纸张大小不同。也许苏门答腊PDF真的与众不同,或者尝试一下:


后记开始阅读“循环”后第一行的文本。那一行的内容应该在第10行“身高。如果文本在第一行之后开始,或者如果有许多空行,则pdf在打印之前将有那么多空行。边距与postscript指定的相同,因此您的输入必须是在您自己的文本开始之前打印的空行。确保循环和要打印的文本之间没有空行。

我通读了这个程序,我认为可以安全地修改它以使用环境定义的页面参数

您只需注释掉硬编码页面大小的行:

%/pageheight 11 inch def
并将其替换为从页面设备获取高度的这一行

/pageheight currentpagedevice /PageSize get 1 get def
或者此版本使用剪辑路径的边界框

/pageheight newpath clippath pathbbox 4 1 roll pop pop pop def
请注意,此程序不支持任何形式的文字包装或文档布局。
文本必须有明确的换行符。

我通读了这个程序,我认为可以安全地修改它,以使用环境定义的页面参数

您只需注释掉硬编码页面大小的行:

%/pageheight 11 inch def
并将其替换为从页面设备获取高度的这一行

/pageheight currentpagedevice /PageSize get 1 get def
或者此版本使用剪辑路径的边界框

/pageheight newpath clippath pathbbox 4 1 roll pop pop pop def
请注意,此程序不支持任何形式的文字包装或文档布局。
文本必须有明确的换行符。

此PostScript说明其页面为11英寸高,但未设置该值。PS解释器正在使用另一个默认大小。它可能为8.5x14英寸,但也可能是一个完全不同的值,如A4。你必须检查它的设置。@usr2564301我以为PDF的全部意义在于你总是得到1:1的副本?操作系统/计算机/环境/设置没有差异?你说的“它没有设定那个值”是什么意思?您有什么建议?此文件中没有设置最终页面大小的内容,因此将使用PS解释器的默认大小。您的维度与编写此代码时使用的默认维度不同。这就是为什么页边距看起来是关闭的。@usr2564301好吧,我应该做些什么来给它指定一个页面大小呢?露西娜,你是从PostScript开始的,而不是从PDF开始的。PostScript是一种编程语言,您拥有的是一个程序,当您运行它时,会发生一些事情。当你用它制作PDF时,它(有点)会制作一个冻结版本。PDF的媒体大小是固定的,但我们应该使用什么大小?您的程序没有请求大小(这是一个请求,因为打印机可能没有您想要的大小,然后它会做什么取决于策略,它可能会使用更大和中心,更小和裁剪,放弃并请求帮助等)。既然你没有;如果不请求媒体大小,您将获得解释器的默认值。此PostScript声明其页面高11“,但未设置该值。您的PS解释器正在使用另一个默认大小。它可能是8.5x14”,但也可能是一个完全不同的值,如A4。你必须检查它的设置。@usr2564301我以为PDF的全部意义在于你总是得到1:1的副本?操作系统/计算机/环境/设置没有差异?那你怎么办