galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 170 forks source link

Error when using pdf-form-fill.js #223

Open jhauckster opened 6 years ago

jhauckster commented 6 years ago

Hello and thank you for developing HummusJS! For years we have been working in a Java environment where we take an existing pdf containing a form, fill the form values programmatically, and insert a signature image. We are looking to recreate this functionality in nodejs - which is how we came upon HummusJS.

I tested one of our existing PDFs using the pdf-form-fill.js code that you provided, but I get the following error:

TypeError: inheritedProperties is not a function at writeAppearanceXObjectForText (/usr/src/app/src/pdf-form-fill.js:136:90) at writeFieldWithAppearanceForText (/usr/src/app/src/pdf-form-fill.js:224:3) at updateTextValue (/usr/src/app/src/pdf-form-fill.js:256:3) at updateFieldWithValue (/usr/src/app/src/pdf-form-fill.js:323:7) at writeFilledField (/usr/src/app/src/pdf-form-fill.js:388:5) at /usr/src/app/src/pdf-form-fill.js:436:7

Attached is the pdf called reg2.pdf and here is my node server code:

app.get('/fill', function (req, res) { try { res.writeHead(200, {'Content-Type': 'application/pdf'}) var pdfWriter = hummus.createWriterToModify( new hummus.PDFRStreamForFile(__dirname + "/reg2.pdf"), new hummus.PDFStreamForResponse(res) ) var data = { "firstName": "Eric", "lastName": "Jones", "gender": "M", "financeOption": "Direct Pay" }

fillForm(pdfWriter, data);

pdfWriter.end()

res.end()

} catch (err) { res.end() console.log('fill error', err) } })

Any idea why I am getting this error?

Thanks for your help,

Joe reg2.pdf

DamickDoni commented 6 years ago

try it with createWriterToModify like this

var writer = hummus.createWriterToModify('path/to/input', { modifiedFilePath: 'path/to/output' }); var data = {yourdatas }; fillForm(writer, data); writer.end();

You can also try the PDFTK framework. Works better and you don't have problems with corrupted files.

jhauckster commented 6 years ago

Thanks @DamickDoni, I appreciate the quick response. I have pretty much the same code as what you suggested, except I am outputting to the node response object rather than to a file. I tried using your suggestion and reading and writing to a file instead of using the response object and I get the same "inheritedProperties is not a function" error.

I have also looked at PDFTK and it looks like it will fill the pdf form, but I haven't been able to figure out how to write an image to the pdf document in a specific location on one of the pages. Any suggestions?

chunyenHuang commented 6 years ago

For your original question, inheritedProperties should be an object. So the easy fix is you change the following line: https://github.com/galkahana/HummusJSSamples/blob/master/filling-form-values/pdf-form-fill.js#L136

inheritedProperties('DA') > inheritedProperties['DA']

I have created a PR for it.

For the second question, you can read this simple-image-drawing

jhauckster commented 6 years ago

Thank you @chunyenHuang for your message. I changed the code in my pdf-form-fill.js file from inheritedProperties('DA') to inheritedProperties['DA']. The error is not occurring now, but unfortunately the form is not being filled - it is just displaying the blank pdf document. I added some console statements to the code to try to debug. When I look at the fieldsDictionary object (from line 136) it is: { F: PDFInteger { value: 4 }, FT: PDFName { value: 'Tx' }, Ff: PDFInteger { value: 1 }, MK: PDFDictionary {}, P: PDFIndirectObjectReference {}, Q: PDFInteger { value: 1 }, Rect: PDFArray {}, Subtype: PDFName { value: 'Widget' }, T: PDFLiteralString { value: 'firstName' }, Type: PDFName { value: 'Annot' } }

When I look at the inheritedProperties object (from the same line), it is just an empty object. Not sure what the da variable is used for, but it doesn't seem to be set correctly.

Any help would be greatly appreciated.

Joe

chunyenHuang commented 6 years ago

I am not sure about this but I think it's covered by other objects or the behavior needs to be defined.

Based on your original post

var data = {
    "firstName": "Eric",
    "lastName": "Jones",
    "gender": "M",
    "financeOption": "Direct Pay"
}

It did fill out the form but input like firstName and lastName requires a focus to display and the gender is in wrong field. screen shot 2017-11-21 at 10 46 45 am

I think I will need some time to investigate this.

jhauckster commented 6 years ago

Thanks @chunyenHuang I appreciate you looking into it.

chunyenHuang commented 6 years ago

Since your gender and financeOption fields are fieldType==btn, so you may have to change the input to

{
    "gender": 1, // 0-Female, 1-Male
    "financeOption": 2 // 0-Direct Pay, 1-Not Interest, 2-Care Credit
}

And you will have to modify the pdf-form-fill.js so it can actually pass the btn value instead of using 0. https://github.com/galkahana/HummusJSSamples/blob/7154b35fc79e27a5864af273ab3d2bcf219d25a7/filling-form-values/pdf-form-fill.js#L317

// from
updateOptionButtonValue(handles,fieldDictionary,(flags>>15) & 1 ? value : (value ? 0:null));

// to
var buttonValue = (!flags) ? value : (flags >> 15) & 1 ? value : value ? 0 : null
updateOptionButtonValue(handles, fieldDictionary, buttonValue);

I am still investigating on the text fields.

jhauckster commented 6 years ago

Thanks @chunyenHuang, I'll make that change and let you know how if works.

In case it helps, attached is a pdf that was generated using the old java way of doing it. I wonder if that process replaced the form fields with text?

I appreciate your help.

Joe hummus sample filled 12-3-2017.pdf

formspoint commented 6 years ago

@jhauckster @chunyenHuang where you ever able to get form-fill working with hummusjs? If so, could you post an simple example somewhere?

m1m1s1ku commented 6 years ago

Form filling "works" partially, in my case, I have values written when I click on a form field, but nothing displayed when print.. ><

I tried many PDF versions, different cases, maybe someone has an idea ?

After many tries, i think that this issue is related to the size of form fields.. I can't make it work for almost 80% of a simple document :/

mohammedabualsoud commented 5 years ago

@chunyenHuang I have the same issue the text controls values are displayed only if you focus on them. Why this is happening ?