Skip to main content
kolonuk
Participant
September 4, 2014
Question

Same image multiple times in one document

  • September 4, 2014
  • 2 replies
  • 1175 views

Hi there,

I'm a software developer, and currently the system I'm working on produces postscript documents for invoices/orders etc. and I'm looking for a way to reduce the size of the postscript file.  One simply way would be to re-use the same image (company logo, for example), rather than specifying it again for every page.

The images are produced using the linux tool ps2pdf - the output is then chopped up so it literally only has the image producing code, and then dumped as-is into the document where the image is required.  What I'd like to do is put this postscript image into a procedure, then call it with the height, width, x and y position whenever I need it.  Doing this will drastically reduce down the PS file from MB sizes to KB sizes.

This is an example of how the documents are currently produced: Postscript multiple images - Pastebin.com

This is an example of what I thought would work, with the image inside a procedure: Postscript multiple images - with procedure - Pastebin.com

(These aren't actually real documents, just an example of how it works!)

Can anyone help me to get the second example working?  Essentially, I copied the image producing code into a procedure, and then I'm trying to call it.  No matter what I try, it doesn't want to play - the error I'm getting in Ghostscript is "/syntaxerror in --%ztokenexec_continue--".  What I can find on the web about this error doesn't help - I'm not particularly experienced with postscript, so please be nice!

Thanks in advance!

John

This topic has been closed for replies.

2 replies

Legend
September 5, 2014

And for why it doesn't work... typical image definitions look like this

parameters including "currentfile" image

lines of image data

What happens when this is executed is that the interpreter puts the parameters on the stack, then calls image. Image reads from its source, currentfile, which is exactly the same source used to execute operands and operators. When image has finished, it stops reading currentfile and the interpreter will resume.

Now, when a procedure is defined the input is tokenised, put in an array, and this is saved for later use.

When a procedure is executed, the operands and operators in the array are repeated almost as if they were found in the input. But this is false security, because it breaks down with inline data. If you put the lines above in a procedure, then EACH TIME the procedure is executed it will read from currentfile - and the current position is probably the call to the procedure. In fact, you can define a procedure like the above but leave out the lines of inline data. Include the lines of inline data after each call and it will read the image each time, but this does not help with your aim.

This was very tricky to solve before ReusableStreamDecode, which allows you to turn any section of your input into a file to be used over and over - it becomes currentfile. This was invented for level 3 PostScript.

Participating Frequently
September 4, 2014

John,

You are close but want to use a reusable stream decode filter e.g.

/imgFile
  currentfile
  /ASCII85Decode filter <</Intent 0 /AsyncRead true>>
  /ReusableStreamDecode filter
2dK<@>Z*Od;EA#[4BbWH=].Fd;En8]6<ZuG=%>GT7QsjB5#t-;=%>Y`9M&Yi3*oND

<snip>

>XrR:8o%g.C1'nP?rLZF:LI:#?rg'2<_Zq"8mG%]>Y.Rp92&\c~>
def

/oneimage

{

gsave

[540.0000 2 div 0.0000 0.0000 540.0000 2 div 36.0000 126.0000] concat

/ImageDict 8 dict def

ImageDict begin

  /ImageType 1 def

  /Width 16 def

  /Height 16 def

  /BitsPerComponent 8 def

  /ImageMatrix [16.0000 0.0000 0.0000 -16.0000 0.0000 16.0000] def

  /DataSource imgFile def

  /MultipleDataSources false def

  /Decode [ 0 1 0 1 0 1 ] def

end

% snipped setcolorspace

ImageDict image

imgFile resetfile

grestore

} def

oneimage

144 144 translate

oneimage