Skip to content

Instantly share code, notes, and snippets.

@inez
Created June 17, 2020 05:48
Show Gist options
  • Save inez/6a416d46ecb690a45212b6c805e82802 to your computer and use it in GitHub Desktop.
Save inez/6a416d46ecb690a45212b6c805e82802 to your computer and use it in GitHub Desktop.
(function(API){
API.myText = function(txt, options, x, y) {
options = options ||{};
/* Use the options align property to specify desired text alignment
* Param x will be ignored if desired text alignment is 'center'.
* Usage of options can easily extend the function to apply different text
* styles and sizes
*/
if( options.align == "center" ){
// Get current font size
var fontSize = this.internal.getFontSize();
// Get page width
var pageWidth = this.internal.pageSize.width;
// 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.
*/
txtWidth = this.getStringUnitWidth(txt)*fontSize/this.internal.scaleFactor;
// Calculate text's x coordinate
x = ( pageWidth - txtWidth ) / 2;
}
// Draw text at x,y
this.text(txt,x,y);
}
})(jsPDF.API);
var letters = [
{letter: 'B', inverted: false},
{letter: 'L', inverted: false},
{letter: 'A', inverted: false},
{letter: 'C', inverted: false},
{letter: 'K', inverted: false},
{letter: 'L', inverted: true},
{letter: 'I', inverted: true},
{letter: 'V', inverted: true},
{letter: 'E', inverted: true},
{letter: 'S', inverted: true},
{letter: 'M', inverted: false},
{letter: 'A', inverted: false},
{letter: 'T', inverted: false},
{letter: 'T', inverted: false},
{letter: 'E', inverted: false},
{letter: 'R', inverted: false}
];
var doc = new jsPDF('p', 'in', [11, 17]);
doc.setFontSize(1100)
doc.setFontType('bold')
for(var i = 0; i < letters.length; i++) {
var letter = letters[i];
if(letter.inverted) {
doc.rect(0, 0, 11, 17, 'F')
doc.setTextColor(255)
} else {
doc.setTextColor(0)
}
doc.myText(letter.letter,{align: "center"},0,13);
if(i != letters.length - 1) {
doc.addPage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment