2

I have a pdf file with 3 pages. I want to print first page as simplex and second & third page as duplex(double-sided - second on the front, third on the back). How can I generate the required postscript with ghostscript.

I found the below solution on the internet and it works, but it makes the whole document as duplex.

gswin64c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=output.ps -c "<</PSDocOptions (<</Duplex true>> setpagedevice)>> setdistillerparams" -f input.pdf

Above one is a document level option, I found page level option on the Ghostscript documentation page in below link. https://www.ghostscript.com/doc/9.50/VectorDevices.htm#PS

But i could not find any sample code for PSPageOptions parameter. I have tried with below command, but no luck.

gswin64c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=out.ps -c "<</PSPageOptions [(/Duplex false)(/Duplex true)(/Duplex true)] /LockDistillerParams true>>setdistillerparams" -f input.pdf

Did I miss anything? Someone, please help me out.

user1992
  • 169
  • 1
  • 15

1 Answers1

2

From Postscript Language Reference Manual 3rd Ed. at 6.1.1:

 Note: setpagedevice is a page-oriented operator used to control the output process-
  ing of one or more pages of a page description. Any call to setpagedevice implicitly
  invokes erasepage and initgraphics, and thus must precede the descriptions of the
  pages to be affected. If the current device is not a page device, the effect of invoking
  setpagedevice is device-dependent.

Therefore try to open the PostScript file in a text editor and insert the "<</Duplex false>> setpagedevice" at the beginning of the Page 1 code and the "<</Duplex true>> setpagedevice" at the beginning of the Page 2 code. There is also <</Tumble true>> option that might be desired as explained at 6.2.3:

Duplex           boolean       A flag determining whether the output is to be printed duplex (on both sides
                               of the physical medium) or simplex (on one side of the medium only). If this
                               flag is true, pairs of consecutive page images will be printed on opposite sides
                               of a single sheet of medium; if false, each page will be printed on a separate
                               sheet.
                               On device activation, a duplex device always prints the first page on a new
                               sheet of medium; on deactivation, it automatically delivers the last sheet of
                               medium if it has been printed on only one side.
Tumble           boolean      A flag specifying the relative orientation of page images on opposite sides of a
                              sheet of medium in duplex output (that is, when Duplex is true). If Tumble is
                              false, the default user spaces of the two pages are oriented suitably for binding
                              at the left or right, with vertical (y) coordinates in the two spaces increasing
                              toward the same edge of the physical medium. If Tumble is true, the default
                              user spaces are oriented suitably for binding at the top or bottom, with verti-
                              cal coordinates increasing toward opposite edges of the medium. If Duplex is
                              false, the Tumble parameter has no effect.
                               Note that Tumble is defined in terms of default user space—the user space
                               established by setpagedevice. The orientation of default user space with re-
                               spect to the medium is determined by the PageSize and Orientation
                               parameters, possibly altered by the Install procedure. Consistent results are
                               obtained across all devices that support duplexing, regardless of how the me-
                               dium moves through the printing mechanism. However, if a page description
                               alters user space by invoking operators such as rotate, the visual orientation
                               of the material printed on the page may differ from the orientation of default
                               user space.

EDIT: Now I see the point. I was able to modify your approach to insert the strings into the postscript except can't check the duplex printing with my simplex printer. The ghostscript does insert the correct strings into the %%BeginPageSetup as expected:

-c "<</PSPageOptions [ ( (page +1) print) (<</Duplex true>> setpagedevice) ( (page 2+) print) ] /LockDistillerParams true>> setdistillerparams"

The first and third pages emit the page strings and I found the setpagedevice in the correct location for page +2 so I hope everything is working. Your first attempt was very close and I learned something too.

page +1>>showpage, press <return> to continue<<
>>showpage, press <return> to continue<<
page 2+>>showpage, press <return> to continue<<
page +1>>showpage, press <return> to continue<<
>>showpage, press <return> to continue<<
page 2+>>showpage, press <return> to continue<<
page +1>>showpage, press <return> to continue<<
beginner6789
  • 575
  • 3
  • 7
  • Is there any way to pass as a command from outside instead of editing the ps file directly? like we did for document level "gswin64c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=output.ps -c "<> setpagedevice)>> setdistillerparams" -f input.pdf" – user1992 Sep 30 '20 at 05:11
  • 1
    Wow that is close. I edited my answer above with better info. – beginner6789 Sep 30 '20 at 12:26
  • Added the command which i used for all 3-pages in the document to make simplex & duplex. "gswin32c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=output.ps -c "<> setpagedevice) (<> setpagedevice) (<> setpagedevice) ] /LockDistillerParams true>> setdistillerparams" -f input.pdf" – user1992 Sep 30 '20 at 19:55
  • The above command only working with Windows 32 bit Ghostscript not with 64 bit, not sure why?. The same works in Linux 64 bit ghostscript. – user1992 Sep 30 '20 at 19:58
  • Need your input please. I have added duplex parameters with above solution and verified by printing out pages, its printing back to back wherever I put duplex as expected. Now, i have added duplex parameter for 2 pdf files and converted to 2 individual postscript, the problem is, when i merge these pdfs with Ghostscript, it losing page-level parameter we passed while converting to ps. I tried below suggested answer to merge postscripts. https://stackoverflow.com/a/3445325/13696415 why its loosing added parameter while merging? any idea? – user1992 Oct 02 '20 at 21:44
  • The ps2write device works with <> setdistillerparams when converting a pdf file to ps and also when reworking a ps file to ps with ghostscript as found by my experiments. So I would first distill and merge the 2 pdf files into a big postscript file and then set the /PSPageOptions with the ps2write device. Check your newer post about this issue for anything more. – beginner6789 Oct 04 '20 at 12:11