Skip to content
the poor coder

jspdf text position

Poor Man
Published
Reading time
1 min read
On this page

JSPDF Text Position#

When creating a PDF document using JSPDF, you may want to position the text at a specific location on the page. To do this, you can use the text() method of the JSPDF object.

Using Absolute Positioning#

To position the text at an absolute position on the page, you can use the text() method with the x and y coordinates as arguments. For example:

Text
var doc = new jsPDF();

doc.text(20, 20, 'Hello World!');

This will position the text "Hello World!" at coordinates (20, 20) on the page.

Using Relative Positioning#

You can also position the text relative to its current position using the text() method with the options object as an argument. The options object can contain properties such as align, baseline, and angle. For example:

Text
var doc = new jsPDF();

doc.text('Hello World!', 50, 50, {
  align: 'center',
  baseline: 'middle',
  angle: 45
});

This will position the text "Hello World!" at coordinates (50, 50), with the text centered horizontally and vertically, and rotated 45 degrees.

Using a Rectangular Area#

You can also position the text within a rectangular area using the text() method with the options object as an argument. The options object can contain properties such as maxWidth and maxLines. For example:

Text
var doc = new jsPDF();

doc.text('Hello World!', 20, 20, {
  maxWidth: 50,
  maxLines: 2
});

This will position the text "Hello World!" within a rectangular area starting at coordinates (20, 20) with a maximum width of 50 and a maximum of 2 lines.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC