phihag/pdfform.js

error in using npm package

Closed this issue · 7 comments

I'm still new to javascript to bear with me, but when I use the npm package, imported like this
import * as pdfform from 'pdfform.js';
it get this error:

ERROR in ./node_modules/pdfform.js/dist/pdfform.minipdf.dist.js
Module not found: Error: Can't resolve './minipdf.js' in 'C:\Users\Jason\git\dev\globalflyte-webapp\node_modules\pdfform.js\dist'
ERROR in ./node_modules/pdfform.js/dist/pdfform.minipdf.dist.js
Module not found: Error: Can't resolve './minipdf_js.js' in 'C:\Users\Jason\git\dev\globalflyte-webapp\node_modules\pdfform.js\dist'

but everything is fixed when i just copy everything into /dist/ like this:
capture

I think this is either an error with the npm package or I'm doing something wrong.

go through README to use this lib in browser, https://github.com/phihag/pdfform.js/blob/master/README.md

PS: im using this lib in nodejs

ok I figured it out, I had to change the import I think using angular does mess with the importing.
thanks

ok I figured it out, I had to change the import I think using angular does mess with the importing.
thanks

Jason, could you please tell me what you had to change? I'm having the same issue. Thanks in advance!

P.S.: I'm using Angular with TypeScript.

I am using angular w/ typescript so this should help:

I import using this

import { HttpClient } from '@angular/common/http'
import * as pdfform from '../../../../node_modules/pdfform.js/pdfform.js'

then get the fields using

var pdfFieldsJson = pdfform().list_fields(pdfStream)

and then add the data I want in those fields using

pdfform().transform(pdfStream, dataJson)

Thank you Jason!
The following also worked for me:

In .angular-cli.json, put the dist-file into the scripts section:

       "scripts": [
        "../node_modules/pdfform.js/dist/pdfform.minipdf.dist.js"
       ],

Add the exported symbol in typings.d.ts:
declare var pdfform: any;

Then, in the target TS file, do not import anything, just use it like this:

pdfform.transform(pdfDoc, fillValues);

However, with this method I couldn't figure out how to use list_fields() as it isn't exported as it seems. As I don't need it anyway it doesn't matter for me.

@jmstark If you add the below line under the // Backwards compatibility only section (whatever that means) at line number 503 of pdfform.js you will be able to access list_fields in the same way as transform.

pdfform.list_fields = function(data) {return pdfform().list_fields(data);};

@leavesoftea Thanks. I prefer not to change the file however, as we use npm install and do not include pdfform (or any other npm packages) directly in our project repository. But should I need list_fields(), I guess I can use it the way Jason suggested.