Take the 2-minute tour ×

I use Ghostscript to convert JPEG to PDF:

gs -sDEVICE=pdfwrite -o d.pdf viewjpeg.ps -c "(Imgp3826.Jpeg) viewJPEG showpage 

This creates PDF with default page size and image, located on left bottom corner. I need to modify script to create page with correct dimensions.

in this note: http://stackoverflow.com/a/6508854/907576

they do this using ImageMagick, but I can't use it - I can use Ghostscript only.

Is is possible to get width-height values from viewjpeg.ps and set it to page size?

I see in viewjpeg.ps:

/height NextByte 8 bitshift NextByte add def
/width NextByte 8 bitshift NextByte add def

Looks like they use JPEG headers to read this info.

Can I then reuse it for <</PageSize [${dimension}]>> ?

share|improve this question
Did you try chrisl's suggestion? If yes, and if it works (it does for work for me), then you should 'accept' his answer.... –  Kurt Pfeifle Oct 3 '12 at 22:10

2 Answers 2

You can't use those numbers directly as arguments to /PageSize, as those are the height and the width of the image (in image samples), while the arguments to PageSize are in PostScript units (1/72 inch).

You could, however, scale them by the current resolution and use the scaled numbers.

(width/resolution) * 72 and (height/resolution) * 72 should do the job.

So :

currentpagedevice /HWResolution get aload
height exch div 72 mul
exch width exch div 72 mul
exch 2 array 3 1 roll astore
<< /PageSize 3 -1 roll >> setpagedevice

Caveat: I haven't had time to actually try this....

share|improve this answer
In which place i have to add it? inside viewjpeg.ps ? –  radistao Oct 2 '12 at 14:44

FWIW, the simple addition listed above won't work alone, due to how the viewJPEG procedure is written.

I suggest augmenting viewjpeg.ps with another procedure:

/viewJPEGgetsize {      % <file|string> ==> width height
    JPEGdict begin
    /saved exch def
    /scratch 1 string def
    dup type /stringtype eq { (r) file } if
    /F exch def

    readJPEGmarkers begin
    width height
    saved end restore
} bind def

And then you can call it like this:

gs -I../lib -sDEVICE=pdfwrite \
   -o stuff.pdf viewjpeg.ps   \
   -c "(image.jpg) <</PageSize 2 index viewJPEGgetsize 2 array astore>> setpagedevice viewJPEG"
share|improve this answer
This is a good improvement for viewJPEG.ps. Hi, Chris :-) --- It's very useful for me too. I hope you'll commit it to Git as well :-) –  Kurt Pfeifle Oct 2 '12 at 16:48
Hi Kurt, I'll probably commit the change to git - the main reason I favoured the above approach is that existing uses of viewjpeg.ps will still work exactly as before. I do want to see which of the other view___.ps utilities can have the same type of addition to them. –  chrisl Oct 2 '12 at 20:11
That's great. I'm using the view__.ps utilities quite frequently. –  Kurt Pfeifle Oct 2 '12 at 20:29
Currently there's only viewjpeg.ps and viewgif.ps that can be done like this - the others work differently, and some are impossible. If there's interest, I'll look at adding a different solution for MIFF, PBM and PCX. –  chrisl Oct 3 '12 at 15:54
My interest in this is definitely there. –  Kurt Pfeifle Oct 3 '12 at 21:16

Your Answer


