-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
148 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
var PDFDocument = require('pdfkit'); | ||
|
||
/** | ||
* @typedef {object} ReturnType | ||
* @prop {Promise<Buffer>} promise | ||
* @prop {PDFDocument} pdf | ||
*/ | ||
|
||
/** | ||
* Creates a Buffer containing a PDF. | ||
* Returns a Promise that is resolved with the resulting Buffer of the PDFDocument. | ||
* @returns {ReturnType} | ||
*/ | ||
module.exports = function (params) { | ||
var requestParams = { | ||
placeholder: { | ||
contactInfo: '[email protected]', | ||
name: 'test name', | ||
location: 'test Location', | ||
}, | ||
text: 'node-signpdf', | ||
pages: 1, | ||
layout: 'portrait', | ||
end: true, | ||
}; | ||
Object.assign(requestParams, params); | ||
if (requestParams.pages < 1) { | ||
requestParams.pages = 1; | ||
} | ||
|
||
var pdf = new PDFDocument({ | ||
autoFirstPage: false, | ||
size: 'A4', | ||
layout: requestParams.layout, | ||
bufferPages: true, | ||
});; | ||
pdf.info.CreationDate = ''; | ||
|
||
// Add some content to the page(s) | ||
for (var i = 0; i < requestParams.pages; i += 1) { | ||
.addPage() | ||
.fillColor('#333') | ||
.fontSize(25) | ||
.moveDown() | ||
.text(requestParams.text) | ||
.save(); | ||
} | ||
|
||
var ended = new Promise(function (resolve) { | ||
// Collect the ouput PDF | ||
// and, when done, resolve with it stored in a Buffer | ||
var pdfChunks = []; | ||
pdf.on('data', function (data) { | ||
pdfChunks.push(data); | ||
}); | ||
pdf.on('end', function () { | ||
resolve(Buffer.concat(pdfChunks)); | ||
}); | ||
}) | ||
|
||
return { | ||
ended: ended, | ||
pdf: pdf, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
var fs = require('fs'); | ||
var readTestResource = require('./readTestResource'); | ||
var createPdfkitDocument = require('./createPdfkitDocument'); | ||
|
||
module.exports = { | ||
readTestResource: function (filename) { | ||
if (filename === '.' || (/(\/|\\|\.\.)/).test(filename)) { | ||
throw new Error(`"${filename}" is invalid filename.`); | ||
} | ||
return fs.readFileSync(`${__dirname}/../../resources/${filename}`); | ||
} | ||
readTestResource: readTestResource, | ||
createPdfkitDocument: createPdfkitDocument, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var fs = require('fs'); | ||
|
||
module.exports = function (filename) { | ||
if (filename === '.' || (/(\/|\\|\.\.)/).test(filename)) { | ||
throw new Error(`"${filename}" is invalid filename.`); | ||
} | ||
return fs.readFileSync(`${__dirname}/../../resources/${filename}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
const sharedConfig = require('../../jest.config.base'); | ||
module.exports = { | ||
...sharedConfig, | ||
coveragePathIgnorePatterns: [ | ||
...sharedConfig.coveragePathIgnorePatterns, | ||
'/src/pdfkit/', | ||
], | ||
coverageThreshold: { | ||
...sharedConfig.coverageThreshold, | ||
'**/pdfkitAddPlaceholder.js': { | ||
functions: 100, | ||
lines: 96.77, | ||
statements: 96.77, | ||
branches: 88.88, | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import PDFDocument from 'pdfkit'; | ||
import forge from 'node-forge'; | ||
import {readTestResource} from '@signpdf/internal-utils'; | ||
import {readTestResource, createPdfkitDocument} from '@signpdf/internal-utils'; | ||
import {pdfkitAddPlaceholder} from '@signpdf/placeholder-pdfkit010'; | ||
import {plainAddPlaceholder} from '@signpdf/placeholder-plain'; | ||
import { | ||
|
@@ -14,52 +13,13 @@ import signer from './signpdf'; | |
* Returns a Promise that is resolved with the resulting Buffer of the PDFDocument. | ||
* @returns {Promise<Buffer>} | ||
*/ | ||
const createPdf = (params) => new Promise((resolve) => { | ||
const createPdf = (params) => { | ||
const requestParams = { | ||
placeholder: { | ||
contactInfo: '[email protected]', | ||
name: 'test name', | ||
location: 'test Location', | ||
}, | ||
text: 'node-signpdf', | ||
addSignaturePlaceholder: true, | ||
pages: 1, | ||
layout: 'portrait', | ||
...params, | ||
}; | ||
|
||
const pdf = new PDFDocument({ | ||
autoFirstPage: false, | ||
size: 'A4', | ||
layout: requestParams.layout, | ||
bufferPages: true, | ||
}); | ||
pdf.info.CreationDate = ''; | ||
|
||
if (requestParams.pages < 1) { | ||
requestParams.pages = 1; | ||
} | ||
|
||
// Add some content to the page(s) | ||
for (let i = 0; i < requestParams.pages; i += 1) { | ||
.addPage() | ||
.fillColor('#333') | ||
.fontSize(25) | ||
.moveDown() | ||
.text(requestParams.text) | ||
.save(); | ||
} | ||
|
||
// Collect the ouput PDF | ||
// and, when done, resolve with it stored in a Buffer | ||
const pdfChunks = []; | ||
pdf.on('data', (data) => { | ||
pdfChunks.push(data); | ||
}); | ||
pdf.on('end', () => { | ||
resolve(Buffer.concat(pdfChunks)); | ||
}); | ||
const {pdf, ended} = createPdfkitDocument(params); | ||
|
||
if (requestParams.addSignaturePlaceholder) { | ||
// Externally (to PDFKit) add the signature placeholder. | ||
|
@@ -77,7 +37,9 @@ const createPdf = (params) => new Promise((resolve) => { | |
// Also end the PDFDocument stream. | ||
// See pdf.on('end'... on how it is then converted to Buffer. | ||
pdf.end(); | ||
}); | ||
|
||
return ended; | ||
}; | ||
|
||
describe('Test signing', () => { | ||
it('expects PDF to be Buffer', () => { | ||
|