Saving PDF with number that is too large for some PDF readers results in broken PDF
What were you trying to do?
I have a PDF to which I want to add some text on the bottom and top of each page for the purpose of watermarking. The result is a new PDF file that is distributed to users.
Here's the original PDF. The file can be opened with Adobe Reader. Opening the file with Preview under Mac OS also works.
Upon parsing the file I get a warning similar to issue #1029.
Parsed number that is too large for some PDF readers:
As suggested in #1029 I'm ignoring the warning and - for the sake of demonstrating the problem - just saving the parsed with w/o any modifications.
Here's the resulting PDF. Again, no modifications applied, I just saved the parsed file.
Problem: the new file cannot be opened with Adobe Reader anymore. Opening it with Preview still works though.
How did you attempt to do it?
// reading file
const data = await readFile(file_in)
// parsing file
const rawPdf = await pdfLib.PDFDocument.load(
data, { ignoreEncryption: true }
)
// saving file
const pdfBytes = await rawPdf.save()
fs.writeFile(file_out, pdfBytes, (fs_err) => {
if(fs_err) console.log(err)
})
What actually happened?
The resulting file is corrupt and cannot be opened with Adobe Reader anymore.
What did you expect to happen?
Since the original file file - despite containing numbers that are too large as the parsing error suggests - could be opened with Adobe Reader and was technically ok, I would have expected the same result for the new file, i.e. the file can still be opened with Adobe Reader.
How can we reproduce the issue?
- Download the original file
- Open the file in Adobe Reader --> works
- Parse and save the file into a new file
- Try open the new file in Adobe reader --> not possible, Adobe complains that the file is corrupt
Version
1.17.1
What environment are you running pdf-lib in?
Node
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
It would already help to add a new method to query the pdf-lib instance about any warning.
const rawPdf = await pdfLib.PDFDocument.load(
data, { ignoreEncryption: true }
)
if (rawPdf.getWarning() == WARNING_INVALID_NUMBER ) {
// stop processing the file and return original file instead
// this way, the user could at least open the file
}