Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using it in AWS Lambda #2

Open
czhao-nsrecom opened this issue Nov 16, 2017 · 9 comments
Open

Using it in AWS Lambda #2

czhao-nsrecom opened this issue Nov 16, 2017 · 9 comments

Comments

@czhao-nsrecom
Copy link

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

@jasonphillips
Copy link
Owner

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?

@hubdotcom
Copy link

@mechatroNick
Copy link

mechatroNick commented Oct 13, 2018

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

@Sparticuz
Copy link

Sparticuz commented Feb 12, 2019

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 };

@ImNoumanDilshad
Copy link

ImNoumanDilshad commented Nov 6, 2019

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.

@BTMPL
Copy link

BTMPL commented Dec 4, 2019

@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.

@ImNoumanDilshad
Copy link

@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.

@archermc
Copy link

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

@zunnurainbadar
Copy link

zunnurainbadar commented Jun 8, 2021

@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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants