Skip to content

Commit 8813035

Browse files
committed
prcoessing css style justify added
1 parent bd9fd98 commit 8813035

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

jspdf.plugin.from_html.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,22 +406,30 @@
406406
}
407407

408408
//if text alignment was set, set margin/indent of each line
409-
if (style['text-align'] !== undefined && (style['text-align'] === 'center' || style['text-align'] === 'right')) {
409+
if (style['text-align'] !== undefined && (style['text-align'] === 'center' || style['text-align'] === 'right' || style['text-align'] === 'justify')) {
410410
for(var i = 0; i < lines.length; ++i) {
411411
var length = this.pdf.getStringUnitWidth(lines[i][0][0], fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k;
412412
//if there is more than on line we have to clone the style object as all lines hold a reference on this object
413413
if (i > 0) {
414414
lines[i][0][1] = clone(lines[i][0][1]);
415415
}
416416
var space = (maxLineLength-length);
417-
console.log(lines[i][0][0]+" with space: "+space)
417+
418418
if (style['text-align'] === 'right') {
419419
lines[i][0][1]['margin-left'] = space;
420420
//if alignment is not right, it has to be center so split the space to the left and the right
421-
} else {
421+
} else if (style['text-align'] === 'center') {
422422
lines[i][0][1]['margin-left'] = space/2;
423+
//if justify was set, calculate the word spacing and define in by using the css property
424+
} else if (style['text-align'] === 'justify') {
425+
var countSpaces = lines[i][0][0].split(' ').length-1;
426+
lines[i][0][1]['word-spacing'] = space/countSpaces;
427+
//ignore the last line in justify mode
428+
if (i === (lines.length-1)) {
429+
lines[i][0][1]['word-spacing'] = 0;
430+
}
423431
}
424-
};
432+
}
425433
}
426434

427435
return lines;
@@ -436,7 +444,19 @@
436444
}
437445
defaultFontSize = 12;
438446
font = this.pdf.internal.getFont(style["font-family"], style["font-style"]);
439-
return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj");;
447+
448+
//set the word spacing for e.g. justify style
449+
if (style['word-spacing'] !== undefined && style['word-spacing'] > 0) {
450+
this.pdf.internal.write(style['word-spacing'].toFixed(2), "Tw");
451+
}
452+
453+
this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj");
454+
455+
//set the word spacing back to neutral => 0
456+
if (style['word-spacing'] !== undefined) {
457+
this.pdf.internal.write(0, "Tw");
458+
}
459+
440460
};
441461
Renderer.prototype.renderParagraph = function() {
442462
var blockstyle, defaultFontSize, fontToUnitRatio, fragments, i, l, line, lines, maxLineHeight, out, paragraphspacing_after, paragraphspacing_before, priorblockstype, styles;
@@ -487,7 +507,7 @@
487507
wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]);
488508
indentMove = wantedIndent-currentIndent;
489509
currentIndent = wantedIndent;
490-
}
510+
}
491511
//move the cursor
492512
out(indentMove, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td");
493513
i = 0;
@@ -545,7 +565,8 @@
545565
TextAlignMap = {
546566
left: "left",
547567
right: "right",
548-
center: "center"
568+
center: "center",
569+
justify: "justify"
549570
};
550571
UnitedNumberMap = {
551572
normal: 1

0 commit comments

Comments
 (0)