Skip to content

Commit

Permalink
escape single parenthesis when generating fdf files
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Williamson committed Mar 10, 2020
1 parent 5c91fe8 commit 4123256
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
17 changes: 15 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ class PdfTk {
return new Buffer(str, encoding);
}

/**
* Sanitizes fdf input
* @statuc
* @public
* @param {String} str - String to sanitize
* @returns {String} Sanitized string
*/
static sanitizeForFdf(str) {
return str
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)');
}

/**
* Creates fdf file from JSON input.
* Converts input values to binary buffer, which seems to allow PdfTk to render utf-8 characters.
Expand Down Expand Up @@ -208,15 +221,15 @@ class PdfTk {
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(prop.toString(), 'binary'),
PdfTk.stringToBuffer(PdfTk.sanitizeForFdf(prop.toString()), 'binary'),
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(')\n/V('),
]);
body = Buffer.concat([
body,
PdfTk.stringToBuffer(data[prop].toString(), 'binary'),
PdfTk.stringToBuffer(PdfTk.sanitizeForFdf(data[prop].toString()), 'binary'),
]);
body = Buffer.concat([
body,
Expand Down
1 change: 1 addition & 0 deletions pretest.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const commands = [
'test/files/form.pdf fill_form test/files/formwithnumber.fdf output test/files/filledformwithnumber.temp.pdf',
'test/files/form.pdf fill_form test/files/form.fdf output test/files/filledformflat.temp.pdf flatten',
'test/files/form.pdf fill_form test/files/form.blank.fdf output test/files/filledformempty.temp.pdf flatten',
'test/files/form.pdf fill_form test/files/form.escape.fdf output test/files/filledformescape.temp.pdf flatten',
'test/files/form.pdf generate_fdf output test/files/form.temp.fdf',
'test/files/form.pdf stamp test/files/logo.pdf output test/files/stamp.temp.pdf',
'test/files/form.pdf multistamp test/files/logo.pdf output test/files/multistamp.temp.pdf',
Expand Down
24 changes: 24 additions & 0 deletions test/files/form.escape.fdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%FDF-1.2
%����
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V ([email protected])
/T (email)
>>
<<
/V (John Doe\))
/T (name)
>>]
>>
>>
endobj
trailer

<<
/Root 1 0 R
>>
%%EOF
17 changes: 17 additions & 0 deletions test/fillForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ describe('fillForm', function () {
.catch(err => expect(err).to.be.null);
});

it('should escape single parenthesis', function () {

const testFile = fs.readFileSync(path.join(__dirname, './files/filledformescape.temp.pdf'));
const input = path.join(__dirname, './files/form.pdf');

return pdftk
.input(input)
.fillForm({
name: 'John Doe)',
email: '[email protected]',
})
.flatten()
.output()
.then(buffer => expect(buffer.equals(testFile)).to.be.true)
.catch(err => expect(err).to.be.null);
});

it('should catch an error if given a bad input path', function () {

const input = path.join(__dirname, './files/doesnotexist.pdf');
Expand Down

0 comments on commit 4123256

Please sign in to comment.