I'm trying to convert a plaintext document to PDF. The only method which has come anywhere close to actually working is installing "GhostScript" and then using the following PostScript script, dug up by the SE user @RedGrittyBrick (thanks), which takes a plaintext document (underneath the script) and produces a PDF from it.
It technically works, but visually messes up the top and left margins for each page in such a manner that the top margin becomes "way too much" and the left margin is "a bit too for in" (compared to the right margin). At least when viewed in SumatraPDF, which is the only PDF viewer I have.
The script states:
/topmargin 1 inch def
/leftmargin 1 inch def
However, it visually looks like the top margin is maybe 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.
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
But I can't just keep it like that as it will more than likely break on others' computers/PDF viewers. And even if it doesn't, I still really bugs me that I don't understand what is going on.
I have been told that the reason for this happening is that the PostScript does not specify a "page size". However, I have no idea how I would specify this into the document, nor how it can be possible that the author of the script never did this in the first place. It seems like such a basic, major mistake, yet the person who gave it to me claims to have used it for many years in many different environments successfully, so what does that mean? That SumatraPDF has very exotic default settings? That the person in question has very low standards? That I'm going insane? I really don't know what to make of this, or how to fix it.
I thought the whole point of PDF was to always create a 1:1 copy, with no ambiguity whatsoever in the dimensions and how things will be rendered... Apparently not. This is the script:
%!
%
% 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
I then run:
ps2pdf in.ps out.pdf



The easy way to turn a 'plaintext document' into PDF is to open the document in your favourite text editor and then 'save as PDF' or 'Print to PDF' from there. It's far more reliable than trying to use an ancient PostScript program which (as is clearly demonstrated by the fact it doesn't work for you) is lacking in features. Recent versions of Linux, Windows and Mac all have this capability and avoids the kind of problems you are seeing.
Instead of assuming the media size to be 11 inches, the program should interrogate the interpreter to discover the current media size and use that. Or as I replied to your earlier question here the program should request a given media size from the interpreter. As I said previously, you need to add something like:
Where the numbers in the array, delimited by '[]' are the requested width and height, in points (1/72 inch). Obviously you need to put that in the program somewhere before the main loop. The
setpagedeviceoperator initilises the graphics state and erases the page, so make sure you do that before drawing anything.The request above is, obviously, for US Letter media which is 11 inches long, as your program expects.
You keep on stating that PDF is supposed to avoid ambiguity and yes, it does, because a PDF file has a media size in it. But what you have here is not a PDF file, it's a PostScript program.
The PostScript program need not (and in your case does not) request a media size, it can simply use whatever the interpreter has as a default. For example; printers in the US normally have US Letter, printers in Europe have A4. So when you run your PostScript program it uses whatever default is current. In the US your program would likely result in a PDF file which uses US Letter, in Europe it would probably be A4 and the PDF file you produce by running the program would therefore use A4. I would imagine this is why your experience differs from whoever gave you the program in the first place, your environments differ.
The name /topmargin is not magic, it's just a variable name. I don't know what programming languages you are familiar with, but if I created a local variable called topmargin in C++ I wouldn't expect it to have any effect on my program just because it was called topmergin.
But this is nothing to do with PDF, it's a consequence of running the program in two different environments. Each of the PDF files you create will be consistent, no matter which PDF viewer you choose to use, but if the two files are created with two different media sizes then the two files will look different.