Reputation: 15534
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}]>>
?
Upvotes: 2
Views: 3679
Reputation: 1
gs -sDEVICE=pdfwrite -o /pathTo/newPdf.pdf "/pathTo/viewjpeg.ps" -c "(/pathTo/jpgToPdf.JPG) << /PageSize [400 300] >> setpagedevice viewJPEG"
Upvotes: 0
Reputation: 31199
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....
Upvotes: 2
Reputation: 491
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"
Upvotes: 2