justbrown
justbrown

Reputation: 25

Postscipt: Reading a file, writing to pdf

I am trying to parse a .txt file using PostScript with the Ghostscript interpreter. The .txt files are log files of sorts that I need to extract a date stamp from. There will be a line in the file such as "date: [01-May-2011 06:41:52]". I am trying to simply output the "01-May-2011 06:41:52" part (without brackets etc) to a PDF.

My PostScript code:

/grabdate {


linehold ([) search {  pop pop           % get everything after brackets
(]) search   {exch pop exch pop          % get everything before brackets

== flush 

} {pop} ifelse
} {pop} ifelse
} def


/strrx 256 string def                    % temp line stash
/filename (C:\\path\\to\\file.txt) def

filename (r) file /myworkfile exch def   % name of opened file

{myworkfile strrx readline {/linehold exch store grabdate} 
{myworkfile closefile exit} ifelse

} loop

Using Ghostscript on the command prompt I issue the command:

gswin32c \
  -q \
  -dNOPAUSE \
  -dBATCH \
  -sOutputFile=out.pdf \
  -sDEVICE=pdfwrite myfile.ps

The PostScript code partially works, in that it outputs the correctly parsed "date" line to standard output (because of the == flush) but my problem is, I can't seem to get the "grabdate" operation to write that same "date" string onto a PDF, instead of to standard output. Is there a set of PostScript commands I can use to do this? What am I missing here?

Any help appreciated.

Upvotes: 2

Views: 193

Answers (1)

KenS
KenS

Reputation: 31199

The '==' operator specifically sends the output to stdout, it is not a 'marking operarator, that is it makes no marks on the output page.

Ghostscript reads PostScript and interprets the program, any marking operations are fed as graphics primitives to the output device, and the device decides what to do with it. In the case of pdfwrite it writes a PDF marking operation, equivalent to the PostScript one, into the output file.

Obviously, non-marking operators don't produce graphic primitives, and so they are not passed to the device.

If you want to have your 'grabdate' routine write text on the page then you will have to select a font, (possibly re-encode it) scale it, set the current point then emit the text using a show operator. You will have to keep track of the current point and if it moves outside the page boundary take action (reposition, or emit showpage and start a new page)

You should look at the PostScript operators; findfont, scalefont, selectfont, setfont, moveto (and its variant rmoveto) currentpoint, stringwidth, and the whole family of show operators (show, widthshow, ashow, awidthshow, cshow, kshow, xshow, yshow, xyshow, glyphshow).

Your current program won't 'work' (in the sense of producing any output) on many PostScript interpreters as it stands, because you never emit a showpage. You should do that too.

Upvotes: 3

Related Questions