views:

254

answers:

5

I am trying to merge two or more postscript files into one. I tried concatenation but it does not work as each postscript file may have different resource header.

Have anyone done this before? Are there any libraries (commercial or open source) out there? I do not mind C++, C# or even Java libraries.

Edited These are large postscript files (more than 200 Mb) and their purpose is only for color printing (not for online viewing).

Conclusion

  1. ps2write is not the answer as it does not support DSC.
  2. pswrite as reader pipitas has correctly pointed out produces L1 output. It is not the soluton.
  3. Using pdfwrite is workable. In this option, we convert two ps to a PDF and then convert the merged PDF to a ps. There may be a problem with this solution as there may be some information lost during the conversion. Besides the extra conversion steps take additional resources and time.
  4. If we do not need to view the output file, concatenating two postscript file together with the following line "false 0 startjob pop" inserted in between the files is also a solution. (See also this link)

In conclusion, the interim solution to merge two postscript files are option 3 or 4.

+2  A: 

Here is an example Ghostscript commandline, which would convert and merge the two (or more) PostScript files into one PDF in a one go:

 gswin32c.exe ^
   -o c:/path/to/output.pdf ^
   -sDEVICE=pdfwrite ^
   -dPDFSettings=/Screen ^
   [...more desired parameters (optional)...] ^
   /path/to/first.ps ^
   /path/to/second.ps ^
   /path/to/third.pdf

Edit: my first shot had falsely assumed PDF input files. It works of course with PostScript as well (or even a mix of PS/PDF)... And the output may also be PS.

pipitas
Hi pipitas. Thanks for the tips (I give you 1 point) however I cannot use the pdf approach as it would need me to convert from postscript to pdfs and then merge pdfs back to postscript before converting the merged pdf to postscript. I have gone down this path before and it was very very slow (the process did not complete; I stopped it after 6 hours). Note: I have very large postscript files. Are you aware of a pure postscript appraoch?
Syd
You've got quite a few "conversions" to many here: *would need me to convert from postscript to pdfs and then merge pdfs back to postscript before converting the merged pdf to postscript* ?? -- It also depends on your print service providers. Sometimes these guys **DO** prefer PDF (and PDF is smaller, since it is compressed), and sometimes they even do have printers which consume PDF (not PostScript). -- If your process did not complete, you didn't know switches to tweak Ghostscript for performance and higher RAM allowances?
pipitas
@papitas.. typo on my previous comment. the pdf approach is 1) convert each postscript to pdf2) merge the pdfs into one pdf3) convert the merged pdf to postscript.
Syd
@Syd: for a pure PS workflow, see my second answer.
pipitas
@papitas. thanks for the updated answer. I remember I have tried this before but I lost the high resolution in the merged file --> gswin32c.exe -o C:\Temp\pscript\test\output.ps -sDEVICE=pswrite %1 %2. I also remember setting the "-r" option but it the output size became far too large. Are there any parameters I should be using to keep the high res of the original document?
Syd
@Syd: `pswrite` is the PostScript device that by default may write out PostScript Level 1 data. And PS L1 doesn't know some hi-level operators which Adobe added to the language later, at PS L2 and PS L3. Hence the bigger output sizes (f.e. by pixelization of some font types). You could try to add `-dLanguageLevel=3` to your command. (However, currently Ghostscript's level 3 generates the same output as its level 2 does...)
pipitas
@Syd: the `ps2write` output device also converts fonts into bitmap fonts if those are not available otherwise (f.e. if embedding is forbidden by its license). I don't have much time now, but later tonight I may be able to workout a complete commandline with all the options that may be useful for you to minimize the PS output filesize without compromizing the print quality...
pipitas
@Papitas, I will mark your answer as the best answer. From my research, ps2write does not support DSC. pswrite as you have pointed out produces L1 output. The large file is caused by the rastering. At this point, there are two options: 1) is to use pdfwrite to convert the two ps to a PDF and then convert use merged PDF to a ps. 2) to concat the two postscript files (with the "false 0 startjob pop" between the files). both are not ideal but at least will produce a working output. Until there is a better solution, your advise has given to me at least an interim solution. thank you very much :)
Syd
Hi pipitas, I am so sorry. I just realised that I have been mistyping your name all this while. My bad. My sincere apologies :)
Syd
+1  A: 

Of course you can also merge various input files (PS, PDF or a mix of them) into one PostScript file. I'll include a few more tweaking parameters into the next example commandline, which will increase the RAM allowance for Ghostscript by 800 Mb (provided you have a machine with that much of memory):

 gswin32c.exe ^
   -o c:/path/to/output.ps ^
   -sDEVICE=ps2write ^
  -c "800000000 setvmthreshold" ^
   [...more desired parameters (optional)...] ^
   /path/to/first.ps ^
   /path/to/second.ps ^
   /path/to/third.ps

You should state which application did create your PostScripts, and with what kind of settings. Only then you can expect some more specific advice. Your PostScripts may f.e. include hi-res pictures (e.g. at 1200dpi) whereas your print device may only be capable of 600dpi. In that case downsampling to 600dpi would make the files considerably smaller without necessarily imposing quality penalties.

pipitas
Thanks pipitas. Saw your answer with ps2write instead of pswrite. Add one more point to you for the second answer. I will try that at the next convenience. Oops. regarding your question, the application that produces the postscript is a third party vendor product.
Syd
@papitas - Added my comments below.
Syd
A: 

@pipitas. I have tried the command ps2write. The merged file seemed to keep the resolution but I can only see the first page (out of total 18). If I ignored the DSC error in GhostView,

1) it will show all the pages using GhostView. However I find that GhostView could not properly handle the resultant output (e.g. page up does not work).

2) I try to print to the laser printer but it failed with an ERROR : invalidaccess.

The DSC error is

DSC Information
At line 2317:
  /CharSet(/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/R/S/T/U/V/W/Y/Z/a/at/b/bar/c/colon/comma/d/dollar/e/eight/f/five/four/g/h/hyphen/i/j/k/l/m/n/nine/o/one/p/parenleft/parenright/period/q/quoteright/r/s/seven/six/slash/space/t/three/two/u/underscore/v/w/x/y/zero)/F

Lines in DSC documents must be shorter than 255 characters.
Response = Ignore All DSC

Any hints? Maybe I need to post process the output file?

Updated: here are the lines before and after line 2317

<</Type/FontDescriptor/FontName/FXQYHF+ArialMT-Regular/FontBBox[-28 -210 981 782]/Flags 34
/Ascent 782
/CapHeight 728
/Descent -210
/ItalicAngle 0
/StemV 100
/MissingWidth 278
/XHeight 530
/CharSet(/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/R/S/T/U/V/W/Y/Z/a/at/b/bar/c/colon/comma/d/dollar/e/eight/f/five/four/g/h/hyphen/i/j/k/l/m/n/nine/o/one/p/parenleft/parenright/period/q/quoteright/r/s/seven/six/slash/space/t/three/two/u/underscore/v/w/x/y/zero)/FontFile 192 0 R>>
endobj
192 0 obj
<</Filter[/ASCII85Decode
/LZWDecode]
/Length1 1796
/Length2 19365
/Length3 533/Length 34855>>stream
Syd
@Syd: Just as a test, try to convert the resulting PostScript to PDF to see which error messages Ghostscript produces(`gswin32c -o ouput.pdf -sDEVICE=pdfwrite c:/path/to/input.pdf`). Which version of Ghostscript do you have? (output of `gswin32c.exe -h` or `gs -h`) --
pipitas
Good morning pipitas. Thank you so much for your continued perseverance in my quest. I tried two options: 1) gswin32c -o output2.pdf -sDEVICE=pdfwrite test1.ps test2.ps. 2) gswin32c -o output2.pdf -sDEVICE=pdfwrite output2.ps. Both pdf worked! No errors. I am using GPL Ghostscript 8.62 (2008-02-29)
Syd
@Syd: can you lookup line 2317 in your PS file throwing the DSC error? Please also quote 1 line before and 1 line after after.
pipitas
@pipatas, updated above. thanks :)
Syd
+1  A: 
dreamlax
@dreamlax. Thanks for your response. psmerge is not available in the Windows world (not part of the ghost utilities). Probably it is available in one of the cgywin toolset. Having said this, thanks for point out to the strict conformance of DSC format before it could be used (1 point). My research has shown that many users do not have have much success with psmerge. Maybe I am better with just "false 0 startjob pop" command in between the postscript file as an interim solution.
Syd
@Syd: How about a simple `/saveobj save def` at the beginning of each document and `saveobj restore` at the end? I'm not sure whether that has an equivalent effect.
dreamlax
@dreamlax. it does not make any difference to "false 0 startjob pop". but thanks for the suggestion (+1 to your comment).
Syd
A: 

'+1' for displaying the same naïvety as I did, when I was trying to concatenate 2 Word docs a few years ago... ;-)

yes. we all learnt from our mistakes :-). In the postscript world, the pure concatenation may actually work except that it does not conform to DSC. one of the trick is to insert "false 0 startjob pop" in between the files. <<content from first postscript file>><<the postscript command (above)>><<content from 2nd postscritp file>>
Syd