Skip to content
Merged
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
7 changes: 7 additions & 0 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ numbers to a document.
// doc.end() will call it for you automatically.
doc.end();

## Setting default font

The default font is 'Helvetica'. It can be configured by passing `font` option

// use Courier font by default
const doc = new PDFDocument({font: 'Courier'});

## Setting document metadata

PDF documents can have various metadata associated with them, such as the
Expand Down
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class PDFDocument extends stream.Readable {
// Initialize mixins
this.initColor();
this.initVector();
this.initFonts();
this.initFonts(options.font);
this.initText();
this.initImages();
this.initOutline();
Expand Down
6 changes: 4 additions & 2 deletions lib/mixins/fonts.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PDFFontFactory from '../font_factory';

export default {
initFonts() {
initFonts(defaultFont = 'Helvetica') {
// Lookup table for embedded fonts
this._fontFamilies = {};
this._fontCount = 0;
Expand All @@ -13,7 +13,9 @@ export default {
this._registeredFonts = {};

// Set the default font
return this.font('Helvetica');
if (defaultFont) {
this.font(defaultFont);
}
},

font(src, family, size) {
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/document.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const PDFDocument = require('../../lib/document').default;

describe('PDFDocument', () => {
describe('font option', () => {
let fontSpy;

beforeEach(() => {
fontSpy = jest.spyOn(PDFDocument.prototype, 'font').mockReturnThis();
});

afterEach(() => {
fontSpy.mockRestore();
});

test('not defined', () => {
new PDFDocument();

expect(fontSpy).toBeCalledWith('Helvetica');
});

test('a string value', () => {
new PDFDocument({ font: 'Roboto' });

expect(fontSpy).toBeCalledWith('Roboto');
});

test('a falsy value', () => {
new PDFDocument({ font: null });
new PDFDocument({ font: false });
new PDFDocument({ font: '' });

expect(fontSpy).not.toBeCalled();
});
});
});