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'});
In the above example we have used the method fontSize()
. It sets the size of the font. It accepts unitless integer.
doc.fontSize(11);
This method is used to set the text color. It takes the color name.
doc.fillColor('green').text(lorem);
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.
liineBreak
: Default value is true
, which means the text will wrap if the text is too long to fit in a single line. You can set this option to false if you don't want your text to be wrapped in the new line.width
: The width that text should be wrapped to (by default, the page width minus the left and right margin).height
: The maximum height that text should be clipped to.ellipsis
: The character to display at the end of the text when it is too long. Set to true to use the default character.
colums
: The number of columns to flow the text into.columnGap
: The amount of space between each column (1/4 inch by default).indent
: The amount in PDF points (72 per inch) to indent each paragraph of text.paragraphGap
: The amount of space between each paragraph of text.lineGap
: The amount of space between each line of text.wordSpacing
: The amount of space between each word in the text.characterSpacing
: The amount of space between each character in the text.fill
: Whether to fill the text (true by default).stroke
: Whether to stroke the text.link
: A URL to link this text to (shortcut to create an annotation).underline
: Whether to underline the text.strike
: Whether to strike out the text.continued
: Whether the text segment will be followed immediately by another segment. Useful for changing styling in the middle of a paragraph.Here is an example combining some of the options above, wrapping a piece of text into three columns, in a specified width and height.
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' });
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));
doc.fillColor('green').text(lorem,{link : 'https://www.google.com'});