1

I have converted pdf file to postscript using ghostscript, while conversion, I have passed page-level parameter for the duplex option as below.

gswin32c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=output.ps \
  -c "<</PSPageOptions [ (<</Duplex false>> setpagedevice) 
  (<</Duplex true>> setpagedevice) (<</Duplex true>> setpagedevice) ] 
  /LockDistillerParams true>> setdistillerparams" -f input.pdf

Refer solution link for the above command: https://stackoverflow.com/a/64128881/13696415

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 which i passed while converting to ps. I tried below suggested answer to merge postscript. https://stackoverflow.com/a/3445325/13696415 why its loosing added parameter while merging? How to retain page level parameter while merging? some one please help.

luser droog
  • 18,988
  • 3
  • 53
  • 105
user1992
  • 169
  • 1
  • 15

1 Answers1

1

I can confirm the %%BeginPageSetup entries for setpagedevice are lost when merging 2 postscript files. Even the /LockDistillerParams fails to save the settings. Just running the postscript files again with the ghostscript ps2write device causes the output to drop the previous settings. I suspect ghostscript rewrites these every time if /PSPageOptions is missing to redo them. I don't know of a way to save the settings when merging.

I have tried two other techniques with good results.

(1) Merge the 2 postscript files and then use the ps2write device to write the desired settings to the combined postscript file.

gs -dBATCH -dNOPAUSE -sDEVICE=ps2write -sOutputFile=merged.ps -f file1.pdf file2.pdf
gs -dBATCH -dNOPAUSE -sDEVICE=ps2write -sOutputFile=merged-out.ps -c ' << /PSPageOptions [ (<</Duplex false>> setpagedevice) (<</Duplex true>> setpagedevice) (<</Duplex true>> setpagedevice) ] /LockDistillerParams true >> setdistillerparams ' -f merged.ps

(2) Use ghostscript to merge the 2 pdf files using the ps2write device and with the /PSPageOptions setdistillerparams included for an all in one operation. I have found this only works for certain pdf files. This doesn't work if the pdf files were generated with the cairographics library used by my Firefox for example even if redistilled with ghostscript.

My test here was for two 12 page well behaved pdf files. The results show the % page3 string at page 13 as desired. The strings can be changed to use the setpagedevice as needed:

gs -dBATCH -dNOPAUSE -sDEVICE=ps2write -sOutputFile=file1+2.ps -c '<< /PSPageOptions [(% page1)(% page2)(% page3)(% page4)(% page5)] /LockDistillerParams true >>setdistillerparams' -f file1.pdf file2.pdf

P.S. Please edit your original post to show the correct sDEVICE callout. And the backslash can be omitted as implied depending on the user.

beginner6789
  • 575
  • 3
  • 7
  • Thanks for your input. I have already tried this approach. It worked for me. But the problem is, we have at least 1000 documents to be converted to PS and insert duplex parameters. On average, each document will have 3pages, so we have to pass 3000 duplex parameters like (<> setpagedevice). Even I have tried to pass 3000 parameters by writing one python program to append parameters with the original Ghostscript command, but it's failing with the error "Argument list too long”. – user1992 Oct 05 '20 at 15:02
  • the reason for the error "Argument list too long” is, there is a limitation for argument length for the Linux command line. to increase that, we need to change in one config file and the max length is based on server stack memory. https://unix.stackexchange.com/a/45584 . This approach won't help me. – user1992 Oct 05 '20 at 15:04
  • Is there any other software available to merge PS other than Ghostscript? I've tried with pdftops, the result the same as Ghostscript, losing existing parameters. then found one tool called "psjoin", it's merging without losing parameters, but it makes PS files very huge size and took much time to merge many numbers of files. When I tested, it took 4hrs to merge 1000 files. But in real-time, we can be expected more number of files than this. So I gave up on this too. Please let me know if you found any other tool. – user1992 Oct 05 '20 at 15:10
  • Regarding the backslash on the original post, someone edited and added backslash to avoid horizontal scrolling. – user1992 Oct 05 '20 at 15:12