Skip to content

Commit 10a715b

Browse files
committed
chore: Fixed build
1 parent 6853a6e commit 10a715b

File tree

10 files changed

+319
-197
lines changed

10 files changed

+319
-197
lines changed

.vscode/launch.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@
2626
"args": [
2727
],
2828
"program": "${file}"
29-
}
29+
},
30+
{
31+
"type": "node",
32+
"request": "launch",
33+
"name": "Mocha Tests",
34+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
35+
"env": {
36+
"TZ": "UTC",
37+
"LOG_LEVEL": "fatal"
38+
},
39+
"args": [
40+
"--no-timeouts",
41+
"--colors",
42+
],
43+
"cwd": "${fileDirname}/..",
44+
"internalConsoleOptions": "openOnSessionStart"
45+
},
3046
]
3147
}

docs/API.md

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@
1212
<dt><a href="#map">map(pdfFile, [options])</a> ⇒ <code>object</code></dt>
1313
<dd><p>Generates a map from a PDF file and saves as YAML.</p>
1414
</dd>
15+
<dt><a href="#open">open(fileName)</a> ⇒ <code>PDF</code></dt>
16+
<dd><p>Opens a PDF form.</p>
17+
</dd>
18+
<dt><a href="#getTemplate">getTemplate()</a> ⇒ <code>object</code></dt>
19+
<dd><p>Gets the form template.</p>
20+
</dd>
21+
<dt><a href="#fillForm">fillForm(filledTemplate)</a></dt>
22+
<dd><p>Fills out a form using <code>filledTemplate</code>.</p>
23+
</dd>
24+
<dt><a href="#slice">slice(begin, end)</a> ⇒ <code>PDF</code></dt>
25+
<dd><p>Slice pages from <code>begin</code> (0-based, inclusive) to <code>end</code> (exclusive).</p>
26+
</dd>
27+
<dt><a href="#append">append(pdf)</a></dt>
28+
<dd><p>Appends PDF to the current PDF.</p>
29+
</dd>
30+
<dt><a href="#save">save(dest, [flatten])</a> ⇒ <code>Promise</code></dt>
31+
<dd><p>Saves the PDF to file.</p>
32+
</dd>
1533
</dl>
1634

1735
## Typedefs
@@ -38,7 +56,7 @@ Creates a fillable form from a PDF.
3856
* [.fill(filler, [options])](#Form+fill)
3957
* [.save(source, dest)](#Form+save)
4058
* [.slice(begin, end, dest)](#Form+slice)
41-
* [.join(parts, dest)](#Form+join)
59+
* [.join(dest, parts)](#Form+join)
4260

4361
<a name="Form+init"></a>
4462

@@ -94,7 +112,7 @@ form.setFormName('banana');
94112
### form.setSourcePDF(source)
95113
Change the source PDF name. By default, this is the the `source` PDF
96114
path provided in [load](load) function. It allows the source PDF to be
97-
changed prior to calling [slice](slice) or [save](save).
115+
changed prior to calling [slice](#slice) or [save](#save).
98116

99117
**Kind**: instance method of [<code>Form</code>](#Form)
100118
**Access**: public
@@ -186,16 +204,16 @@ for slicing pages.
186204
187205
<a name="Form+join"></a>
188206
189-
### form.join(parts, dest)
207+
### form.join(dest, parts)
190208
Join multiple PDF `parts` into a new PDF `dest`.
191209
192210
**Kind**: instance method of [<code>Form</code>](#Form)
193211
**Access**: public
194212
195213
| Param | Type | Description |
196214
| --- | --- | --- |
197-
| parts | <code>Array.&lt;string&gt;</code> | PDF file name parts to join. |
198-
| dest | <code>string</code> | The destination PDF file to write. |
215+
| dest | <code>PDF</code> | - |
216+
| parts | <code>Array.&lt;string&gt;</code> | - |
199217
200218
<a name="map"></a>
201219
@@ -211,6 +229,72 @@ Generates a map from a PDF file and saves as YAML.
211229
| pdfFile | <code>string</code> | | The PDF file containing a form. |
212230
| [options] | <code>object</code> | <code>{}</code> | Options. |
213231
232+
<a name="open"></a>
233+
234+
## open(fileName) ⇒ <code>PDF</code>
235+
Opens a PDF form.
236+
237+
**Kind**: global function
238+
**Returns**: <code>PDF</code> - A PDF for filling.
239+
240+
| Param | Type | Description |
241+
| --- | --- | --- |
242+
| fileName | <code>string</code> | The path to the PDF file to open. |
243+
244+
<a name="getTemplate"></a>
245+
246+
## getTemplate() ⇒ <code>object</code>
247+
Gets the form template.
248+
249+
**Kind**: global function
250+
**Returns**: <code>object</code> - A PDF form template.
251+
<a name="fillForm"></a>
252+
253+
## fillForm(filledTemplate)
254+
Fills out a form using `filledTemplate`.
255+
256+
**Kind**: global function
257+
258+
| Param | Type |
259+
| --- | --- |
260+
| filledTemplate | <code>object</code> |
261+
262+
<a name="slice"></a>
263+
264+
## slice(begin, end) ⇒ <code>PDF</code>
265+
Slice pages from `begin` (0-based, inclusive) to `end` (exclusive).
266+
267+
**Kind**: global function
268+
**Returns**: <code>PDF</code> - The new PDF.
269+
270+
| Param | Type | Description |
271+
| --- | --- | --- |
272+
| begin | <code>int</code> | The index of the page to slice from. |
273+
| end | <code>int</code> | The index of the page to slice to. |
274+
275+
<a name="append"></a>
276+
277+
## append(pdf)
278+
Appends PDF to the current PDF.
279+
280+
**Kind**: global function
281+
282+
| Param | Type | Description |
283+
| --- | --- | --- |
284+
| pdf | <code>PDF</code> | The PDF to append. |
285+
286+
<a name="save"></a>
287+
288+
## save(dest, [flatten]) ⇒ <code>Promise</code>
289+
Saves the PDF to file.
290+
291+
**Kind**: global function
292+
293+
| Param | Type | Default | Description |
294+
| --- | --- | --- | --- |
295+
| dest | <code>str</code> | | The destination file name. |
296+
| [flatten] | <code>bool</code> | <code>true</code> | Flattens the PDF form before saving. |
297+
214298
<a name="HelperFunction"></a>
215299
216300
## HelperFunction ⇒ <code>number</code> \| <code>string</code>

package-lock.json

Lines changed: 24 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@semantic-release/git": "^10.0.1",
4444
"c8": "^7.13.0",
4545
"chai": "^4.3.7",
46-
"command-exists": "^1.2.9",
46+
"chai-as-promised": "^8.0.1",
4747
"conventional-changelog-conventionalcommits": "^5.0.0",
4848
"eslint": "^8.40.0",
4949
"husky": "^8.0.3",

src/map.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async function map(pdfFile, options = {}) {
4242
// extract the template from PDF
4343
log('opening PDF file:', pdfFile);
4444

45-
const pdfDoc = await PDF.open(pdfFile)
45+
const pdfDoc = await PDF.open(pdfFile);
4646
const template = pdfDoc.getTemplate();
4747

4848
// fill a form, mapping each input ID `key` to a unique index `i`
@@ -73,8 +73,6 @@ async function map(pdfFile, options = {}) {
7373

7474
// update the form with each input ID `key` filled as unique index `i`
7575
log('calling fillFormWithFlattenAsync', pdfFile);
76-
// await pdfFiller.fillFormWithFlattenAsync(
77-
// pdfFile, filledFile, template, false);
7876
await pdfDoc.fillForm(template, filledFile);
7977

8078
return {
@@ -94,7 +92,7 @@ function ensureDirectory(dir) {
9492
return fs.access(dir, fs.constants.R_OK | fs.constants.W_OK)
9593
.catch(() => {
9694
log('mkdir:', dir);
97-
fs.mkdir(dir);
95+
return fs.mkdir(dir);
9896
});
9997
}
10098

src/pdf.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class PDF {
2222
* Opens a PDF form.
2323
*
2424
* @param {string} fileName - The path to the PDF file to open.
25-
* @returns
25+
* @returns {PDF} A PDF for filling.
2626
*/
2727
static async open(fileName) {
2828
log('open', fileName);
@@ -63,7 +63,7 @@ export default class PDF {
6363
try {
6464
field.setText(filledTemplate[key]);
6565
} catch (ex) {
66-
if (`${ex}`.indexOf(" must be of type `string`") >= 0) {
66+
if (`${ex}`.indexOf(' must be of type `string`') >= 0) {
6767
// automatically convert numbers to strings
6868
field.setText(`${filledTemplate[key]}`);
6969
} else {
@@ -142,8 +142,8 @@ export default class PDF {
142142
* Saves the PDF to file.
143143
*
144144
* @param {str} dest - The destination file name.
145-
* @param {bool=true} flatten - Flattens the PDF form before saving.
146-
* @returns
145+
* @param {bool} [flatten=true] - Flattens the PDF form before saving.
146+
* @returns {Promise}
147147
*/
148148
async save(dest, flatten = true) {
149149
if (flatten) {

test/example.test.js

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
// const fs = require('fs');
2-
import fs from 'fs';
1+
import fs from 'fs/promises';
32
import path from 'path';
43
import { expect } from 'chai';
54
import simple from 'simple-mock';
6-
import pdfFiller from 'pdffiller';
5+
import { PDFDocument } from '@cantoo/pdf-lib';
76
import { map, Form } from '../src/index.js';
87

98
const examplePdf = path.join('example', 'f1040.pdf');
@@ -12,40 +11,50 @@ const fillerFile = path.join('example', 'f1040-script.yaml');
1211
const configFile = path.join('example', 'config.yaml');
1312
const outputPdf = path.join('example', 'example.pdf');
1413

14+
async function mockForm(fieldName) {
15+
const doc = await PDFDocument.create();
16+
doc.addPage([550, 750]);
17+
const form = doc.getForm();
18+
const textField = form.createTextField(fieldName);
19+
simple.mock(PDFDocument, 'load').resolveWith(doc);
20+
simple.mock(fs, 'readFile').resolveWith('bytes');
21+
simple.mock(fs, 'writeFile').resolveWith();
22+
return textField;
23+
}
24+
1525
describe('example', () => {
1626
beforeEach(() => {
17-
simple.mock(pdfFiller, 'fillFormWithFlattenAsync')
18-
.resolveWith();
27+
simple.mock(fs, 'writeFile').resolveWith();
1928
});
2029

2130
afterEach(() => {
2231
simple.restore();
2332
});
2433

2534
it('should generate a map YAML file for the example PDF file', async () => {
26-
// simple.mock(child_process, 'execFile').throwWith(new Error('omg'));
27-
simple.mock(pdfFiller, 'generatePDFTemplateAsync').resolveWith({
28-
key1: 'banana'
29-
});
30-
simple.mock(fs.promises, 'writeFile').resolveWith();
35+
await mockForm('key1');
36+
simple.mock(fs, 'writeFile').resolveWith();
3137

3238
await map(examplePdf, { dir: 'example' });
33-
expect(fs.existsSync(mapFile)).to.be.true;
3439

35-
expect(fs.promises.writeFile.calls).to.have.length(1);
36-
expect(fs.promises.writeFile.calls[0].args[0])
40+
// expect(fs.existsSync(mapFile)).to.be.true;
41+
expect(fs.writeFile.calls).to.have.length(1);
42+
expect(fs.writeFile.calls[0].args[0])
3743
.to.equal('example/f1040-map.yaml');
38-
expect(fs.promises.writeFile.calls[0].args[1])
44+
expect(fs.writeFile.calls[0].args[1])
3945
.to.equal('key1: \'0\'\n');
40-
expect(pdfFiller.generatePDFTemplateAsync.calls).to.have.length(1);
41-
expect(pdfFiller.generatePDFTemplateAsync.calls[0].args[0])
42-
.to.equal('example/f1040.pdf');
4346
});
4447

4548
it('should fill the example PDF form', async () => {
46-
simple.mock(pdfFiller, 'fillFormWithFlattenAsync').resolveWith();
49+
const form = new Form();
50+
await form.init(configFile);
51+
await form.load(examplePdf, mapFile);
52+
await form.fill(fillerFile);
53+
await form.save(outputPdf);
4754

48-
const expected = {
55+
expect(fs.writeFile.calls).to.have.length(1);
56+
expect(fs.writeFile.calls[0].arg).to.equal('example/example.pdf');
57+
expect(form.ctx.forms.f1040).to.deep.equal({
4958
'filing.status': '1',
5059
'your.first.name.and.middle.initial': 'Joe A',
5160
'topmostSubform[0].Page1[0].f1_02[0]': 'Joe A',
@@ -54,17 +63,6 @@ describe('example', () => {
5463
'topmostSubform[0].Page1[0].f1_03[0]': 'Bloggs',
5564
'social.security.number': '012345678',
5665
'topmostSubform[0].Page1[0].YourSocial_ReadOrderControl[0].f1_04[0]': '012345678'
57-
};
58-
59-
const form = new Form();
60-
await form.init(configFile);
61-
await form.load(examplePdf, mapFile);
62-
await form.fill(fillerFile);
63-
await form.save(outputPdf);
64-
65-
expect(pdfFiller.fillFormWithFlattenAsync.calls)
66-
.to.have.length(1);
67-
expect(pdfFiller.fillFormWithFlattenAsync.lastCall.args)
68-
.to.deep.equal([ examplePdf, outputPdf, expected, false ]);
66+
});
6967
});
7068
});

0 commit comments

Comments
 (0)