pdffiller-stream icon indicating copy to clipboard operation
pdffiller-stream copied to clipboard

Using it in AWS Lambda

Open czhao-nsrecom opened this issue 7 years ago • 9 comments

Hi. I am trying to use the module to fill pdf from template in a Lambda function on AWS.

my template is located in /tmp as /tmp/template.pdf. and I verified with fs the file is there. But When I run pdffiller-stream.fillForm( tmpFile, filldata); I am getting an error

pdftk exec error: Error: Unable to find file.

Is this something you can help?

pdftk is loaded as part of my lambda as per this blog

https://lob.com/blog/aws-lambda-pdftk

Thanks.

Colin

czhao-nsrecom avatar Nov 16 '17 21:11 czhao-nsrecom

Hmm - yeah it should run fine in Lambda using the approach in that blog.

On inspection, your error message (Error: Unable to find file) looks like it's coming from pdftk itself -- so the problem isn't with connecting to pdftk, but with finding your input file.

The first argument you pass to fillForm() is just a filepath which will be sent verbatim to pdftk, so my guess is that you are perhaps using a relative path, or some kind of location that isn't available to pdftk when it executes.

Could you try an absolute path to your file, and see if that works or gives you a different error?

jasonphillips avatar Jan 25 '18 22:01 jasonphillips

check out https://www.npmjs.com/package/pdffiller-aws-lambda

hubdotcom avatar Apr 12 '18 10:04 hubdotcom

You can build PDFTK from source on EC2 - AMI image, then bundle the output executable into your AWS Lambda Deployment Package. Alternatively, you could download it here at your own risks: lambda-pdftk-example

mechatroNick avatar Oct 13 '18 02:10 mechatroNick

I've gotten this working on Lambda, but yes, it requires building pdftk from the source. You should built it yourself. (not saying https://github.com/lob/lambda-pdftk-example is wrong, but I got different checksums when I built it myself)

I used the official Cent OS 6 AMI (on a t2.medium) and followed these instructions EDIT: it needs to be done on an older OS ami as libgcj has been deprecated in newer os's.

sudo yum install gcc gcc-java libgcj libgcj-devel gcc-c++
wget https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk-2.02-src.zip
unzip pdftk-2.02-src.zip
cd pdftk-2.02-dist/pdftk
make -f Makefile.Redhat

After make is done (no need to install), copy pdftk to ./bin and /usr/lib64/libgcj.so.10 to ./lib

The lambda code to get this working is really easy.

// Add ./bin to PATH, Lambda already adds ./lib to LD_LIBRARY_PATH
if (process.env.LAMBDA_TASK_ROOT) {
  process.env.PATH = `${process.env.PATH}:${process.env.LAMBDA_TASK_ROOT}/bin`;
}

Now you can use pdffiller-stream to do whatever is needed

const sourcePDF = "invoice.pdf";
const FDFData = await pdfFiller.generateFDFTemplate(sourcePDF);
return { FDFData };

Sparticuz avatar Feb 12 '19 21:02 Sparticuz

I ran into an issue when using this with AWS lambda functions. The issue is that Lambda is not allowing the package to make a temp_.fdf file in the root.

errno": -30,
   "code": "EROFS",
   "syscall": "open",
   "path": "./temp_data1573020878383t6cjo.fdf"

I try to change the location of the temp file from root to /tmp folder, but its also not working. Help will be much appreciated.

ImNoumanDilshad avatar Nov 06 '19 06:11 ImNoumanDilshad

@ImNoumanDilshad this is not related to your dest file, that's an internal temp file that the library creates internally. It's hardcoded in the index.js file.

BTMPL avatar Dec 04 '19 15:12 BTMPL

@ImNoumanDilshad this is not related to your dest file, that's an internal temp file that the library creates internally. It's hardcoded in the index.js file.

Yes, I figured it out.

ImNoumanDilshad avatar Dec 05 '19 05:12 ImNoumanDilshad

@ImNoumanDilshad What did you end up doing to get this to work?

archermc avatar Feb 17 '20 23:02 archermc

@archermc this issue can be resolved by using pdf filler's function fillFormWithOptions in which you can pass the path for the temporary directory like this fillFormWithOptions(sourcePDF,destinationPDF,values,false,'/tmp(this is the temporary path in case of lambda)',callback).

zunnurainbadar avatar Jun 08 '21 06:06 zunnurainbadar