使用GhostScript设置从PDF转换的Jpeg的自动高度/宽度 [英] Setting auto-height/width for converted Jpeg from PDF using GhostScript

查看:110
本文介绍了使用GhostScript设置从PDF转换的Jpeg的自动高度/宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GS进行从PDF到JPEG的转换,以下是我使用的命令:

I am using GS to do conversion from PDF to JPEG and following is the command that I use:

gs -sDEVICE=jpeg -dNOPAUSE -dBATCH -g500x300 -dPDFFitPage -sOutputFile=image.jpg image.pdf

在此命令中,您可以看到-g500x300是设置转换后的图像大小(宽度x高度).

In this command as u can see -g500x300 is to set the converted image size (Width x Height).

有没有一种方法可以直接设置宽度而不必输入高度,因此它将基于宽度来使用其原始纵横比来缩放高度?我知道可以通过使用ImageMagick转换来实现,只需在高度参数即-resize 500x0上放置0即可.我尝试使用GhostScript,但我认为这不是正确的方法.

Is there a way to just set the Width without having to input the Height so it will base on the width to scale the height using its original aspect ratio? I know it can be achieved by using ImageMagick convert where you simply put 0 on the height parameter i.e. -resize 500x0. I tried with GhostScript but I don't think that is the correct way to do it.

我决定不使用ImageMagick转换的原因是因为在转换大型多页PDF时它非常慢.

I decided not to use ImageMagick convert reason why because it is very slow when it comes to converting a big sized multiple page PDF.

感谢您的帮助!

推荐答案

这篇文章解释了为什么ghostscript更快-

This post explains why ghostscript is faster - https://serverfault.com/questions/167573/fast-pdf-to-jpg-conversion-on-linux-wanted, and the only workaround to fix it would involve modifying the imagemagick code.

不幸的是,ghostscript不支持自动确定的输出大小.这主要是因为所使用的-g选项实际上是确定将保存呈现的输出的设备大小,而不是呈现呈现的输出本身的大小.由于-dPDFFitPage开关会尝试匹配设备的大小,因此输出大小正在更改.而且,尽管您可以使用-dDEVICEHEIGHT=n定义jpeg'device'的高度,但这将使设备的宽度保持默认值.

Unfortunately, autodetermined output size is not supported by ghostscript. This is primarily because the -g option used is actually determining the device size that will hold the rendered output, and not the rendered output itself. That output size is changing because of the -dPDFFitPage switch which then tries to match the device size. And although you can define just the height of the jpeg 'device' using -dDEVICEHEIGHT=n, that will leave the device width at the unchanged default.

尽管有些乏味的解决方法,但是您可以使用ghostscript或imagemagick获取pdf页面的宽度和高度.要使用ghostscript进行此操作,请参见使用GhostScript来获取页面大小的答案. .然后,您可以计算适当的宽度以设置-g标志以保持宽高比.如果您能找出一组命令来完成所有这些操作,则可以加分:)

Although a somewhat tedious workaround, you can use ghostscript or imagemagick to get the width and height of the pdf page(s). To do this using ghostscript, see the answer to Using GhostScript to get page size. You can then calculate the proper width to set the -g flag to hold the aspect ratio. Bonus points if you can figure out a single set of commands to do all this :)

这篇关于使用GhostScript设置从PDF转换的Jpeg的自动高度/宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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