2

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: https://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}]>> ?

Community
  • 1
  • 1
radistao
  • 14,889
  • 11
  • 66
  • 92
  • 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 03 '12 at 22:10

3 Answers3

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....

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
KenS
  • 30,202
  • 3
  • 34
  • 51
2

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
    save
    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"
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
chrisl
  • 461
  • 2
  • 3
  • 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 02 '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 02 '12 at 20:11
  • That's great. I'm using the view__.ps utilities quite frequently. – Kurt Pfeifle Oct 02 '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 03 '12 at 15:54
  • *My* interest in this is definitely there. – Kurt Pfeifle Oct 03 '12 at 21:16
  • OKay, so MIFF, PBM and PCX all do the showpage operation in the "view___" procedure. For these cases, I'm thinking of adding a FITPAGE option. So for normal command line use, you'd add -dFITPAGE. Does that sound reasonable? – chrisl Oct 04 '12 at 07:46
  • Yes, chrisl, that's what I'd need :-) – Kurt Pfeifle Oct 04 '12 at 10:56
  • @Kurt Pfefifle - I just checked viewjpeg.ps and I do not see where it reads the X and Y desities or the resolution unit. So the height and width being used are the raw pixels which this code ends up mapping to 72 dpi. Which is OK if that is what you want, but note it is not the actual image size as specified in the JPEG image. – ScottProuty Oct 09 '12 at 19:08
  • ScottProuty, what I put above just uses the raw pixels, I assumed most people that want something different would be capable of modifying the example PS snippet I gave for the command line to get the scale they're after - that's the main reason I opted for a procedure that returns the dimensions, rather than one that goes ahead and sets the page size. – chrisl Oct 10 '12 at 15:31
  • Kurt, I've committed the changes I described above to our git repo. – chrisl Oct 10 '12 at 15:31
  • @chrisl - No worries, I just wanted to point it out in case you wanted to do something more robust with the feature. – ScottProuty Oct 10 '12 at 18:53
  • @Kurt Pfefifle - Sorry, I should have made that comment to chrisl. Obviously you are fine with the way it currently works with raw pixels. – ScottProuty Oct 10 '12 at 18:56
  • @ScottProtuty: That's ok :-) – Kurt Pfeifle Oct 10 '12 at 20:05
  • @chrisl: Thanks, and thanks for the info. I'll report any problems I may find in bugzilla :) --- – Kurt Pfeifle Oct 10 '12 at 20:06
0
 gs -sDEVICE=pdfwrite -o /pathTo/newPdf.pdf "/pathTo/viewjpeg.ps" -c "(/pathTo/jpgToPdf.JPG) << /PageSize [400 300] >> setpagedevice  viewJPEG"
  • /pathTo/newPdf.pdf - file that'll be generated
  • /pathTo/jpgToPdf.JPG - input jpg file
  • << /PageSize [400 300] >> setpagedevice viewJPEG - viewjpeg.ps utility command line arguments
  • [400 300] - image size, width and height calculated by formula (width/resolution) * 72 and (height/resolution) * 72
Yerlan
  • 1
  • 1