Ghostscript rotates pages

I use Ghostscript to convert PDF documents to PCL for printing. Recently, I have an additional requirement that before printing all pages should be rotated to "Portrait". I found a way to do this using Ghostscript with the 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" 

Content 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 tried to change this function to replace the landscape sizes with their portrait copy, but could not create a functional postscript. I need to point in the right direction to get the postscript equivalent of the next pseudocode.

 for(each page) { if(PageSize == [792 612]) PageSize = [612 792]; } 

I know that there are non-Ghostscript ways to rotate pages, but if I can get this to work, it will fit in well with my process and will not decrease performance.

Here is an example of one of my pdf files: Sample1.pdf

+6
source share
2 answers

I found a workable solution. It is not as much as I hoped, but it satisfies all my requirements.

The following script will rotate the A4, Letter, and Legal documents in Portrait. To force it to make other page sizes, adjust the minimum and maximum sizes.

 %!PS % Sequence to set up for a single page size, auto fit all pages. % Autorotate A4 Letter and Legal page sizes to Portrait << /Policies << /PageSize 3 >> /InputAttributes currentpagedevice /InputAttributes get %current dict dup { pop 1 index exch undef } forall % remove all page sizes dup 0 << /PageSize [ 595 0 612 99999 ] >> put % [ min-w min-h max-w max-h ] >> setpagedevice 

This script will rotate A4, Letter, and Legal documents in Landscape. The only difference is the Min / Max page size values.

 %!PS % Sequence to set up for a single page size, auto fit all pages. % Autorotate A4 Letter and Legal page sizes to Landscape << /Policies << /PageSize 3 >> /InputAttributes currentpagedevice /InputAttributes get %current dict dup { pop 1 index exch undef } forall % remove all page sizes dup 0 << /PageSize [ 0 595 99999 612 ] >> put % [ min-w min-h max-w max-h ] >> setpagedevice 

This solution is based on the auto -rotate.ps file that I found in the source code of the hylafax project. This project appears to be licensed under BSD.

+2
source

PostScript is a programming language, so you can do a lot with it. What you need to do is override the page size request action. The page size and content are separated in PostScript, so you need to do 2 things:

1) Change the media request from landscape to portrait

2) rotate page content

The easiest way to do this is to override the setpagedevice statement. Here is 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 the configuration changes to see if the page is resized if it gets a new size, and looks to see if it will have a width> height (a simple definition of the landscape). If true, then it modifies the query by replacing the width and height, and then rotates the contents of the page 90 degrees.

You can use this with Ghostscript by placing the above content in a file (e.g. prolog.ps) and then running this file before your own task:

gs ...... prolog.ps job.ps

I tried this, but not with the landscape file, since I did not have it. Also note that you can create a PostScript program that defeats this.

+4
source

Source: https://habr.com/ru/post/914939/


All Articles