[ 
https://issues.apache.org/jira/browse/PDFBOX-5130?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17302818#comment-17302818
 ] 

Tilman Hausherr edited comment on PDFBOX-5130 at 3/16/21, 7:13 PM:
-------------------------------------------------------------------

Change your code to this
{code:java}
try (FileOutputStream fileOutputStream = new FileOutputStream(mergedFileName))
{
    PDFMergerUtility pdfmerger = new PDFMergerUtility();
    pdfmerger.setDestinationStream(fileOutputStream); 
    pdfmerger.addSources(streams);
    pdfmerger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
} {code}
The problem with your code is that you're opening each stream 3 times so it 
gets consumed. Once with load(), then once because you add the array (instead 
of the stream) each time, and once when merging.


was (Author: tilman):
Change your code to this
{code:java}
try (FileOutputStream fileOutputStream = new FileOutputStream(mergedFileName))
{
    PDFMergerUtility pdfmerger = new PDFMergerUtility();
    pdfmerger.setDestinationStream(fileOutputStream); 
    for (InputStream stream : streams)
    {
        //PDDocument document = PDDocument.load(stream);
        //pdfmerger.addSources(streams);
        pdfmerger.addSource(stream);
        //document.close();
        //stream.close();
    }
    pdfmerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
} {code}
The problem with your code is that you're opening each stream 3 times so it 
gets consumed. Once with load(), then once because you add the array (instead 
of the stream) each time, and once when merging.

> Getting Error: End-of-File, expected line while merging input streams to 
> create a single pdf file 
> --------------------------------------------------------------------------------------------------
>
>                 Key: PDFBOX-5130
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5130
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Parsing
>    Affects Versions: 2.0.22
>         Environment: java
>            Reporter: Amar Kumar
>            Priority: Major
>         Attachments: BaseToPdf.java
>
>
> Getting below while merging input streams to create a single pdf file 
> Error: End-of-File, expected line



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to