Setting auto-height/width for converted Jpeg from PDF using GhostScript

隐身守侯 提交于 2019-11-30 19:56:07
PinnyM

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.

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.

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 :)

KenS

You could write a PostScript program to do this readily enough. Here is a start:

%!
% usage: gs -sFile=____.pdf  scale.ps

/File where not {
  (\n   *** Missing source file. \(use -sFile=____.pdf\)\n) =
  Usage
} {
  pop
}ifelse

% Get the width and height of a PDF page
%
/GetSize {
  pdfgetpage currentpagedevice
  1 index get_any_box 
  exch pop dup 2 get exch 3 get
  /PDFHeight exch def
  /PDFWidth exch def
} def


%
% The main loop
% For every page in the original PDF file
%
1 1 PDFPageCount 
{
  /PDFPage exch def
  PDFPage GetSize

% In here, knowing the desired destination size
% calculate a scale factor for the PDF to fit
% either the width or height, or whatever
% The width and height are stored in PDFWidht and PDFHeight
  PDFPage pdfgetpage
  pdfshowpage
} for

pdfgetpage and pdfshowpage are internal Ghostscript extensions to the PostScript language for handling PDF files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!