Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.9k stars 657 forks source link

It's possible fill any pdf or only if I create a form with the same tool? #609

Closed aismaniotto closed 4 years ago

aismaniotto commented 4 years ago

I am digging up for a while e tring to use this tool only to fill a pdf that I already have, but with no success. That's raise me the doubt if that's is possible.

So, it's possible fill a pdf easily? If yes, some trick?

Hopding commented 4 years ago

Hello @aismaniotto! Yes, this is certainly possible to do. pdf-lib has a new form filling API that makes this quite easy. Here’s an example: https://github.com/Hopding/pdf-lib#fill-form. See the README and API docs for more info.

aismaniotto commented 4 years ago

Yesss, I see that. But i don't know how to get the field of my pdf, because thats pdf wasn't me who create, it's a generic government pdf, that's why I ask if I also need create the form. Get it?

https://www.icmbio.gov.br/cepsul/images/stories/mapa_bordo/mb_anexo_ii_espinhel_long_line_superficie.pdf

Hopding commented 4 years ago

You can open the document in some PDF readers (e.g. Adobe Acrobat) to see what the field names are. You can also get all the fields and print out their names programmatically. See https://pdf-lib.js.org/docs/api/classes/pdfform#getfields. E.g.

const form = pdfDoc.getForm()
const fields = form.getFields()
fields.forEach(field => {
  const type = field.constructor.name
  const name = field.getName()
  console.log(`${type}: ${name}`)
})
aismaniotto commented 4 years ago

Using this programmatically I received a empty array :+1: So, I should guess that is not possible use this package on this pdf?

aismaniotto commented 4 years ago

Finded it the answers that I was looking for :) With the currently pdf it's not possible fill it, but I can use this package to create the form on the pdf or some another tool. Any way, thanks

Hopding commented 4 years ago

@aismaniotto I took some time yesterday evening and today to look at the PDF you shared. The reason that pdf-lib returned an empty array is because the PDF doesn't actually contain any form fields whatsoever. You can confirm that this is the case by opening the document in any PDF reader, including Adobe Acrobat, and attempting to edit the form fields. You will be unable to do so. I tried it myself with Preview, Foxit Reader, Adobe Acrobat, and Google Chrome. None of them found any form fields in the PDF.

However, you can modify the document to add form fields to it. You can do this with pdf-lib if you like. But you'll have to manually figure out all the coordinates for the text fields in the PDF's various tables. A better alternative might be to use another tool to scan the PDF you shared and produce a modified version with form fields. Adobe Acrobat is capable of doing this, so I've gone ahead and done it for you already. Here's a copy of your document with form fields added by Acrobat: with_form_fields-mb_anexo_ii_espinhel_long_line_superficie.pdf.

If you use the above snippet to load this new document and print out its form fields, you will see the following (truncated for brevity):

PDFTextField: Lance N
PDFTextField: Lance N_2
PDFTextField: Lance N_3
PDFTextField: LançamentoData diamês
PDFTextField: RecolhimentoData diamês
PDFTextField: LançamentoData diamês_2
...
PDFTextField: N Reg SEAPPR
PDFTextField: N InscCapPortos
aismaniotto commented 4 years ago

Thank you so much for that. I figured that lattely and start to work with some tool to make this form. Honestly, I took to much time to understand that, not because your doc (your doc is great), because I actually never has seen a fillable PDF before hehe Only one last doubt, the tool on Adobe Acrobat to create the form on pdf is a premium or free feature? And again, thank you soo much.

Hopding commented 4 years ago

Pretty sure it's a premium feature. Although, you can get a trial version of Acrobat Premium for free (seems like it expires after a month or so). And no worries about your confusion. PDFs are hard ¯\(ツ)

vbgsmanzoor commented 3 years ago

I want to get PDF field Names with Page number, is it possible ?