Configuring GhostScript to rotate page output other than multiple of 90 degrees (PDF => PNG)

StackOverflow https://stackoverflow.com/questions/21343626

  •  02-10-2022
  •  | 
  •  

Question

I'm trying to rotate GhostScript's output from a PDF input using the following:

gs -dSAFER -dBATCH -dNOPAUSE -r200 -sDEVICE=pngmono \
 -dAutoRotatePages=/None -sOutputFile=output.png -c 10 rotate -f input.pdf

It generates the output file without any rotation (vs. the desired 10 degree rotation). Any ideas what's going wrong here?

Was it helpful?

Solution

Firstly; AutoRotatePages is only defined for the pdfwrite family of devices, other devices don't do anything with it. So specifying it to the pngmono device will have no effect.

Secondly, the PDF interpreter resets the graphics state when it processes the PDF file. It does this because, in order to do things like page fitting, setting the PageSize to the MediaBox of the PDF file and a bunch of other stuff, it calls setpagedevice. One of the implicit actions of setpagedevice is to call initgraphics, which resets the CTM.

Basically you can't rely on the PostScript graphics state at the time when you start processing a PDF file to have any effect on the graphics state while processing the PDF.

If you really want to do this you will have to modify gs/Resource/Init/pdf_main.ps, at the end of pdfshowpage_setpage:

  pop currentdict end setpagedevice
} bind def

You'll need to insert your rotation here, after the setpagedevice. The neatest way to do this is to use a PostScript parameter, say UserRotation. You might then do:

  pop currentdict end setpagedevice
  /UserRotation where {
    /UserRotation get rotate
  } if
} bind def

And call GS with -dUserRotation=10

For those running on a system where the Resources are built into the ROM file system, you will need to modify the file on disk and then tell GS to use the modified Resources using the -I switch (-I/ghostpdl/gs/Resource/Init). For anyone trying to use this in Windows, you will first need to get hold of the Resources (they are not currently supplied as part of the Windows binary release) which will probably mean downloading the Ghostscript sources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top