I'm developing a printer emulator (partly) in PostScript. The command system I want to emulate is designed for roll paper thermal printers, mostly used for printing Point of Sale receipts. I'd prefer to execute the operations in the same order as the real printer would do, which means my program doesn't know the page size until it encounters a "cut" command. I want the program to work with variable paper size CUPS printers¹.
Can I change the page height after I issued painting/show commands without the filled parts of the document disappearing?
I have tried modifying the page device dictionary at the end of the document, but if I change the PageSize array, everything on the document disappears.
For example if I run the following program:
<< /PageSize [ 100 30 ] >> setpagedevice
0 0 moveto
(Text) show
showpage
I get the output:
But when I modify the code to adjust the page size right before the showpage command:
<< /PageSize [ 100 30 ] >> setpagedevice
0 0 moveto
(Text) show
<< /PageSize [ 100 100 ] >> setpagedevice
showpage
I only get a blank image:
I'm aware I can defer the execution of the painting/show operators, so my program calculates the document size before painting, and only executes the operations when it encounters the cut command. I would be able to implement that by myself, I don't need help with such a solution currently. I'm rather wondering, whether a simpler solution is possible for cutting an already drawn document to a calculated page size.
You cannot use marking operations and then select the page size in PostScript. Setting the media size in PostScript executes an implicit erasepage which clears any marks on the page.
See the note on page 408 of the 3rd edition PLRM (Section 6.1.1 PageDevice Dictionary):
Note: setpagedevice is a page-oriented operator used to control the output processing of one or more pages of a page description. Any call to setpagedevice implicitly invokes erasepage and initgraphics, and thus must precede the descriptions of the pages to be affected.