vizicsaba89 / pdf-signer

sign pdf documents
MIT License
37 stars 33 forks source link

Signing multi-page PDFs created from other sources. #8

Closed caribviper closed 4 years ago

caribviper commented 4 years ago

Discovered that the library was not allowing the signing of PDFs created from MS Word or puppeteer. Specifically multi-page documents. The error seems to be how the page is referenced in your function getPageRef, specifically the last line:

// current code 
const split = shouldAnnotationAppearOnFirstPage ? pages.trim().substring(0, 5) : pages.trim().substring(pages.length - 7, pages.length)

I have replaced the above line as follows and it seem to work

const split = (shouldAnnotationAppearOnFirstPage ? pages.trim().substring(0, 5) : pages.trim().substring(pages.length - 7, pages.length)).trim().split(' ', 3);

Also discovered error on same page references for first and last pages. Thus final changes for function:

const getPageRef = (pdf: Buffer, info: any, shouldAnnotationAppearOnFirstPage: boolean = false) => {
  const pagesRef = getPagesDictionaryRef(info)
  const pagesDictionary = findObject(pdf, info.xref, pagesRef)
  const kidsPosition = pagesDictionary.indexOf('/Kids')
  const kidsStart = pagesDictionary.indexOf('[', kidsPosition) + 1
  const kidsEnd = pagesDictionary.indexOf(']', kidsPosition)
  const pages = pagesDictionary.slice(kidsStart, kidsEnd).toString()
  const pagesSplit = pages.trim().split(' ');
  const split = shouldAnnotationAppearOnFirstPage ? pagesSplit.slice(0, 3): pagesSplit.slice(pagesSplit.length - 3);
  return `${split[0]} ${split[1]} ${split[2]}`

By the way, I am enjoying using your library

pankucsi commented 4 years ago

Unfortunately in the moment, you can only sign pdf which contains xref table.

I dont know puppeteer / MS Word how generating those pdf files. Can you share those files so we can investage them when we have time for that?

In the other hand feel free to create a PR. In the future we should create more test and set up a nice flow for deploying. We will prepare better for these cases.

And thank you for the feedbacks. We really appricate it.

caribviper commented 4 years ago

I have attached the pdf from word.

pdf_from_word.pdf

And a puppeteer pdf is generated from any print to pdf within chrome/chrome based browser. Also I do believe both contain xref tables as having made the adjustment above, I am able to see them. Hope this helps.

vizicsaba89 commented 4 years ago

The problem is that the last page of the document has a pageref index of 11, and unfortunately we only handle 1 digit page references :( Thank you for finding this bug @caribviper, it'll be fixed soon!

mcalcano commented 4 years ago

Discovered that the library was not allowing the signing of PDFs created from MS Word or puppeteer. Specifically multi-page documents. The error seems to be how the page is referenced in your function getPageRef, specifically the last line:

// current code 
const split = shouldAnnotationAppearOnFirstPage ? pages.trim().substring(0, 5) : pages.trim().substring(pages.length - 7, pages.length)

I have replaced the above line as follows and it seem to work

const split = (shouldAnnotationAppearOnFirstPage ? pages.trim().substring(0, 5) : pages.trim().substring(pages.length - 7, pages.length)).trim().split(' ', 3);

Also discovered error on same page references for first and last pages. Thus final changes for function:

const getPageRef = (pdf: Buffer, info: any, shouldAnnotationAppearOnFirstPage: boolean = false) => {
  const pagesRef = getPagesDictionaryRef(info)
  const pagesDictionary = findObject(pdf, info.xref, pagesRef)
  const kidsPosition = pagesDictionary.indexOf('/Kids')
  const kidsStart = pagesDictionary.indexOf('[', kidsPosition) + 1
  const kidsEnd = pagesDictionary.indexOf(']', kidsPosition)
  const pages = pagesDictionary.slice(kidsStart, kidsEnd).toString()
  const pagesSplit = pages.trim().split(' ');
  const split = shouldAnnotationAppearOnFirstPage ? pagesSplit.slice(0, 3): pagesSplit.slice(pagesSplit.length - 3);
  return `${split[0]} ${split[1]} ${split[2]}`

By the way, I am enjoying using your library

Thank you for your input @caribviper, i was having the same issue with any multi-page PDF from any source (from html-pdf, fpdf, word, windows print to pdf, etc.). This solved the issue at least for now.

pankucsi commented 4 years ago

@mcalcano thanks for the suggestions.

For now we fixed the issue, but the code is little messy. We didnt have enough time to manage this repository, but we would like improve it in the future. So play stay tuned 😁

I had merge the branch to master and published a new version of it, so i am closing this issue. If you guys find any other bug, or want a new feature feel free to open it.