jjwilly16 / node-pdftk Goto Github PK
View Code? Open in Web Editor NEWA wrapper for PDF Toolkit with streams and promises.
License: MIT License
A wrapper for PDF Toolkit with streams and promises.
License: MIT License
Line 72 in 4123256
Reproduce:
Run
npm install && npm test
to see:
Error: could not load a required library for this operation.
for simple
return pdftk
.input(input)
.dumpDataFields()
.output()
.then(buffer => expect(buffer.equals(testFile)).to.be.true)
.catch(err => expect(err).to.be.null);
Node js 14.14.0
Describe the bug
When attempting to burst a PDF that has an owner password set, node-pdftk throws an error with the warning from pdftk even though ignoreWarnings
is set to true and pdftk returns a 0 exit code from the same command.
To Reproduce
Grab this PDF to use for input in the below code: http://www.orimi.com/pdf-test.pdf
Code to reproduce:
const pdftk = require('node-pdftk')
pdftk.configure({ignoreWarnings: true})
pdftk.input('/tmp/pdf-test.pdf').burst().then(() => console.log('Success')).catch((err) => console.error(err))
Output:
Promise {
<pending>,
domain:
Domain {
domain: null,
_events: { error: [Function: debugDomainError] },
_eventsCount: 1,
_maxListeners: undefined,
members: [] } }
> WARNING: The creator of the input PDF:
/tmp/pdf-test.pdf
has set an owner password (which is not required to handle this PDF).
You did not supply this password. Please respect any copyright.
Running pdftk /tmp/pdf-test.pdf burst
outputs the warning but returns a 0 exit code.
Expected behavior
An error shouldn't be thrown since I'm ignoring warnings.
Describe the bug
PDFs do not open on iOS Chrome. I've tested Safari on iOS, Chrome and Firefox on Android, Chrome, Firefox and Chromium Edge on Windows and Mac, and Safari on Mac. All work, just not that iOS Chrome.
I'm porting code from an old PHP app, and the PDFs the PHP version creates will open in iOS Chrome.
To Reproduce
Steps to reproduce the behavior:
const buffer: Buffer = await pdftk
.input(blankPdfPath)
.fillForm(data)
.flatten()
.output();
Buffer is then uploaded to S3.
I tested that the PDF that's input can be opened in Chrome just fine.
PDF does not open. It downloads and asks what to open the file with (choosing Chrome again does not work).
Expected behavior
PDF should open in Chrome.
System info that creates PDF:
System info trying to open PDF:
Any other info?
I don't know what else might help you here... do you want one of the generated PDFs or the input PDF?
Is your feature request related to a problem? Please describe.
Currently, this pdftk
library will throw an error upon inputting a JSON with number property (e.g. {"some_field": 1}
).
Describe the solution you'd like
It would be good if the library will accept numbers, by converting it to string automatically. This should be achievable by using JS's Number.prototype.toString()
or some other way.
Describe alternatives you've considered
At least, it should be documented clearly that pdftk
doesn't take numbers
When using an array as input for cat command (no handles), these problems occur:
no argument given for catCommand
pdftk.input([a, b]).cat()
throws error (split method called on undefined)
empty string as catCommand
pdftk.input([a, b]).cat('')
only first file "a" used, all others ignored.
pdftk.input([a, b]).cat(' ')
only first file "a" used, all others ignored.
pdftk.input([a, b]).cat([])
works as expected
Describe the bug
The function 'updateInfo' will assume that if the parameter 'data' is a string, that it is in fact a path to a file which contains the info to be updated. If it is not a string it will assume it's an object which can be serialised to something pdftk can work with through 'generateInfoFromJSON'.
The issue is two fold; Firstly, I would like to have a way to provide a string that is simply passed on, instead of treated as if it is a path. I could serialise it into a JSON, but herein lies the second issue; the function 'generateInfoFromJSON' does not fully support everything that can be thrown at pdftk (notably; bookmarks)
To Reproduce
Pass a string to 'updateInfo'; it tries to load it as a file.
Expected behavior
I would like to be able to pass a string and simply be passed to pdftk, either because of the addition of a flag ('isFile'?) or some other mechanism. Alternatively, 'generateInfoFromJSON' could be changed to support all the info that can be supported.
Any sample code snippet to use this package to split the pdf into separate pages ?
It will be very useful if we can get one ..
Thank you !
Describe the bug
When I try to concatenate PDFs in an Express application, the resulting PDF is empty:
const documents = new Map();
documents.set('A', 'input1.pdf')
documents.set('B', 'input2.pdf')
const input_obj = Object.fromEntries(documents);
const cat_str = [ ... documents.keys()].join(' ');
const output = 'output.pdf';
pdftk
.input(input_obj)
.cat(cat_str)
.output(output)
.then(buffer => {
// Do stuff with the output buffer
documents.set('output', output)
res.json(Object.fromEntries(documents))
})
.catch(err => {
// handle errors
res.json(err)
});
The equivalent pdftk
command on the command line succeeds:
pdftk A=input1.pdf B=input2.pdf cat A B output output.pdf
To Reproduce
Steps to reproduce the behavior:
output.pdf
Expected behavior
The generated file contains all input PDFs in the specified order.
Screenshots
N/A
Desktop (please complete the following information):
Additional context
N/A
const pdftk = require('node-pdftk');
pdftk.configure({
bin: 'C:/Program Files (x86)/PDFtk Server/bin',
Promise: require('bluebird'),
ignoreWarnings: true,
});
pdftk
.input([textfile, change,log])
.output()
.then(buf => {
let path = './merged.pdf';
fs.open(path, 'w', function (err, fd) {
fs.write(fd, buf, 0, buf.length, null, function (err) {
fs.close(fd, function () {
console.log('wrote the file successfully');
});
});
});
});
This is my code. But the error i am getting is : pdftk was called but is not installed on your system.
I installed both pdftk and pdftk server. I set the pdftk_path variable to 'C:/Program Files (x86)/PDFtk Server/bin', in my environment variables too.
I configured the same through options. But still i am getting the error.
Please help me
I have this working fine locally, but it's crashing after deploying to heroku. Is it possible to get it working with Heroku?
Ideally I would have a PDF form auto generated this way.
Describe the bug
Hi @jjwilly16
first of all thank you very much for this great package.
In version 2.1.2
, a bugfix was pushed that ignores null
fields in the payload with 5c91fe8
Unfortunately it seems as if this change was not published to npm
in 2.1.2
To Reproduce
Steps to reproduce the behavior:
npm init -y
node-pdftk
packagenpm i node-pdftk
and verify in package_lock.json
that the currently installed version is 2.1.2
"node-pdftk": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/node-pdftk/-/node-pdftk-2.1.2.tgz",
"integrity": "sha512-xapuoppEweAHOLdolxinm3kmEGtQSrWxu5M3fnu+Y3ABpVGx+Xc4NpeGahTVRdFMGhufLyyoBNnUJPUwXVNU9A=="
}
node_modules/node-pdftk/index.js
and search for the generateFdfFromJSON
method (starts at line 188
).Note that in line 204
, we have the following if statement
if (data.hasOwnProperty(prop)) {
However I think it should actually be
if (data.hasOwnProperty(prop) && data[prop] !== null && data[prop] !== undefined) {
because there is a commit as of Github version 2.1.2
which fixes the FDF
null
bug: 5c91fe8
Because of this the FDF form filling still fails if any of the fields is null with the 2.1.2
version of this package installed via npm
Expected behavior
The bugfix that is already on master should also be pushed to npm
so that FDF files can be generated even if fields are null
.
The solution would be to push the latest changes to npm
.
return pdftk.input('MASTER_test.pdf').fillForm(data).flatten().output(fileName);
Saves the file locally on the server with fileName
and sends back the buffer but in numerics/numbers 378068704549465410372262.....
was expecting to see something like
%PDF-1.4
%����
1 0 obj
Is the buffer formatted? how do we get from numbers to ASCII text
I tried
private bin2string(array) {
var result = "";
for (let idx = 0; idx < array.length; idx++) {
result += String.fromCodePoint(array[idx]);
}
return result;
}
Which is close I get
%PDF-1.4
%âãÏÓ <<-- this is wrong
1 0 obj
Thanks
Is your feature request related to a problem? Please describe.
I was wondering if it would be possible to insert images.
Describe the solution you'd like
Pass a dataUri or ArrayBuffer on the data object and insert the image.
Thanks for all your work on this library. It works great!
Is your feature request related to a problem? Please describe.
Every once in a while, when calling .output()
, node-pdftk
throws an error and the only information in the error message is the string "2"
causing our logs to read ERROR: 2
.
Describe the solution you'd like
A way to turn on more verbose logging to investigate the cause of the error. Is there a way to do this currently?
I notice in pdftk doc we can uncompress a pdf for editing using following command
pdftk doc.pdf output doc.unc.pdf uncompress
I wonder does node-pdftk provide a way to do the same thing here, cannot find any info about uncompressing pdf in the doc. Thank you.
PDFTk unfortunately does not link to the most recent MacOS version from their home page. So if a user of your library uses MacOS >= 10.11, PDFTk won't work at all but they'll likely blame the wrapper.
The correct PDFTk version is at https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg
I emailed the author to update his page but this has been an open issue for a year: https://stackoverflow.com/questions/39750883/pdftk-hanging-on-macos-sierra
To have a self-contained application, the pdftk binary can be included as an asset of the application and used from there rather than having a global install of pdftk.
Can the include optionally pass param to specify the path (command) to the pdftk binary?
Describe the bug
So I followed the examples quite thoroughly. However the .output() call fails with the following exception:
(node:21488) UnhandledPromiseRejectionWarning: Error: write EPIPE
at Socket._writeGeneric (net.js:768:25)
at Socket._write (net.js:787:8)
at doWrite (_stream_writable.js:396:12)
at writeOrBuffer (_stream_writable.js:382:5)
at Socket.Writable.write (_stream_writable.js:290:11)
at Socket.write (net.js:711:40)
at _Promise ([...]\node_modules\node-pdftk\index.js:360:33)
it's thrown from the line child.stdin.write(this.stdin);
any ideas what could be causing it?
i think this is a mistake in output method. when pdftk throw a waring, even if i set ignoreWarnings() , but not use, this programe still throw err . and then ,i found this code have mistake.
if (!(this._ignoreWarnings && data.toString().toLowerCase().includes('error'))) {
it should be
if (!(this._ignoreWarnings && !data.toString().toLowerCase().includes('error'))) {
miss '!' before data.toString()
Describe the bug
Cannot set the checkbox when filling PDF.
To Reproduce
I have dumbed the avalaible fields.
$ pdftk some_pdf.pdf dump_data_fields_utf8
---
FieldType: Button
FieldName: Zaškrtávací pole 01
FieldFlags: 0
FieldJustification: Left
FieldStateOption: Ano0
FieldStateOption: Ano1
FieldStateOption: Ano2
FieldStateOption: Ano3
FieldStateOption: Ano4
FieldStateOption: Ano5
FieldStateOption: Ano6
FieldStateOption: Off
trying to set it using :
pdftk
.input(originalPdfPath)
.fillForm({
'Zaškrtávací pole 01' : 'Ano0',
'Textové pole3' : 'Test using UTF8 field name',
})
.output()
.then(buffer => {
return buffer;
}).catch((e) => {
console.error(e)
})
Expected behavior
Fill the checkbox
Desktop (please complete the following information):
Hey there,
Library looks nice! I'd like to use it for a project, but I'm a little hesitant because I don't see any tests. Is this something you'd accept a PR for?
Hello. When I try to send euro sign €, the result is ¬
I try to type it directly
const data = {
value: '10 €'
}
or like this
const data = {
value:'10 '+ String.fromCharCode(8364)
}
I'have the problème on Windows 10 and on Linux Alpine.
I try with node-pdftk 2.0.1 and 1.3
ps: It work in PHP with php-pdftk and the same pdf in input.
Describe the bug
When you set permissions in pdftk if number of permissions are more then one, pdftk return error about unexpected data in output section.
For example:
Error: Unexpected data in output section:
FillIn CopyContents
Exiting.
Errors encountered. No output created.
Done. Input errors, so no output created.
It occurs when permission array passed in spawn like concatenated string.
To Reproduce
Steps to reproduce the behavior:
const pdftk = require('node-pdftk');
// Works
pdftk
.input(['Turgor.pdf'])
.allow(['FillIn'])
.output('Turgor_secured.pdf')
.then(() => {
console.log('done 1 ');
})
.catch(err => {
console.log(1, err);
});
// Don't works
pdftk
.input(['Turgor.pdf'])
.allow(['FillIn', 'CopyContents'])
.output('Turgor_secured.pdf')
.then(() => {
console.log('done 2');
})
.catch(err => {
console.log(2, err);
});
Expected behavior
Create a pdf file with proper permissios without error.
Describe the bug
To Reproduce
pdftk
.input('./my.pdf')
.fillForm({
'listbox': 'option1'
})
Matching FDF for field fill
<<
/V (option1)
/T (listbox)
>>
pdftk
.input('./my.pdf')
.fillForm({
'listbox': 'option1,option2'
})
Matching FDF
<<
/V (\(item1,item2\))
/T (listbox)
>>
pdftk
.input('./my.pdf')
.fillForm({
'listbox': ['option1','option2']
})
Matching FDF
<<
/V (item1,item2)
/T (listbox)
>>
Expected behavior
Desktop :
Additional context
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="listbox">
<value>option1</value>
<value>option2</value>
</field>
</fields>
</xfdf>
I'm using the fillForm method, passing one JSON Object as designed. My node-pdftk version is 5.5.1, node -v: 8.9.3.
It keeps throwing the following error, with the following stack, no matter how hard I try to stick to the pattern:
TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object. at Function.Buffer.from (buffer.js:202:9) at Function.generateFdfFromJSON (/src/node_modules/node-pdftk/index.js:181:28) at PdfTk.fillForm (/src/node_modules/node-pdftk/index.js:439:52) at Object.fillForm (/src/routes/pdf/pdf_worker.js:33:14) at Object.createContractJob (/src/routes/core/flag_step.js:301:24) at callback (/src/routes/listeners/robot_signals.js:21:14) at SubscriptionDefinition.invokeSubscriber (/src/node_modules/postal/lib/postal.js:172:19) at /src/node_modules/postal/lib/postal.js:529:21 at /src/node_modules/postal/lib/postal.js:418:5 at arrayEach (/src/node_modules/lodash/lodash.js:537:11) at Function.forEach (/src/node_modules/lodash/lodash.js:9359:14) at /src/node_modules/postal/lib/postal.js:538:7 at /src/node_modules/lodash/lodash.js:4944:15 at baseForOwn (/src/node_modules/lodash/lodash.js:3001:24) at /src/node_modules/lodash/lodash.js:4913:18 at Function.forEach (/src/node_modules/lodash/lodash.js:9359:14)
As said, the FDF content is in proper json format, here it is:
{ fdf: { city: 'Zug', city_1: 'Zug', city_2: 'Zug', city_3: 'Zug', company_address: 'Gubelstrasse 12, 6300', company_address_1: 'Gubelstrasse 12, 6300', company_address_2: 'Gubelstrasse 12, 6300', company_address_3: 'Gubelstrasse 12, 6300', zip: '88584', zip_1: 'Gubelstrasse 12, 6300', zip_2: 'Gubelstrasse 12, 6300', zip_3: 'Gubelstrasse 12, 6300', date: 'Fri Jan 05 2018 07:47:39 GMT+0000 (UTC)', date_1: 'Fri Jan 05 2018 07:47:39 GMT+0000 (UTC)', date_2: 'Fri Jan 05 2018 07:47:39 GMT+0000 (UTC)', signature_date: 'Fri Jan 05 2018 07:47:39 GMT+0000 (UTC)', date_end: 'Thu Apr 05 2018 07:47:39 GMT+0000 (UTC)', hour_rate: '30 Euros', client_name: 'André', client_name_1: 'André', client_name_2: 'André', client_name_3: 'André', city_zipcode: '88485000' }, contract_id: '5a4f889b81d40e457ef2dd38' }
How can I fix that?
I have the following piece of code:
addWatermark: (input, output, cb) => {
pdftk
.input(input)
.stamp('./pdftools/logo.pdf')
.output(output)
.then((buffer) => {
logger.info('WATTERMARK-APPLYING-LOG: going to call watermarkCallback');
cb("", "pdf has been branded", "");
})
.catch((err) => {
logger.info("PDF-BRANDING - WATTERMARK-APPLYING-ERROR: " + err);
cb(err, null, err);
});
}
It's a pretty straight forward piece of code, no magic, nothing really to it, just vanilla node-pdftk.
It executes the input method, then the stamp one, and the output.
The problem is that it never reaches the Then, and Catch methods. It just falls outside the method.
I tried many different approaches, but I cannot seem to find the reason.
Input, output, and cb params are loaded correctly, but yet, Then and Catch do not work.
I'm using node-pdfk v 1.3.0
edit: I've updated to node-pdftk v 1.3.1 (latest) and the bug continues.
I've got an error Fatal error: spawn ENAMETOOLONG when trying to process too many files or files has too long pathes.
Is it possible to implement same solution as here to use stdin:
the .cat() method isn't working with buffers as input.
" does not exist at Function.input (/node_modules/node-pdftk/index.js:88:68) at pdftk.input.stamp.output.then.catch.then.buffer (/index.js:108:6) at <anonymous>
when outputting the used buffers as files and then cat, it works as expected.
pdftk .input({ A: fs.readFileSync('./page1.pdf'), B: './page2.pdf', }) .cat('A B')
Hi,
This is not an issue but a question. I was getting an exception using pdftk v 2.02 and the stacktrace has reference to the iText lib author Lowagie.
Unhandled Java Exception in create_output():
java.lang.ArrayIndexOutOfBoundsException: 0
at pdftk.com.lowagie.text.pdf.DocumentFont.fillEncoding(pdftk)
On googling I found Lowagie himself commenting that pdftk is using an obsolete version of iText lib.
My question is, is the pdftk version 2 still using iText lib as I got the above exception with pdftk version 2? If yes, which version of iText is being used in the current version of pdftk?
Thanks
Shaleen
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.