Giter VIP home page Giter VIP logo

fill-pdf's Introduction

fill-pdf

Build Status

A node module to fill out PDF forms (utf8 compatible).

It uses pdftk to fill out PDF forms.

Installation

npm install fill-pdf

Dependencies

You need to have the pdftk binary in your PATH.

Install on Mac OSX

Install on Ubuntu

sudo apt-get install pdftk

Usage example (with express)

const fillPdf = require("fill-pdf");
const formData = { FieldName: 'Text to put into form field' };
const pdfTemplatePath = "templates.pdf";

app.get('/filled_form.pdf', (req, res) => {
  fillPdf.generatePdf(formData, pdfTemplatePath, function(err, output) {
    if ( !err ) {
      res.type("application/pdf");
      res.send(output);
    }
  });
});

Passing Custom Arguments to pdftk

For more specific uses, you can also pass some extra arguments to pdftk. It is done by specifying them as an array, given as a third argument of the fillPdf function.

For instance, if you want to make the output PDF not editable anymore, you can append the flatten argument such as:

const fillPdf = require('fill-pdf');

const extraArgs = ['flatten'];
fillPdf.generatePdf(formData, pdfTemplatePath, extraArgs, (err, output) => {
  // ...
});

Take a look on man pdftk to get a list of all available arguments.

Acknowledgements

Based on utf8-fdf-generator

fill-pdf's People

Contributors

dapus avatar dommmel avatar jpetitcolas avatar jspizziri avatar michaeluranaka avatar paulwithap avatar peny avatar scottoneal avatar wassimsalib avatar westy92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fill-pdf's Issues

Add Release Notes/Changelog

Could you please add release notes detailing what breaking changes came with version 1.0.0?

I've seen people add a CHANGELOG.md and/or add release notes here.

Exception when filling a PDF

I was trying to fill a PDF form and I have got this exception, but I couldn't figure out what is happening. I tried to open the PDF form and manually fill it with the same data and I didn't notice any issue, except I found it is formatting my inputs (i.e if I type 9999999999 in phone number, it replaces it with (999) 999-9999)

stderr: Unhandled Java Exception:
java.lang.ClassCastException: com.lowagie.text.pdf.PRIndirectReference cannot be cast to com.lowagie.text.pdf.PdfNumber
   at com.lowagie.text.pdf.PdfReader.getNormalizedRectangle(pdftk)
   at com.lowagie.text.pdf.AcroFields.getAppearance(pdftk)
   at com.lowagie.text.pdf.AcroFields.setField(pdftk)
   at com.lowagie.text.pdf.AcroFields.setFields(pdftk)

debug: Generating - Error:
 Error: Non 0 exit code from pdftk spawn: 2
    at ChildProcess.<anonymous> (/var/tmp/sharpone/node_modules/fill-pdf/index.js:68:16)
    at ChildProcess.emit (events.js:98:17)
    at Process.ChildProcess._handle.onexit (child_process.js:820:12) [Error: Non 0 exit code from pdftk spawn: 2]

Filling Asian charicters

I'm doing some form filling and have to accommodate Japanese text. This does not seem to be supported with the default options.

let FillData = {
    text: 'ベルゼルグ王国第一王女 アイリス'
}

fillPdf.generatePdf(FillData, Form.path, function(err, output) {
      if ( !err ) {
          res.type("application/pdf");
          res.send(output);
      }
});

This will output a pdf with my text field filled with:
ã…Žã…«ã‡¼ã…«ã‡°ç”‰åł½ç¬¬ä¸•ç”‰å¥³ ㇢㇤ㅪㇹ

How do I configure this to allow the correct encoding?

Escape parenthesis

Hello! I faced with issue that parenthesis not escaped by your module.

I not sure is it issue or not. Probably I need escape them from my side, before pass field values to your module.

What do you think about it?
Is it make sense escape parenthesis in the line 22, e.g:

var value = data[name].toString().replace('\r\n','\r').replace(/(?=[()])/g, '\\');

Thanks!

Update iconv.

Can you please update the iconv version? There are some bugs preventing the current version from working on Node 6.2.

Also, it might be possible to switch to iconv-lite, depending on what features are in use.

process hangs

pdftk /Users/Desktop/medical/node_modules/fill-pdf/test.pdf fill_form - output /var/folders/bg/z3yjjyjj1g3d3w3sxdvsjyy80000gn/T/11686-1158-1knf4m9.pdf

process just hangs, am I missing something?

issue with absolute path

I had an issue when passing an absolute path to the generatePdf function. My path looked something like this /srv/www/api/services/../../assets/template.pdf because this path is generated dynamically using __dirname , I temp. fixed this issue by removing __dirname from the generatePdf in https://github.com/dommmel/fill-pdf/blob/master/index.js#L60 , but I didn't raise a pull request because I didn't test it thoroughly.

HINT: I can't use relative paths, because fill-pdf module is installed locally in a different folder than where it is installed on my production env. but my api code / folder structure is the same, that's why am forming the path to the .pdf template from the service.

Not working with checkboxes?

This library has been great, but I couldn't get it to work with checkboxes? I was using your example pdf, and the checkboxes were not being selected.

I provided a small snippet of what I've been testing and when setting PHD to 'Yes' it does not select the checkbox still.

router.get('/test.pdf', function (req, res) {

  let formData = {
    Name_Last: 'Doe',
    Name_First: 'John',
    Name_Middle: '(Sir) Francis',
    Telephone_Home: 1112223333,
    Sex: 'MALE',
    Address_2: '232343423',
    City: 'Annapolis',
    STATE: 'MD',
    ZIP: 22334,
    PHD: 'Yes',
    TRADE_CERTIFICATE: 'Yes',
    PARENS: 'a)\n>>\n<<\n/T (Name_Last)\n/V (VALUE NOT ESCAPED)\n>>\n<<\n/T (asdf)\n/V ('
  };

  fillPdf.generatePdf(formData, `${__dirname}/test.pdf`, function (err, output) {
    if (!err) {
      res.type("application/pdf");
      res.send(output);
    }
  });

});

Image support?

I'd like to add a digital signature to a template PDF as well as the form fields. I appreciate creating the digital signature is out of the scope of this project (I've already written such logic using canvas) but I'd like to insert the generated signature image into the PDF in a fixed location.

Can't check checkboxes

Hello,
I can't check thecheckboxes of this PDF: https://www.formulaires.service-public.fr/gf/cerfa_14952.do
The name of the checkboxes are:

  • FieldName: topmostSubform[0].Page1[0].Case_?_cocher12[0]
    
  • FieldName: topmostSubform[0].Page1[0].Case_?_cocher29[0]
    
  • FieldName: topmostSubform[0].Page2[0].Case_?_cocher8[0]
    
  • FieldName: topmostSubform[0].Page2[0].Case_?_cocher14[0]
    

I tried to set the values to "On", "Yes", "checked", 1.

Thanks for solving this ;)

Support custom directories for template files

Right now the directory where template files are supposed to be located is hardcoded to the ./app directory of the project root.

https://github.com/dommmel/fill-pdf/blob/master/index.js#L58

I think it would be wise to be able to specify where the templates are located yourself, using __dirname etc. might be a bit bothersome but it makes for a much more flexible project.

However, this would be a breaking change so I would really like input from @dommmel before I start working on a pull request.

Font size?

Any possible way to set font size? I have a form with limited room for input, and need to lower the font size to get data to fit.

generateFdf confusingly named

pdftk has a generate_fdf operation which appears to be completely different from this module's generateFdf. Renaming it would be best, but a comment in the code or a note on the README would help too.

Error when trying to include Form Data

For me it's working when i don't use any form Data, but as soon as i try to do something like this:

var formData={Nachname:"hallasdasdo"};

fillPdf.generatePdf(formData,file, function(output) {

                console.log("output");
                console.log(output);

                fs.writeFile(fileDestination, output, pdfCompleteCallback)


            });

I get this Error:

stderr: Unhandled Java Exception in create_output():

stderr: java.lang.ArrayIndexOutOfBoundsException: 0
at pdftk.com.lowagie.text.pdf.DocumentFont.fillEncoding(pdftk)
at pdftk.com.lowagie.text.pdf.DocumentFont.doType1TT(pdftk)
at pdftk.com.lowagie.text.pdf.DocumentFont.(pdftk)
at pdftk.com.lowagie.text.pdf.AcroFields.getAppearance(pdftk)
at pdftk.com.lowagie.text.pdf.AcroFields.setField(pdftk)
at pdftk.com.lowagie.text.pdf.AcroFields.setFields(pdftk)

Any Ideas?

Vague error: Error: spawn pdftk ENOENT

Hey there,

I'm getting a very vague error when trying to fill a pdf - Error: spawn pdftk ENOENT

For reference, I am able to pass the same data to pdf-fill-form with no issue. I would rather use your library, since the only dependency is pdftk, and that makes it easy to run on an AWS Lambda instance.

Any idea what could be going on/how to fix? I can provide more information if necessary

Getting to 1.0.0

@dommmel

Before fill-pdf hits 1.0.0, there are few major items that, I believe, should probably be ironed out:

  • Reworking generateFdf() to not use so many Buffer objects, it is quite costly especially when creating larger FDF documents.
  • Come up with several ways to output the final PDF. Currently, the return of generatePdf() returns a Buffer object, of the entire PDF, from readFile of the temporary result PDF. It would be nice to support 3 possible return modalities:
    • Buffer Object (as exists)
    • fs read stream
    • file reference
  • It would be nice if we could make the location of where the template PDFs reside, more configurable. Currently, users are relegated to template PDFs being within their projects root directory with __dirname, however this should probably be extended to fetch from anywhere in the filesystem.
  • Make GhostScript an optional dependency, currently I believe pdftk can take care of 90% of the end-users use case, and GhostScript might be more of a superfluous dependency(it can be quite costly since the OS needs to setup another child_process for it)

Let me know what you think.

[Error: Non 0 exit code from pdftk spawn: 1]

I've been getting this error on one of our servers and I'm really confused about it. Here is the full dump

{ [Error: Command failed: gs -dNOCACHE -sDEVICE=pdfwrite -sOutputFile=/tmp/116419-18385-13yaogd.pdf -dbatch -dNOPAUSE -dQUIET  /tmp/116419-18385-1c8wdss.pdf  -c quit
GPL Ghostscript 9.10: Unrecoverable error, exit code 1
]
  killed: false,
  code: 1,
  signal: null,
  cmd: 'gs -dNOCACHE -sDEVICE=pdfwrite -sOutputFile=/tmp/116419-18385-13yaogd.pdf -dbatch -dNOPAUSE -dQUIET  /tmp/116419-18385-1c8wdss.pdf  -c quit' }
[Error: Non 0 exit code from pdftk spawn: 1]
{ [Error: Command failed: gs -dNOCACHE -sDEVICE=pdfwrite -sOutputFile=/tmp/116419-18385-w4famo.pdf -dbatch -dNOPAUSE -dQUIET  /tmp/116419-18385-gssdlb.pdf  -c quit
GPL Ghostscript 9.10: Unrecoverable error, exit code 1
]
  killed: false,
  code: 1,
  signal: null,
  cmd: 'gs -dNOCACHE -sDEVICE=pdfwrite -sOutputFile=/tmp/116419-18385-w4famo.pdf -dbatch -dNOPAUSE -dQUIET  /tmp/116419-18385-gssdlb.pdf  -c quit' }

Checkboxes

Is it possible to mark checkboxes with the package in it's current version?

ENOENT on generatePdf

I'm facing issue with pdftk on mac. Apparently it cannot generate pdf:

      fillPdf.generatePdf({ name: 'alex' }, `${__dirname}/fixtures/form.pdf`, (err, output) => {
        if (err) {
          console.log(err)
        } else {
          console.log(output)
        }
      })
 { Error: ENOENT: no such file or directory, open '/tmp/119219-94314-hv0ys2.14oi.pdf'
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/tmp/119219-94314-hv0ys2.14oi.pdf' }
Error: ENOENT: no such file or directory, open '/tmp/119219-94314-hv0ys2.14oi.pdf'

pdftk is installed:

 pdftk --version

pdftk 2.02 a Handy Tool for Manipulating PDF Documents
Copyright (c) 2003-13 Steward and Lee, LLC - Please Visit: www.pdftk.com
This is free software; see the source code for copying conditions. There is
NO warranty, not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.