Skip to content

Commit bd9fd98

Browse files
committed
processing of css style text-align center and right added
1 parent ddbfc0f commit bd9fd98

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

jspdf.plugin.from_html.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
*/
2727

2828
(function(jsPDFAPI) {
29-
var DrillForContent, FontNameDB, FontStyleMap, FontWeightMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, process, tableToJson;
29+
var clone,DrillForContent, FontNameDB, FontStyleMap, FontWeightMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, process, tableToJson;
30+
clone = (function(){
31+
return function (obj) { Clone.prototype=obj; return new Clone() };
32+
function Clone(){}
33+
}());
3034
PurgeWhiteSpace = function(array) {
3135
var fragment, i, l, lTrimmed, r, rTrimmed, trailingSpace;
3236
i = 0;
@@ -140,6 +144,7 @@
140144
tmp = void 0;
141145
css["font-family"] = ResolveFont(computedCSSElement("font-family")) || "times";
142146
css["font-style"] = FontStyleMap[computedCSSElement("font-style")] || "normal";
147+
css["text-align"] = TextAlignMap[computedCSSElement("text-align")] || "left";
143148
tmp = FontWeightMap[computedCSSElement("font-weight")] || "normal";
144149
if (tmp === "bold") {
145150
if (css["font-style"] === "normal") {
@@ -399,8 +404,28 @@
399404
}
400405
}
401406
}
407+
408+
//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')) {
410+
for(var i = 0; i < lines.length; ++i) {
411+
var length = this.pdf.getStringUnitWidth(lines[i][0][0], fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k;
412+
//if there is more than on line we have to clone the style object as all lines hold a reference on this object
413+
if (i > 0) {
414+
lines[i][0][1] = clone(lines[i][0][1]);
415+
}
416+
var space = (maxLineLength-length);
417+
console.log(lines[i][0][0]+" with space: "+space)
418+
if (style['text-align'] === 'right') {
419+
lines[i][0][1]['margin-left'] = space;
420+
//if alignment is not right, it has to be center so split the space to the left and the right
421+
} else {
422+
lines[i][0][1]['margin-left'] = space/2;
423+
}
424+
};
425+
}
426+
402427
return lines;
403-
};
428+
};
404429
Renderer.prototype.RenderTextFragment = function(text, style) {
405430
var defaultFontSize, font;
406431
if (this.pdf.internal.pageSize.height - this.pdf.margins_doc.bottom < this.y + this.pdf.internal.getFontSize()) {
@@ -411,7 +436,7 @@
411436
}
412437
defaultFontSize = 12;
413438
font = this.pdf.internal.getFont(style["font-family"], style["font-style"]);
414-
return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj");
439+
return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj");;
415440
};
416441
Renderer.prototype.renderParagraph = function() {
417442
var blockstyle, defaultFontSize, fontToUnitRatio, fragments, i, l, line, lines, maxLineHeight, out, paragraphspacing_after, paragraphspacing_before, priorblockstype, styles;
@@ -440,7 +465,11 @@
440465
l = void 0;
441466
this.y += paragraphspacing_before;
442467
out("q", "BT", this.pdf.internal.getCoordinateString(this.x), this.pdf.internal.getVerticalCoordinateString(this.y), "Td");
443-
while (lines.length) {
468+
469+
//stores the current indent of cursor position
470+
var currentIndent = 0;
471+
472+
while (lines.length) {
444473
line = lines.shift();
445474
maxLineHeight = 0;
446475
i = 0;
@@ -451,7 +480,16 @@
451480
}
452481
i++;
453482
}
454-
out(0, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td");
483+
//if we have to move the cursor to adapt the indent
484+
var indentMove = 0;
485+
//if a margin was added (by e.g. a text-alignment), move the cursor
486+
if (line[0][1]["margin-left"] !== undefined && line[0][1]["margin-left"] > 0) {
487+
wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]);
488+
indentMove = wantedIndent-currentIndent;
489+
currentIndent = wantedIndent;
490+
}
491+
//move the cursor
492+
out(indentMove, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td");
455493
i = 0;
456494
l = line.length;
457495
while (i !== l) {
@@ -504,6 +542,11 @@
504542
italic: "italic",
505543
oblique: "italic"
506544
};
545+
TextAlignMap = {
546+
left: "left",
547+
right: "right",
548+
center: "center"
549+
};
507550
UnitedNumberMap = {
508551
normal: 1
509552
/*

0 commit comments

Comments
 (0)