Reputation: 2592
I have an ImageMagick command for converting first page of a PDF to an image.
convert file.pdf[0] -background white -flatten -resize 173 \
-crop 173X229+0+0 -gravity NorthWest +repage test.jpg
Here I needed 173px width image with max 229px height for the first page. For bigger files (~9MBs), convert was taking around 2 mins.
When I tried to test it with gs using the following, the script took only a fraction of seconds:
gs -sDEVICE=jpeg -dFirstPage=1 -dLastPage=1 -o test4.jpg file.pdf
I need help resizing the image to 173px and then cropping the height to 229px. Can anyone help with this gs script?
Upvotes: 1
Views: 156
Reputation: 31141
In /ghostpdl/Resource/Init/pdf_main.ps at around line 2086 is the routine pdf_PDF2PS_matrix which does the scaling. At around line 2094 we see:
currentpagedevice /.HWMargins get aload pop
currentpagedevice /PageSize get aload pop
% Adjust PageSize and .HWMargins for the page portrait/landscape orientation
2 copy gt % PageSize_is_landscape
7 index aload pop 3 -1 roll sub 3 1 roll exch sub exch
10 index /Rotate pget not { 0 } if cvi 90 idiv 1 and 0 ne { exch } if
gt % Box_is_landscape
ne {
1 index 0 translate 90 rotate % add in a rotation
This checks the orientation of the media which has been set against the orientation of the requested media from the PDF file, and if they are not the same, it rotates the PDF file by 90 degrees.
You can change this to:
currentpagedevice /.HWMargins get aload pop
currentpagedevice /PageSize get aload pop
% Adjust PageSize and .HWMargins for the page portrait/landscape orientation
2 copy gt % PageSize_is_landscape
7 index aload pop 3 -1 roll sub 3 1 roll exch sub exch
10 index /Rotate pget not { 0 } if cvi 90 idiv 1 and 0 ne { exch } if
gt % Box_is_landscape
ne pop false {
1 index 0 translate 90 rotate % add in a rotation
which will prevent the if clause from executing (by pop'ping the boolean from the stack and replacing it with a 'false') so the rotation will not take place.
For me this does not rotate your landscape pages.
How you use this depends on how your Ghostscript has been built, which may depend on the package maintainer for your Linux distribution.
If the resources are built into a ROM file system, then you will need to locate the copy stored on disk, modify the file, and then either rebuild Ghostscript so that the modified file is built into the ROM file system, or use the -I switch to tell Ghostscript to ignore the ROM file system and read the resources form a location on disk.
If Ghostscript is not built to use a ROM file system then you need to locate the resources on disk, and modify that file. Or again you can use the -I switch to tell Ghostscript to use a set of modified resources.
It may be best overall to use the -I switch anyway, as that way you can retain the normal resources, and only use this modified code when you want to do this task.
Upvotes: 1