0

I have been writing some code in Typescript to compute incremental update of two PDFs, because the library we use doesn't support this (pdf-lib)

The process is as follow:

  1. Load PDF to pdf-lib
  2. Make modifications (in my case: write a digital signature)
  3. Write the new file
  4. Compare the two files object-by-object and put the difference at the end of the original file as an incremental update, then write the xref/trailer.

My code works great, until I tested a PDF with an XREF table in an object stream. In this case, Adobe Reader complains with:

adobe error

From that I see, the format of XREF as streams is different, however my code always writes a regular XREF table at the end.

I have uploaded the original and modified PDF here: http://www.wikiupload.com/JPM2J1I9H3HD4JP

The important bits are:

  • The original file ends with an XREF stream:
8 0 obj
...
endobj
startxref
116
  • The incremental update I got ends with:
xref
0 1
0000000000 65535 f 
...
trailer
<<
/Size 79
/Root 21 0 R
/Info 19 0 R
/Prev 116
>>
startxref
152863
%%EOF

My question is: if the original PDF contains XREF streams, should the incremental update also write the XREF in the same object as replacement? Or is there a way to make the two methods work together? Or is the problem unrelated?

wormsparty
  • 2,481
  • 19
  • 31
  • Your link leads to a page requiring some login. – mkl Mar 30 '21 at 10:06
  • 1
    Other than that, your question seems to duplicate [this one](https://stackoverflow.com/a/14964889/1729265); essentially no. *If the original PDF uses classic xrefs, you need to use the same at append time. If it uses streams, you need to use streams.* as Leonhard Rosenthol expressed it. – mkl Mar 30 '21 at 10:27
  • @mkl thanks, that's what I thought but couldn't find anything when I was searching. I'll write a streamed version, then – wormsparty Mar 30 '21 at 10:43
  • Ok, so I've closed your question as duplicate thereof. – mkl Mar 30 '21 at 11:08
  • As an aside, I hope your original file additionally has a %%EOF at its end. Without it it would have been invalid to start with... – mkl Mar 30 '21 at 11:11
  • Without xref in streams, all files are perfectly valid. So I expect the problem to only be about streams. – wormsparty Mar 30 '21 at 11:17
  • I just read the spec about cross-reference, streams, and they say `However, it is possible to construct a file called a hybrid-reference file that is readable by a PDF 1.4 consumer. Such a file contains objects referenced by standard cross-reference tables in addition to objects in object streams that are referenced by cross-reference streams.` Couldn't we modify the original xref table with a XRefStm field, or am I misunderstanding something? – wormsparty Mar 30 '21 at 11:19
  • A hybrid is a very special beast. First of all it does not match your issue as there you start from a file with a cross reference _stream_ while hybrid files in all revisions have cross reference table sections, merely with an extra link to a plug-in cross reference stream. – mkl Mar 30 '21 at 11:30

0 Answers0