使用Ghostscript将PDF格式化为PNG时,请使用PDF格式的MediaBox/CropBox

use*_*080 10 pdf png crop ghostscript

我一直在使用Ghostscript将我用PDF格式绘制的单个图转换为PNG:

gswin32c -sDEVICE=png16m -r300x300 -sOutputFile=junk.png ^
         -dBATCH -dNOPAUSE Figure_001-a.pdf
Run Code Online (Sandbox Code Playgroud)

从某种意义上说,我得到一个PNG,它包含了情节.

但它也包含大量的空白区域(示例源图像:http://cdsweb.cern.ch/record/1258681/files/Figure_001-a.pdf).

如果您在Acrobat中查看它,您会注意到绘图周围没有空白区域.如果使用上面的命令行,你会发现该图只占空间的1/3左右.

当我用EPS文件做同样的事情时,我遇到了同样的问题.但是,有一个命令行参数-dEPSCrop可以传递给PS渲染引擎以关注BoundingBox.

我需要类似的参数来渲染PDF.我无法在文档中找到它(-dEPSCrop实际上也没有).

san*_*hez 8

我有完全相同的问题.我通过添加-dUseArtBox开关来修复它.

例:

 /usr/bin/gs -dUseArtBox -dNOPAUSE -sDEVICE=pngalpha -sOutputFile=output.png input.pdf
Run Code Online (Sandbox Code Playgroud)

注意: 自ghostscript版本9.07起-dUseArtBox支持切换

-dUseArtBox Sets the page size to the ArtBox rather than the MediaBox. The art box defines the extent of the page's meaningful content (including potential white space) as intended by the page's creator. The art box is likely to be the smallest box. It can be useful when one wants to crop the page as much as possible without losing the content.


Kur*_*fle 6

有多种选项可以控制Ghostscript呈现给定输入的"媒体大小":

-dPDFFitPage
-dUseTrimBox
-dUseCropBox

使用PDFFitPage Ghostscript将呈现当前页面设备大小(通常是默认页面大小).使用UseTrimBox,它将使用TrimBox(它将同时将PageSize设置为该值).使用UseCropBox,它将使用CropBox(它将同时将PageSize设置为该值).默认情况下(不提供参数),Ghostscript将使用MediaBox进行渲染.

对于您的示例,看起来添加"-dUseCropBox"将完成您期望的工作.

注意,您还可以使用"-sPAPERSIZE"(在Ghostscript知道的所有预定义值中选择)或(为了更大的灵活性)使用来控制输出的总体大小"-dDEVICEWIDTHPOINTS=NNN -dDEVICEHEIGHTPOINTS=NNN".


top*_*kip 0

您是否尝试过使用pdftex(例如 texlive 附带)或(尚未尝试)python 脚本pdfcrop 来使用 pdfcrop

我使用提到的第一个工具有类似的工作流程。