简体   繁体   English

将多个输入插入到Ghostscript中

[英]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. 我正在尝试创建一个单行Linux命令,以使用Ghostscript组合两个PDF文件(从URL下载)。 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. 我相信正在发生的事情是,在两个输入的PDF有机会完成下载之前,正在运行Ghostscript命令,也许有一种方法可以等待这种情况发生。

OK there are several problems here; 好,这里有几个问题。

Firstly, Ghostscript and the pdfwrite device don't combine PDF files. 首先,Ghostscript和pdfwrite设备不会合并PDF文件。 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. 如果您通过stdin将PDF文件通过管道传输到Ghostscript,则它只是创建一个临时文件并将整个文件存储在其中。 This is necessary because PDF files require random access to be interpreted. 这是必需的,因为PDF文件需要随机访问才能进行解释。

Finally, you can't pipe from stdin twice on the command line. 最后,您不能在命令行上两次从stdin进行管道传输。 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. 要处理两个文件,您需要编写自己的Ghostscript接口并使用流接口发送数据,或将它们都放在命令行中。

So: gs file1.pdf file2.pdf 因此: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. 您尝试做的事情根本行不通,并且无论如何也不会阻止创建两个PDF文件,只是Ghostscript为您创建了它们,而您却看不到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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