highkite / pdfAnnotate

Javascript library for creating annotations in PDF documents
MIT License
556 stars 90 forks source link

save() writes corrupt PDF #52

Open fiee opened 3 years ago

fiee commented 3 years ago

I found this library used in a project that should have worked before. Now, even a simple example like:

'use strict';
const annotpdf = require('annotpdf');
const AnnotationFactory = annotpdf.AnnotationFactory;

AnnotationFactory.loadFile('test.pdf').then((factory) => {
  factory.createTextAnnotation({
          page: 0,
          rect: [50, 50, 80, 80],
          contents: "Pop up note",
          author: "Max"
  })
  factory.save('test_ann.pdf')
})

... creates a PDF file that no viewer will open.

The only error message that said anything was “root object missing or corrupted”.

I installed 1.0.14

highkite commented 3 years ago

Hello fiee,

I cannot reproduce your problem. I did the following:

  1. create an empty directory for trying to reproduce your bug
  2. Create an empty file test.js with your code inserted
  3. npm install annotpdf
  4. I can open the created document test_ann.pdf with the evince PDF viewer and also firefox

So it seems to work. Also the produced PDF syntax looks valid on first glance.

Best

fiee commented 3 years ago

Thank you for looking into that.

I did the same as you and get the same result as before. Also my colleague; he found out the only viewer that can open these PDFs is Okular; he’s on some Debian stable.

I tested on MacOS 10.14, node.js 16.13.0; I tried an older version of node.js before (can’t remember which). I checked without success: Apple Preview 10.1, Safari 14.1, Foxit Reader 11.1, Acrobat Pro 9, Acrobat Reader DC 2021.007, PDF Master Editor 5.7, Skim 1.6.5, TeXshop 4.68, Affinity Designer 1.10.3 and PDF Studio Pro 2019. (The latter says “missing root catalogue”.) I.e. Apple’s, Adobe’s and some other PDF frameworks can’t handle it.

I found it works in: Firefox v.93, Opera v.80 and Inkscape 1.1 (poppler/Cairo as well as “internal lib”).

EDIT: more tests on PureOS (~Debian):

I confirm Okular 21.08 works; also Thunderbird 78.14, Firefox ESR 78.8, Inkscape 0.92, qpdfview 0.4.17, Atril 1.20.3, Chromium, Zathura, MuPDF, TeXworks and Evince. (Not all of these can show the annotations.)

Fails: PDF Mod, Xpdf, Master PDF Editor 5, PDF Studio Pro 2019, PDFsam, Foxit Reader 2.4.4

highkite commented 3 years ago

I tried it now with Adobe Acrobat Reader DC 2021.007.20099 (on windows) and pdfmod (0.9.1) on linux/ ubuntu 20.04.

It worked in both cases. However, I used node version v16.6.1. I will try with your node version v16.13.0 to see if it makes a difference.


I tested it with v16.13.0 and it is still working in Adobe Acrobat Reader DC 2021.007.20099 and pdfmod. Can you upload the corrupted PDF document?

fiee commented 3 years ago

sketchtest2_ann.pdf

I can’t really imagine that the node version is the problem.

highkite commented 3 years ago

Thank you. I think I have an idea what's going wrong. It does not like the mixing of xref objects and explicit tables. Please, can you upload the original file (without annotation). So I can test with it.


EDIT: Never mind. I extracted the original PDF part. There is really a problem in the library, i.e., the library does not work correctly and I will fix it. However, at least pdfmod is not able to work with the original file, since it is based on the PdfSharp library, that does not support iref streams. Just to let you know.

Thank you again for reporting the issue with the iref streams. I will work on that :+1:

fiee commented 3 years ago

Here’s the original file anyway: sketchtest2.pdf

We only checked with files produced by TeX (LaTeX and ConTeXt); I encountered that some libraries have problems with them (at least if we include form elements), that might be the iref streams again.

Thank you!