Skip to content

Commit 2c565a2

Browse files
committed
margin padding support for floated images
1 parent 8247a27 commit 2c565a2

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

jspdf.plugin.from_html.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,23 @@
370370

371371
var imagesCSS = GetCSS(cn);
372372
var imageX = renderer.x;
373+
var fontToUnitRatio = 12 / renderer.pdf.internal.scaleFactor;
374+
375+
//define additional paddings, margins which have to be taken into account for margin calculations
376+
var additionalSpaceLeft = (imagesCSS["margin-left"] + imagesCSS["padding-left"])*fontToUnitRatio;
377+
var additionalSpaceRight = (imagesCSS["margin-right"] + imagesCSS["padding-right"])*fontToUnitRatio;
378+
var additionalSpaceTop = (imagesCSS["margin-top"] + imagesCSS["padding-top"])*fontToUnitRatio;
379+
var additionalSpaceBottom = (imagesCSS["margin-bottom"] + imagesCSS["padding-bottom"])*fontToUnitRatio;
380+
373381
//if float is set to right, move the image to the right border
382+
//add space if margin is set
374383
if (imagesCSS['float'] !== undefined && imagesCSS['float'] === 'right') {
375-
imageX += renderer.settings.width-cn.width;
384+
imageX += renderer.settings.width - cn.width - additionalSpaceRight;
385+
} else {
386+
imageX += additionalSpaceLeft;
376387
}
377388

378-
renderer.pdf.addImage(images[cn.getAttribute("src")], imageX, renderer.y, cn.width, cn.height);
389+
renderer.pdf.addImage(images[cn.getAttribute("src")], imageX, renderer.y + additionalSpaceTop, cn.width, cn.height);
379390
//if the float prop is specified we have to float the text around the image
380391
if (imagesCSS['float'] !== undefined) {
381392
if (imagesCSS['float'] === 'right' || imagesCSS['float'] === 'left') {
@@ -395,7 +406,7 @@
395406
} else {
396407
return false;
397408
}
398-
}).bind(this, (imagesCSS['float'] === 'left') ? -cn.width : 0, renderer.y+cn.height, cn.width));
409+
}).bind(this, (imagesCSS['float'] === 'left') ? -cn.width-additionalSpaceLeft-additionalSpaceRight : 0, renderer.y+cn.height+additionalSpaceTop+additionalSpaceBottom, cn.width));
399410

400411
//reset floating by clear:both divs
401412
//just set cursorY after the floating element
@@ -413,15 +424,15 @@
413424
}).bind(this, renderer.y+cn.height, renderer.pdf.internal.getNumberOfPages()));
414425

415426
//if floating is set we decrease the available width by the image width
416-
renderer.settings.width -= cn.width;
427+
renderer.settings.width -= cn.width+additionalSpaceLeft+additionalSpaceRight;
417428
//if left just add the image width to the X coordinate
418429
if (imagesCSS['float'] === 'left') {
419-
renderer.x += cn.width;
430+
renderer.x += cn.width+additionalSpaceLeft+additionalSpaceRight;
420431
}
421432
}
422433
//if no floating is set, move the rendering cursor after the image height
423434
} else {
424-
renderer.y += cn.height;
435+
renderer.y += cn.height + additionalSpaceBottom;
425436
}
426437

427438
/*** TABLE RENDERING ***/

0 commit comments

Comments
 (0)