简体   繁体   中英

Piping multiple inputs into Ghostscript

I am trying to create a one-line Linux command to combine two PDF files - which are downloaded from a URL - using Ghostscript. However, I do not want to create any temporary files (everything should be done in memory).

The following command does not appear to work (I tried achieving this by process substitution).

gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=combined.pdf <(curl http://example.com/one.pdf) <(curl http://example.com/two.pdf)

When I run this command, it gives me the error below.

**** Warning:  An error occurred while reading an XREF table.
**** The file has been damaged.  This may have been caused
**** by a problem while converting or transfering the file.
**** Ghostscript will attempt to recover the data.
Error: /ioerror in --run--
Current allocation mode is local
Last OS error: Illegal seek
GPL Ghostscript 9.18: Unrecoverable error, exit code 1

I believe what is happening is that the Ghostscript command is being run before the two input PDFs have had a chance to finish downloading, perhaps there is a way to wait for this to happen.

OK there are several problems here;

Firstly, Ghostscript and the pdfwrite device don't combine PDF files. The input(s) are fully interpreted, converted to graphics primitives, and dispatched to the device. Rendering devices use the graphics library to render the primitives to a bitmap, vector devices create a high level representation of the primitive and write it to the output in the appropriate language. SO its important to note that the content of the output file bears no relation to the content of the input file. Only the visual result, when rendered, should be the same.

Secondly, you can't avoid temporary files. If you pipe a PDF file via stdin to Ghostscript then it simply creates a temporary file and stores the whole file in it. This is necessary because PDF files require random access to be interpreted.

Finally, you can't pipe from stdin twice on the command line. To process two files you either need to write your own interface to Ghostscript and use the stream interface to send data, or put them both on the command line.

So: gs file1.pdf file2.pdf

What you are trying to do simply won't work, and in any event doesn't prevent the creation of the two PDF files, its just that Ghostscript creates them for you where you can't see them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM