Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Text alignment for jsPDF 💥
(function (api, $) {
'use strict';
api.writeText = function (x, y, text, options) {
options = options || {};
var defaults = {
align: 'left',
width: this.internal.pageSize.width
}
var settings = $.extend({}, defaults, options);
// Get current font size
var fontSize = this.internal.getFontSize();
// Get the actual text's width
/* You multiply the unit width of your string by your font size and divide
* by the internal scale factor. The division is necessary
* for the case where you use units other than 'pt' in the constructor
* of jsPDF.
*/
var txtWidth = this.getStringUnitWidth(text) * fontSize / this.internal.scaleFactor;
if (settings.align === 'center')
x += (settings.width - txtWidth) / 2;
else if (settings.align === 'right')
x += (settings.width - txtWidth);
//default is 'left' alignment
this.text(text, x, y);
}
})(jsPDF.API, jQuery);
@Purush0th

This comment has been minimized.

Copy link
Owner Author

@Purush0th Purush0th commented Mar 7, 2016

Text alignment plugin - jsPDF

This small plugin will align the given text in jsPDF

Options:

{
width:100, // text container width (or) page width by default
align:'center' // right, center , left (default)
}

Usage:

var doc = new jsPDF('p', 'pt', 'a4');
//Alignment based on page width
doc.writeText(0, 40 ,'align - center ', { align: 'center' });
doc.writeText(0, 80 ,'align - right ', { align: 'right' });
doc.writeText(0, 120 ,'align - left '});

//Alignment based on text container width
doc.writeText(0, 120 ,'align - center : inside container',{align:'center',width:100});
@JPaulPunzalan

This comment has been minimized.

Copy link

@JPaulPunzalan JPaulPunzalan commented May 9, 2017

Is the justified alignment possible using code?

@ahugo91z

This comment has been minimized.

Copy link

@ahugo91z ahugo91z commented Jan 26, 2018

Great Job Man, thanks, work for me!! It's Awesome

@Rubench0

This comment has been minimized.

Copy link

@Rubench0 Rubench0 commented Mar 14, 2018

@JPaulPunzalan Did you manage to justify the text with this library?

@alireza0x0000001

This comment has been minimized.

Copy link

@alireza0x0000001 alireza0x0000001 commented Sep 21, 2018

Hi guys, I'm new in this field, how can I use this library within my code?
There are a lot of files, do I have to include them all?

@iamsajidjaved

This comment has been minimized.

Copy link

@iamsajidjaved iamsajidjaved commented Mar 6, 2019

kindly upload it to https://www.npmjs.com/ so that we can download it and use it in every technology with ease through npm.

@FernandoLuizNemeChibli

This comment has been minimized.

Copy link

@FernandoLuizNemeChibli FernandoLuizNemeChibli commented May 23, 2019

How do I include this in my React project?
I've tried to just copy and paste in the code, but got error 'jQuery' is not defined no-undef.

@gopimanikandan

This comment has been minimized.

Copy link

@gopimanikandan gopimanikandan commented Aug 9, 2019

Thanks for the gist Purushoth. Great job. Works perfectly.

@Josloader3

This comment has been minimized.

Copy link

@Josloader3 Josloader3 commented Aug 16, 2019

Thank you

@kedar9444

This comment has been minimized.

Copy link

@kedar9444 kedar9444 commented Dec 28, 2019

can i get typescript version of the same ?

@borisBelloc

This comment has been minimized.

Copy link

@borisBelloc borisBelloc commented Mar 23, 2020

any infos somewhere about how to use this pluggin with jspdf inside angular ?

@maggieeee87

This comment has been minimized.

Copy link

@maggieeee87 maggieeee87 commented Oct 16, 2020

This is AWESOME! Thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.