简体   繁体   English

PHP将PDF转换为图像-dUseCropBox

[英]PHP Converting PDF's to images -dUseCropBox

I'm trying to convert a PDF to an image and I need to make sure that the -dUseCropBox parameter is specified for when calling Ghostscript. 我正在尝试将PDF转换为图像,并且需要确保在调用Ghostscript时为-dUseCropBox参数指定了参数。 Can this be done? 可以这样做吗?

convert "/var/www/vhosts/site.co.uk/httpdocs/uploads/source_pdf/PP4SDpdf.pdf" -resize 500X500 "/var/www/vhosts/site.co.uk/httpdocs/uploads/image_pdf/SaturdayTest.jpg"

It works well but just need to get the Ghostscript parameter in. 它运作良好,但只需要输入Ghostscript参数即可。

The switch for imagemagick (the convert-command) is: imagemagick的开关(convert-command)为:

-define pdf:use-cropbox=true

see http://www.imagemagick.org/Usage/formats/#ps_reading 参见http://www.imagemagick.org/Usage/formats/#ps_reading

Is it acceptable for you to run Ghostscript directly (instead of having convert call it anyway) ? 直接运行Ghostscript (而不是通过convert调用它) 是否可以接受

I ask, because convert does not do the PDF => JPEG conversion by itself. 我问,因为convert本身不会进行PDF => JPEG转换。 It calls Ghostscript as its 'delegate' to do the job. 它称Ghostscript为完成任务的“代表”。 So for convert to work you need to have access to a functional Ghostscript installation on that system anyway... . 因此,要使convert正常工作,您仍然必须能够在该系统上访问功能正常的Ghostscript安装...。

But how to add custom parameters to convert s commandline to pass them through to Ghostscript's commandline isn't easy to figure out. 但是,如何添加自定义参数以convert s命令行以将其传递到Ghostscript的命令行并不容易。 Ghostscript's commandline isn't exactly easy either, but at least it is fully documented at a well-known place (see Use.htm , Devices.htm and Ps2pdf.htm there). Ghostscript的命令行也不是很容易,但是至少在一个知名的地方有完整的文档记录 (请参阅Use.htmDevices.htmPs2pdf.htm )。

Here is a command that would convert your input PDF to a series of JPEGs (one file for each PDF page). 这是一个将输入的PDF转换为一系列JPEG(每个PDF页面一个文件)的命令。 I'm assuming Windows -- for Linux just replace the ^ by \\ and gswin32c.exe by gs : 我假设gswin32c.exe对于Linux只需将^替换为\\并将gswin32c.exegs

gswin32c.exe ^
  -o "d:/path with spaces/to/output/dir/input_page_%03d.jpeg ^
  -sDEVICE=jpeg ^
  -dJPEQ=95 ^
  -r720 ^
  -g5000x5000 ^
  -dUseCropBox=true ^
  "d:/path/to/input.pdf"

Explanation: 说明:

  • -dJPEGQ sets the JPEG quality. -dJPEGQ设置JPEG质量。 Accepts integer values in the range 0..100 . 接受范围为0..100整数值。 Higher values create bigger files... (Ghostscript's default for JPEGQ is set to 75.) 较高的值将创建较大的文件...(对于JPEGQ,Ghostscript的默认设置为75。)
  • -r720 sets a (rather high) resolution of 720dpi. -r720将分辨率设置为720dpi(相当高)。 Higher values create bigger files... (Ghostscript's default for its jpeg output device would be 72dpi.) 较高的值将创建较大的文件...(Ghostscript的jpeg输出设备的默认值为72dpi。)
  • -g5000x5000 gives the file dimension in pixels . -g5000x5000像素为单位给出文件尺寸。 (Note: when decreasing the -r... value you MUST also accordingly decrease the -g... value to keep the same dimension in userspace inches or mm.) (注意:减小-r...值时,还必须相应地减小-g...值,以保持相同的尺寸(以用户空间英寸或mm为单位)。)

You could also add -dPDFFitPage=true if that is useful for you. 您也可以添加-dPDFFitPage=true如果这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM