text

Adding text to a document is as simple as calling the text method.

doc.text(Hello world!');
doc.text('Hello Santanu!');

In the above example we have two text method. Each text method ends with a newline automatically and that's why the second text method prints the text in next line.

Internally, PDFKit keeps track of the current X and Y position of text as it is added to the document. This way, subsequent calls to the text method will automatically appear as new lines below the previous line. However, you can modify the position of text by passing X and Y coordinates to the text method after the text itself.

doc.text('Hello world!', 100, 200);

If you want to move down or up by lines, just call the moveDown or moveUp method with the number of lines you'd like to move (1 by default).

// The cursor position will move to two line below the current cursor position.
doc.moveDown(2);
// The cursor position will move up two line above the current cursor position.
doc.moveUp(2);

PDFKit includes support for line wrapping out of the box! If no options are given, text is automatically wrapped within the page margins and placed in the document flow below any previous text, or at the top of the page. PDFKit automatically inserts new pages as necessary so you don't have to worry about doing that for long pieces of text. PDFKit can also automatically wrap text into multiple columns.

If you pass a specific X and Y position for the text, it will not wrap unless you also pass the width option, setting the width the text should be wrapped to. If you set the height option, the text will be clipped to the number of lines that can fit in that height.

When line wrapping is enabled, you can choose a text justification. There are four options: left (the default), center, right, and justify.

var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus. 
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec 
hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit. Ut 
nec accumsan nisl.';

doc.fontSize(11);
doc.text('This text is left aligned. ' + lorem, {width: 410, align: 'left'});

doc.moveDown();
doc.text('This text is Center aligned. ' + lorem, {width: 410, align: 'center'});

doc.moveDown();
doc.text('This text is right aligned. ' + lorem, {width: 410, align: 'right'});

doc.moveDown();
doc.text('This text is left aligned. ' + lorem, {width: 410, align: 'justify'});

fontSize()

In the above example we have used the method fontSize(). It sets the size of the font. It accepts unitless integer.

doc.fontSize(11);

fillColor()

This method is used to set the text color. It takes the color name.

doc.fillColor('green').text(lorem);

Text Styling

PDFKit has many options for controlling the look of text added to PDF documents, which can be passed to the text method. They are enumerated below.

Here is an example combining some of the options above, wrapping a piece of text into three columns, in a specified width and height.

Multiple Colums

	var lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit purus.
		Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vivamus nec 
		hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis fringilla hendrerit.
		Ut nec accumsan nisl. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in suscipit
		purus.  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
		Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in turpis 
		fringilla hendrerit. Ut nec accumsan nisl. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
		Etiam in suscipit purus.  Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere
		cubilia Curae; Vivamus nec hendrerit felis. Morbi aliquam facilisis risus eu lacinia. Sed eu leo in 
		turpis fringilla hendrerit. Ut nec accumsan nisl';

	doc.text(lorem, {
			columns: 3,
			columnGap: 15,
			height: 100,
			align: 'justify'
	});

Continue

As mentioned above, PDFKit supports a simple form of rich text via the continued option. When set to true, PDFKit will retain the text wrapping state between text calls. This way, when you call text again after changing the text styles, the wrapping will continue right where it left off. The options given to the first text call are also retained for subsequent calls after a continued one, but of course you can override them. In the following example, the width option from the first text call is retained by the second call.

	doc.fillColor('green')
			.text(lorem.slice(0, 500),{continued : true, width : 150})
			.fillColor('blue')
			.text(lorem.slice(500));

Link

doc.fillColor('green').text(lorem,{link : 'https://www.google.com'});