I use Ghostscript to convert PDF documents to PCL for printing. Recently I have the additional requirement that all pages must be rotated to Portrait before printing. I have found a way to do so using Ghostscript with following command and postscript function.
"C:\Program Files (x86)\gs\bin\gswin32c.exe" "-dNOPAUSE" "-dNOPROMPT" "-dBATCH" "-sDEVICE=pxlmono" "-Ic:\Program Files (x86)\gs\fonts\;c:\Program Files (x86)\gs\lib\;c:\Program Files (x86)\gs\lib\;" "-r300" "-sOutputFile=C:\EXPORTFILE_e542e04f-5e84-4c8e-9b41-55480cd5ec52.cache" "rotate612x792.ps" "C:\EXPORTFILE_3a5de9da-d9ca-4562-8cb6-10fb8715385a.cache"
Contents of rotate612x792.ps
%! Rotate Pages
<< /Policies << /PageSize 5 >>
/PageSize [612 792]
/InputAttributes currentpagedevice
/InputAttributes get mark exch {1 index /Priority eq not {pop << /PageSize [612 792] >>} if } forall >>
>> setpagedevice
The problem is that this function replaces all page sizes with letter size. My documents are sometimes legal or A4. I have tried to modify this function to replace landscape sizes with their portrait counterpart, but have not been able to produce functioning postscript. I need to be pointed in the right direction to produce the postscript equivalent of the following pseudo code.
for(each page)
{
if(PageSize == [792 612])
PageSize = [612 792];
}
I am aware that there are non-Ghostscript ways of rotating pages, but if I can get this to work it would fit nicely into my process and would not reduce performance.
Here is a sample of one of my pdf files: Sample1.pdf
PostScript is a programming language, so you can do a lot with it. What you need to do here is redefine the action of requesting page sizes. The Page size and content are separate in PostScript, so you need to do 2 things:
1) Alter the media request from landscape to portrait
2) rotate the content of the page
The simplest way to do this is to redefine the 'setpagedevice' operator. Here's an example:
/oldsetpagedevice /setpagedevice load def %% copy original definition
/setpagedevice {
dup /PageSize known { %% Do we have a page size redefinition ?
dup /PageSize get %% get the array if so
aload pop %% get elements remove array copy
gt { %% is width > height ?
dup /PageSize get aload %% get size array, put content on stack
3 1 roll %% roll stack to put array at back
exch %% swap width and height
3 -1 roll %% bring array back to front of stack
astore %% put swapped elements into array
/PageSize exch %% put key on stack and swap with value
2 index %% copy the original dict
3 1 roll %% move dict to back of stack
put %% put new page size array in dict
90 rotate %% rotate content 90 degrees anti-clockwise
} if
} if
oldsetpagedevice %% call the original definition
} bind def
This checks configuration changes to see if the page size is being altered, if it is it gets the new size, and looks to see if width > height (a simple definition of landscape). If that is true then it alters the request by swapping the width and height, and then rotates the page content by 90 degrees.
You can use this with Ghostscript by putting the above content in a file (eg prolog.ps) and then running that file before your own job:
gs ...... prolog.ps job.ps
I have tried this, but not with a landscape file as I didn't have one to hand. Note also that it is possible to construct a PostScript program which will defeat this.