Thumbnail generation with Ghostscript rotates my device size definition for landscape pdf pages

末鹿安然 提交于 2020-01-13 19:44:22

问题


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:

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

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:

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

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

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

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.


回答1:


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




回答2:


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




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/11326318/thumbnail-generation-with-ghostscript-rotates-my-device-size-definition-for-land

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!