Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for PDFKit lists element types #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions lib/elements/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is a part of yipt
* Copyright (C) 2017 Webnium (https://webnium.co.jp/)
* License under MIT license https://opensource.org/licenses/MIT
*/

"use strict";
import {Offset, PDFDocument, TemplateElement} from "../index";
import Types, {bind} from "../evaluators";
import {isNumber} from "util";

interface ListElement extends TemplateElement {
type: "list";
items: any[];
content: string;
options?: any;
}

const OPTIONS = {
lineBreak: Types.boolean,
width: Types.number,
height: Types.number,
align: Types.enum(["justify", "left", "center", "right"]),
ellipsis: Types.string,
columns: Types.number,
columnGap: Types.number,
indent: Types.number,
paragraphGap: Types.number,
lineGap: Types.number,
wordSpacing: Types.number,
characterSpacing: Types.number,
fill: Types.boolean,
stroke: Types.boolean,
link: Types.string,
underline: Types.boolean,
strike: Types.boolean,
continued: Types.boolean,
features: Types.stringArray,
bulletRadius: Types.number,
textIndent: Types.number,
bulletIndent: Types.number,
listType: Types.string
};

export = async function (doc: PDFDocument, content: TemplateElement, vars: any, offset: Offset, _bindings: any) {
const element = content as ListElement;
const bindings = bind(doc, _bindings, offset);
const text = Types.array(element.content, vars);

const options = Types.object(OPTIONS)(element.options, vars, bindings);
const top = Types.number(element.top, vars, bindings);
const left = Types.number(element.left, vars, bindings);

if (isNumber(top) && isNumber(left)) {
doc.list(text, left + offset.left, top + offset.top, options);
} else if (isNumber(top)) {
doc.list(text, undefined, top + offset.top, options);
} else if (isNumber(left)) {
doc.list(text, left + offset.left, undefined, options);
} else if (options !== undefined) {
doc.list(text, options);
} else {
doc.list(text);
}

};
25 changes: 25 additions & 0 deletions tests/list-with-options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict"

import * as PDFKit from "pdfkit";
import * as sinon from "sinon";
import * as chai from "chai";
import * as sinonChai from "sinon-chai";
import yipt from "../../lib/index";

chai.should();
chai.use(sinonChai);

describe("list-with-options", () => {
it("should render pdf.", async () => {
const doc = new PDFKit({margin: 0});
const list = sinon.spy(doc, "list");
await yipt.render(doc, __dirname + "/template.yml");

list.should.have.been.calledWith(["item 1", "item 2", "item 3"], 20, 10, {
width: 200,
indent: 10,
bulletRadius: 2,
textIndent: 20
})
})
})
15 changes: 15 additions & 0 deletions tests/list-with-options/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
yipt:
version: "1.0"
content:
- type: list
top: 10
left: 20
content:
- "item 1"
- "item 2"
- "item 3"
options:
width: 200
indent: 10
bulletRadius: 2
textIndent: 20
54 changes: 54 additions & 0 deletions tests/list-with-variables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict";

import * as PDFKit from "pdfkit";
import * as sinon from "sinon";
import * as chai from "chai";
import * as sinonChai from "sinon-chai";
import yipt from "../../lib/index";

chai.should();
chai.use(sinonChai);

describe("list with variables", () => {
it("should render pdf.", async () => {
const doc = new PDFKit();
const list = sinon.spy(doc, "list");
const data = {
name: "data",
items: ["item 1", "item 2", "item 3"]
};

await yipt.render(doc, __dirname + "/template-simple.yml", data);

list.should.have.been.calledWith(data.items);
})
})

describe("List of objects", () => {
it("should render pdf.", async () => {
const doc = new PDFKit();
const list = sinon.spy(doc, "list");
const data = {
name: "data",
items: [{
name: "item 1",
item: {
name: "fizz"
}
}, {
name: "item 2",
item: {
name: "buzz"
}
}, {
name: "item 3",
item: {
name: "fizzbuzz"
}
}]
}

await yipt.render(doc, __dirname + "/template-complex.yml", data);
list.should.have.been.calledWith(["item 1 fizz", "item 2 buzz", "item 3 fizzbuzz"])
})
})
7 changes: 7 additions & 0 deletions tests/list-with-variables/template-complex.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
yipt:
version: "1.0"
content:
- type: text
content: "{name}"
- type: list
content: "[items].(name & ' ' & item.name)"
7 changes: 7 additions & 0 deletions tests/list-with-variables/template-simple.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
yipt:
version: "1.0"
content:
- type: text
content: "{name}"
- type: list
content: "{items}"
20 changes: 20 additions & 0 deletions tests/list/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

import * as PDFKit from "pdfkit";
import * as sinon from "sinon";
import * as chai from "chai";
import * as sinonChai from "sinon-chai";
import yipt from "../../lib/index";

chai.should();
chai.use(sinonChai);

describe("list", () => {
it("should render pdf.", async () => {
const doc = new PDFKit({margin:0});
const list = sinon.spy(doc, "list");
await yipt.render(doc, __dirname + "/template.yml");

list.getCall(0).should.have.been.calledWith(["Hello", "World", "!"])
})
})
8 changes: 8 additions & 0 deletions tests/list/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
yipt:
version: "1.0"
content:
- type: list
content:
- "Hello"
- "World"
- "!"