2

I want to use GS to generate thumbnails from pdf files.

  • The thumbnail must fit a 90x120 pixel rectangle
  • The image should not be rotated
  • The image should be resized to fit the rectangle with keeping aspect ratio

I use the following command:

gswin32 -dPDFFitPage -dPARANOIDSAFER -dBATCH -dNOPAUSE -dNOPROMPT 
-dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=0 
-dDEVICEWIDTH=90 -dDEVICEHEIGHT=120 -dORIENT1=true 
-sDEVICE=jpeg  -dTextAlphaBits=4 -dGraphicsAlphaBits=4 
-sOutputFile=output.%d.jpg input.pdf

Result:

If I use some PDF with portrait pages like this example, which you can download, then the thumbnail is correct, as you can see here:

enter image description here

If I use it for a PDF with landscape pages, the devicewidth is taken as height somehow:

enter image description here

How can I prevent this behaviour? I want my Porsche to be 90x120 as well. I think maybe i need to provide some Postscript code for Ghostscript (with -c command line argument), but I have no experience with that. Could someone please help me?

EDIT1: I tried the suggestion of KenS. With the -dFIXEDMEDIA my porsche gets cropped like you can see below:

enter image description here

EDIT2: first solution of Kurt Pfeifle is not ok for portrait images (white part right side, not using space properly). See below:

enter image description here

EDIT3: third solution of Kurt Pfeifle is almost good. For landscape images its perfect:

enter image description here

However, the portrait pages have sizing problems, and the background becomes completely gray as well:

enter image description here

So Kurt, i think I could split the pdf to pages (with GS -dFirstPage and -dLastPage), and then if you have some idea, how to decide if a page is landscape or portrait, then I could do some conditional processing. Any idea? As for background color, I think its not that important, but if you have ideas here as well, then they are welcome.

Community
  • 1
  • 1
Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
  • Regarding your comment about your 'Edit 2': you tested my suggestion with your first file. Of course it will not work here. My suggestion was meant for the seconde file (Porsche pictures in landscape). – Kurt Pfeifle Jul 05 '12 at 10:25
  • It's crazy that 5 years later, gs still seems to be unable to simply create a thumbnail without "helpfully" rotating the image for you. – Haydentech Dec 05 '17 at 12:25

4 Answers4

2

Ok, now trying with a more elaborated answer, based on the above mentioned idea of 'manipulating the PDF first (so that it uses a portrait media format to display its landscape image contents)'...

Note, since your original PDF doesn't use a common size for all pages (they all are different), I did my proof of concept with the first page only.

Step 1: Extract page 1 from the original (landscape)

I'm using pdftk for this:

porsches.pdf  cat 1  output porsche-page1.pdf

Step 2: Center landscape content on larger portrait page

I'm using Ghostscript plus a -c ... PostScript commandline snippet for this:

gs \
 -o porsche-page1-on-portrait-medium.pdf \
 -sDEVICE=pdfwrite \
 -dPDFSETTINGS=/prepress \
 -g1920x2560 \
 -r72 \
 -c "<</PageOffset [0 560]>> setpagedevice" \
 -f porsche-page1.pdf

The /PageOffset values are derived from the fact that I added 1120 points to the original page height of 1440 points. Therefor I shift the contents by half the value upwards so the picture is again centered.

Step 3: Create the JPEG thumbnails (portrait)

gs \
 -sOutputFile=proofofconcept-thumb_%03d.jpg \
 -dPDFFitPage \
 -dDEVICEWIDTH=90 \
 -dDEVICEHEIGHT=120 \
 -sDEVICE=jpeg \
  porsche-page1-on-portrait-medium.pdf

Unless I didn't utterly mis-understand what your intention was: this should be the result that you wanted. (Note: I added a black frame to the thumbnail image to make its true dimensions visible even on the white HTML background of Stackoverflow.)

Proof-of-Concept thumbnail

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • I appretiate your time to provide this example. However the conversion process should be a black box. No manual pageoffset determination could be included. However, you gave me a good idea, to take a look around the sources of Ghostscript (i have found it between your other answers :) ). – Gábor Lipták Jul 05 '12 at 11:50
  • Of course you can transform my PoC approach into an automatically running script to make it the 'black box' you want. It just needs some more effort... – Kurt Pfeifle Jul 05 '12 at 12:06
  • 1
    @Gábor Lipták: I abused one of your nice Porsche pics ;-) to illustrate an [answer for another question](http://stackoverflow.com/a/11362152/359307). – Kurt Pfeifle Jul 06 '12 at 15:28
  • Hehe. I like Porsches. Don't scratch it ;) – Gábor Lipták Jul 06 '12 at 20:37
1

Set -dFIXEDMEDIA so that Ghostscript knows the media is fixed.

Another time you might post a smaller example file, we really didn't need all 71 pages, though I appreciate they are nice pictures....

KenS
  • 30,202
  • 3
  • 34
  • 51
1

I don't think what you want is currently possible without manipulating the PDF first (so that it uses a portrait media format to display its landscape image contents).

Since it seems you take issue mostly with the 120 pixel width you're getting, my own workaround for processing the un-modified input would be to change the height/width settings like this:

-dDEVICEWIDTH=90 -dDEVICEHEIGHT=89 

This will create JPEGs that are 90 pixels wide, un-rotated, and these will fit into your 90x120 pixels rectangle (as you required). :-)

(Afterwards, you could still manipulate the obtained JPEGs with one of the ImageMagick commandline tools in order to make them truely 90x120 pixels with the Porsches centered in the rectangle or whatever....)

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Portrait images are not. I am pretty sure, that somehow with postscript is possible. – Gábor Lipták Jul 04 '12 at 20:59
  • *"Portrait images are not."* -- Somehow I do not understand this sentence... ? – Kurt Pfeifle Jul 04 '12 at 21:55
  • Yep, I wanted to edit the comment, but after 5 minutes i could not. So they are not ok. See edit2 in the question please. – Gábor Lipták Jul 05 '12 at 06:53
  • @Gábor Lipták: You seem to have tested my suggestion with your *first* file. Of course it will not work here. My suggestion was meant for the *second* file (Porsche pictures in landscape) only... – Kurt Pfeifle Jul 05 '12 at 12:08
1

Here is how you can script this whole thing more easily. (I only now realized fully, that you seem to want this for Windows...)

First step: Create a 1st intermediate PDF with all equal page sizes

gswin32c.exe ^
   -o 1920x1440pts.pdf ^
   -dPDFFitPage ^
   -sDEVICE=pdfwrite ^
   -dPDFSETTINGS=/prepress ^
   -g19200x14400 ^
    porsche.pdf

This was in preparation for the 2nd step. The 'same size for all pages' will allows us to use the same fixed /PageOffset values for that.

Second step: 2nd intermediate PDF (portrait, with centered landscape content)

gswin32c.exe 
   -o 1920x1440-portrait.pdf ^
   -sDEVICE=pdfwrite ^
   -dPDFSETTINGS=/prepress ^
   -g19200x25600 ^
   -c "<</PageOffset [0 560]>> setpagedevice" ^
   -f 1920x1440pts.pdf 

This was in preparation for the 4th step. Now that we have portrait pages in the PDF (holding the landscape image content), our thumbnail creation will result in portrait thumbnails too...

Third step: Add a gray background to the portrait pages

gswin32c.exe ^
   -o - ^
   -sDEVICE=pdfwrite ^
   -g1920x2560 ^
   -c ".6 setgray 0 0 192 256 rectfill showpage" ^
| ^
pdftk.exe ^
   1920x1440-portrait.pdf ^
   background - ^
   output 1920x1440-portrait-gray-background.pdf

The first command in the pipeline uses Ghostscript to write a PDF page with gray background to stdout, whereas the second command uses pdftk to read the background information from its stdin and create the next intermediary output.

If you don't like that shade of gray, use another value instead of .6. If you don't like gray, but want color, use instead of .6 setgray

  • 1 0 0 setrgbcolor for red background,
  • 0 1 0 setrgbcolor for green background,
  • 0 0 1 setrgbcolor for blue background,
  • 1 0 0 0 setcmykcolor for cyan background,
  • 0 1 0 0 setcmykcolor for magenta background,
  • 0 0 1 0 setcmykcolor for yellow background.

Fourth step: Create your final JPEG thumbnails

gswin32c.exe ^
   -o porsche-thumbnails-portrait_%03d.jpg ^
   -dPDFFitPage ^
   -dDEVICEWIDTH=90 ^
   -dDEVICEHEIGHT=120 ^
   -sDEVICE=jpeg ^
    1920x1440-portrait-gray-background.pdf

I leave it for your own pleasure to copy these commands into a batch file and add a few other things you may want to it... :-)


Update: Of course, if you have a file with pages in landscape as well as portrait, you need to apply some conditional processing. To discover orientation and size of pages on the commandline, pdfinfo may be of help, if called like this:

 pdfinfo -f 4 -l 7 some.pdf

This will print the page size of pages 4 (f irst) to 7 (l ast).

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Thanks for your effort again. You surely have played a lot with PDFs. Haven't you? :) I will try this solution. However the "pageoffset" still seems input page specific in the 2nd step. Is not it that? Nevermind. I will take a look at postscript in my free time. I am interested now :) – Gábor Lipták Jul 05 '12 at 15:53
  • @Gábor Lipták: The `/PageOffset` in the second step is specific to pages of the size *1920 x 2560 pts* (created in the same step) -- which each host 1 full-page image of dimensions *1920 x 1440 pts*. But this is now guaranteed to be the case, because of my first step's result. – Kurt Pfeifle Jul 05 '12 at 16:55
  • 1
    @Gábor Lipták: Yes, quite a lot of time for only 3 upvotes... Heh... ;-) – Kurt Pfeifle Jul 05 '12 at 16:57
  • I see now. I will try it soon. – Gábor Lipták Jul 05 '12 at 20:31
  • All the three was me :) Noone else is interested in my questions. – Gábor Lipták Jul 05 '12 at 20:32
  • So I tried it. See edit3 please, if you still have time for me :) – Gábor Lipták Jul 12 '12 at 07:53
  • @Gábor Lipták: As I stated in **two** other comments on this page: my recommendation was **specifically** for those pages which are landscape. – Kurt Pfeifle Jul 12 '12 at 08:06
  • Hehe, I have found it in your answers: http://stackoverflow.com/a/3132653/337621 . I will combine it with this answer, and I will post it here as a batch file. Thanks Kurt. – Gábor Lipták Jul 12 '12 at 08:12
  • Any idea about this? http://stackoverflow.com/questions/11448260/ghostscript-works-in-command-line-but-does-not-work-if-executed-from-java-on-ai – Gábor Lipták Jul 12 '12 at 08:57
  • @Gábor Lipták: I seem to get more possitive feedback via comments than I do via +1 upvotes... :-) – Kurt Pfeifle Jul 12 '12 at 11:04