[英]Merging PDFs with Sejda fails with stream output
Using Sejda 1.0.0.RELEASE, I basically followed the tutorial for splitting a PDF but tried merging instead ( org.sejda.impl.itext5.MergeTask
, MergeParameters
, ...). 使用Sejda 1.0.0.RELEASE,我基本上按照本教程拆分PDF,但是尝试合并(
org.sejda.impl.itext5.MergeTask
, MergeParameters
等)。 All works great with the FileTaskOutput
: 使用
FileTaskOutput
可以很好地工作:
parameters.setOutput(new FileTaskOutput(new File("/some/path/merged.pdf")));
However I am unable to change this to StreamTaskOutput
correctly: 但是我无法将
StreamTaskOutput
正确更改为StreamTaskOutput
:
OutputStream os = new FileOutputStream("/some/path/merged.pdf");
parameters.setOutput(new StreamTaskOutput(os));
parameters.setOutputName("merged.pdf");
No error is reported, but the resulting file cannot be read by Preview.app and is approximately 31 kB smaller (out of the ~1.2 MB total result) than the file saved above. 没有错误报告,但是预览文件无法读取生成的文件,并且比上面保存的文件小31 KB(约合1.2 MB)。
My first idea was: stream is not being closed properly! 我的第一个想法是:流没有正确关闭! So I added
os.close();
所以我添加了
os.close();
to the end of CompletionListener
, still the same problem. 到
CompletionListener
的末尾,仍然是同样的问题。
Remarks: 备注:
StreamTaskOutput
is that this merge logic will live in a web app, and the merged PDF will be sent directly over HTTP. StreamTaskOutput
的原因是此合并逻辑将存在于Web应用程序中,并且合并的PDF将直接通过HTTP发送。 I could store the temporary file and serve that one, but that is a hack. Turns out, the reason is that StreamTaskOutput
zips the result into a ZIP file! 原来,原因是
StreamTaskOutput
将结果压缩为ZIP文件! OutputWriterHelper.copyToStream()
is the culprit. 罪魁祸首是
OutputWriterHelper.copyToStream()
。 If I rename merged.pdf
to merged.zip
, it's a valid ZIP file containing a perfectly valid merged.pdf
file! 如果我将
merged.pdf
重命名为merged.zip
,这是一个有效的ZIP文件,其中包含完全有效的merged.pdf
文件!
Could anyone (dear authors of the library) comment on why this is happening? 任何人(图书馆的亲爱的作者)都可以评论为什么会这样吗?
The idea is that when a task consumes a MultipleOutputTaskParameters
producing multiple output documents, the StreamTaskOutput
has to group them to be able to write all of them to a stream output. 这个想法是,当任务使用产生多个输出文档的
MultipleOutputTaskParameters
, StreamTaskOutput
必须对其进行分组,以便能够将所有文档写入流输出。 Unfortunately Sejda currently applies the same logic to SingleOutputTaskParameters
, hence your issue. 不幸的是,Sejda当前将相同的逻辑应用于
SingleOutputTaskParameters
,因此是您的问题。 We can fix this in Sejda 2.0 because it makes more sense to directly stream the out document in case of SingleOutputTaskParameters
. 因为它更有意义,直接流出来的文档中的情况下,我们可以在Sejda 2.0解决这个
SingleOutputTaskParameters
。 For Sejda 1.x I'm not sure how to address this remaining compatible with the existing behaviour. 对于Sejda 1.x,我不确定如何解决与现有行为兼容的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.