使用Ghostscript生成缩略图可旋转风景pdf页面的设备尺寸定义 [英] Thumbnail generation with Ghostscript rotates my device size definition for landscape pdf pages
问题描述
我想使用GS从pdf文件生成缩略图.
- 缩略图必须适合90x120像素的矩形
- 图像不应旋转
- 应调整图像的大小以适合矩形并保持宽高比
我使用以下命令:
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
结果:
如果我在人像页面上使用某些PDF 像此示例,您可以下载该缩略图,然后缩略图正确无误,如您在此处看到的那样:
如果我将其用于具有风景页面的PDF ,则设备宽度为作为高度:
如何防止这种行为?我也希望我的保时捷也能达到90x120.我想也许我需要为Ghostscript提供一些Postscript代码(带有-c命令行参数),但是我对此没有经验.有人可以帮我吗?
EDIT1 : 我尝试了 KenS的建议.有了-dFIXEDMEDIA,我的保时捷就会像下面看到的那样被裁剪:
EDIT2 :对于肖像图像,Kurt Pfeifle的第一个解决方案不可行(白色部分)右侧,未正确使用空间).见下文:
EDIT3 : Kurt Pfeifle的第三种解决方案几乎是不错的选择.对于风景图像,它是完美的:
但是,纵向页面存在尺寸调整问题,并且背景也完全变为灰色:
所以,库尔特,我想我可以将pdf分成页面(使用GS -dFirstPage和-dLastPage),然后,如果您有什么想法,请如何确定页面是否为风景或肖像,那么我可以进行一些条件处理.任何的想法?至于背景颜色,我认为它并不重要,但是如果您在这里也有想法,那么欢迎他们.
好吧,现在基于上述 '首先处理PDF的思想(以便它使用纵向媒体格式以显示其横向图像内容)' ...
请注意,由于您的原始PDF并非在所有页面上都使用相同的大小(它们都不同),所以我仅在第一页上做过概念验证.
步骤1:从原始图片(景观)中提取第1页
我为此使用pdftk
:
porsches.pdf cat 1 output porsche-page1.pdf
第2步:在较大的肖像页面上将风景内容居中
我正在为此使用Ghostscript以及-c ...
PostScript命令行片段:
gs \
-o porsche-page1-on-portrait-medium.pdf \
-sDEVICE=pdfwrite \
-dPDFSETTINGS=/prepress \
-g1920x2560 \
-r72 \
-c "<</PageOffset [0 560]>> setpagedevice" \
-f porsche-page1.pdf
/PageOffset
值源自以下事实:我在原始页面高度1440点上添加了1120点.因此,我将内容向上移动一半,以使图片再次居中.
第3步:创建JPEG缩略图(人像)
gs \
-sOutputFile=proofofconcept-thumb_%03d.jpg \
-dPDFFitPage \
-dDEVICEWIDTH=90 \
-dDEVICEHEIGHT=120 \
-sDEVICE=jpeg \
porsche-page1-on-portrait-medium.pdf
除非我没有完全误解您的意图:这应该是您想要的结果. (注意:我在缩略图上添加了黑框,以使它的真实尺寸可见,即使在Stackoverflow的白色HTML背景上也是如此.
I want to use GS to generate thumbnails from pdf files. I use the following command: 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. 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. I'm using
I'm using Ghostscript plus a The
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
Step 1: Extract page 1 from the original (landscape)
pdftk
for this:porsches.pdf cat 1 output porsche-page1.pdf
Step 2: Center landscape content on larger portrait page
-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
/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.)
这篇关于使用Ghostscript生成缩略图可旋转风景pdf页面的设备尺寸定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!