如何使用Ghostscript将PDF文件的一部分裁剪为PNG [英] How to crop a section of a PDF file to PNG using Ghostscript

查看:182
本文介绍了如何使用Ghostscript将PDF文件的一部分裁剪为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将PDF文件中的特定部分裁剪为PNG(这将通过使用Ghostscript和PHP自动实现).这就是我现在要做的,基本上将PDF的首页转换为PNG:

I need to crop a certain section in my PDF file to PNG (this will be automated using Ghostscript with PHP). This is what i do now which basically turns the first page of a PDF to PNG:

gs -q -dNOPAUSE -dBATCH \
   -sDEVICE=pngalpha -dEPSCrop \
   -sOutputFile=output.png input.pdf

具体来说,我正在尝试将这张左上方的卡片裁剪为PNG.我也希望获得有关如何完成此操作的更多建议.

Specifically, i'm trying to crop this top left card to a PNG. I'm also open for more suggestions on how to accomplish this.

推荐答案

首先,
确定您的第一个PDF页面的边界框:

First,
determine the bounding box of your first PDF page:

gs                          \
 -q                         \
 -dBATCH                    \
 -dNOPAUSE                  \
 -sDEVICE=bbox              \
 -dLastPage=1               \
  stackoverflowQuestion.pdf \
2>&1                        \
| grep %%BoundingBox

结果将是:

%%BoundingBox: 119 531 464 814

这意味着:

  • 边界框的左下角位于坐标(119,531)
  • 边界框的右上角位于坐标(464,814)
  • the lower left corner of the bounding box is at coordinate (119,531)
  • the upper right corner of the bounding box is at coordinate (464,814)

这些值位于 PostScript点(其中为72 pt == 1 inch)中.边界框是那个矩形,其中包括这些图形PDF对象,这些对象在页面上留下墨水或墨粉标记.

The values are in PostScript points (where 72 pt == 1 inch) . The bounding box is that rectangle, which includes these graphical PDF objects that leave ink or toner marks on a page.

然后
创建您的PNG.

Then,
create your PNG.

从边界框值导出,您似乎希望它宽345 pt(= 464 - 119)和高283 pt(= 814 - 531).

Deriving from the bounding box value, you seem to want it 345 pt wide (= 464 - 119) and 283 pt high (= 814 - 531). This leads to a pages size of -g345x283 (given in pixels, because Ghostscript uses by default 72 dpi for image output (unless specified otherwise), and therefor 72 px == 1 inch.

或者更好的是,我们与边界框保持1 pt的安全区域,因此我们使图像比最小裸露尺寸大一点,并得到以下图像尺寸:-g347x285.

Or better, we keep a security zone of 1 pt away from the bounding box, so we make the image a bit bigger than the bare minimum and we get this image dimension: -g347x285.

您还需要从左侧边缘切掉119 pt(安全性"为118 pt),从底部边缘切掉531 pt(安全性为530).

You also need to cut off 119 pt from the left edge (118 pt for 'security') and 531 pt from the bottom edge (530 for security).

因此命令将是:

gs                                                      \
  -o out.png                                            \
  -sDEVICE=pngalpha                                     \
  -g347x285                                             \
  -dLastPage=1                                          \
  -c "<</Install {-118 -530 translate}>> setpagedevice" \
  -f stackoverflowQuestion.pdf 

以下是生成的PNG:

要获得更好的PNG质量,请将分辨率从默认的72 dpi提高到720 dpi,然后使用以下命令:

For a better PNG quality, increase the resolution from the default 72 dpi to 720 dpi and use this command:

gs                                                      \
  -o out720dpi.png                                      \
  -sDEVICE=pngalpha                                     \
  -r720                                                 \
  -g3470x2850                                           \
  -dLastPage=1                                          \
  -c "<</Install {-118 -530 translate}>> setpagedevice" \
  -f stackoverflowQuestion.pdf 


更新:

在Windows的CMD窗口中,Ghostscript的控制台应用程序名称为gswin32c.exe和/或gswin64c.exe(而不是gs).另外,您还必须使用^作为换行符(而不是\).

On Windows in a CMD window, the console application names for Ghostscript are gswin32c.exe and/or gswin64c.exe (instead of gs). Also, you'd have to use ^ as a line continuation character (instead of \).

这篇关于如何使用Ghostscript将PDF文件的一部分裁剪为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆