1

The PS script takes a plaintext document and produces a PDF from it. A big thank you to @RedGrittyBrick for digging up this snippet:

%!
%
% From: Jonathan Monsarrat ([email protected])
% 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

It has:

/topmargin 1 inch def
/leftmargin 1 inch def

But it visually looks like the top margin is like 4 inches and not 1 inch as it says in the file. If I modify it to 0, the finished PDF visually appears to have 1 inch top margin. If I, on the other hand, modify the leftmargin to 0 inch, it goes all the way to the left border. This makes no sense to me.

I use SamutraPDF to open PDF files.

The way it visually looks right to me, with proper, even margins on top/right/bottom/left, is:

/topmargin 0 inch def
/leftmargin 0.8 inch def

This, according to the code, should look all wrong, but instead it looks correct.

This makes me very worried. Will everyone else see my documents with messed up margins? Is this somehow SumatraPDF doing something non-standard? Does it have a bug? What is going on? Does PDF have invisible top margin added for all documents by default?

I must admit that I don't understand anything of the language in that PostScript file. At one point, it mentions "500", which seems oddly specific. But my question really is about the "invisible top margin". Why does it happen? What am I doing wrong? Why doesn't the script which, according to the guy who gave it to me, produce perfect margins? He claims to have used it for a very long time in all kinds of environments, so I don't know what to make of this.

9
  • This PostScript states that its page is 11" high but it does not set that value. Your PS interpreter is using another default size. It might be 8.5x14" instead but could also be an entirely different value, such as A4. You'll have to check its settings.
    – Jongware
    Jan 28, 2020 at 13:58
  • @usr2564301 I thought the whole point of PDF was that you always get a 1:1 copy? No difference in OSes/computers/environments/settings? And what do you mean by "it does not set that value"? What do you suggest?
    – user12798369
    Jan 28, 2020 at 14:11
  • There is nothing in this file that sets a definitive page size, so the default size of the PS interpreter will be used. Yours has other dimensions than the default that was used when this code was written. That's why margins seem off.
    – Jongware
    Jan 28, 2020 at 14:15
  • @usr2564301 Well, what do I change to give it a specified page size?
    – user12798369
    Jan 28, 2020 at 15:14
  • 2
    Lucina, you are starting from PostScript not PDF. PostScript is a programming language, what you have there is a program, when you run it, stuff happenms. When you make a PDF from it it (sort of) makes a frozen version of it. The Media size of the PDF is fixed, but what size should we use ? Your program does not request a size (its a request because the printer may not have the size you want, what it does then is up to the Policy, it may use larger and centre, smaller and crop, give up and ask for help, etc). Since you don;t request a media size, you get the default of the interpreter.
    – KenS
    Jan 28, 2020 at 17:11

2 Answers 2

0

This 1992 postscript program works properly when using ps2pdf with ghostscript 9.52 with my Linux. The page height is 11" and the top margin is 1" so the first line does start at 10" height. I have tested this with the letter size except the text should start at the 10" position even with different paper sizes. Maybe SumatraPDF really is different or try this:

The postscript begins reading the text on the first line after the "loop". The contents of that line should be at the 10" height. If the text begins after the first line or if there are a number of blank lines then the pdf will have that many blank lines before the printing. The margin is the same as specified by the postscript so your input must be empty lines being printed before the beginning of your own text. Make sure there are no blank lines between the loop and your text to be printed.

0
0

I read through this program and I think it can safely be modified to use the page parameters defined by the environment.

All you need to do is comment out the line that hard codes the page size:

%/pageheight 11 inch def

And replace it with this line which gets the height from the page device,

/pageheight currentpagedevice /PageSize get 1 get def

Or this version which uses the bounding box of the clip path,

/pageheight newpath clippath pathbbox 4 1 roll pop pop pop def

Note this program doesn't so any sort of word-wrapping or document layout. The text must have explicit newlines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.