Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix margin values not working for html #2977

Merged
merged 23 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3327,7 +3327,7 @@ function jsPDF(options) {
*/
options = options || {};
var scope = options.scope || this;
var payload, da, angle, align, charSpace, maxWidth, flags;
var payload, da, angle, align, charSpace, maxWidth, renderMaxWidthOverflow, flags;

// Pre-August-2012 the order of arguments was function(x, y, text, flags)
// in effort to make all calls have similar signature like
Expand Down Expand Up @@ -3526,6 +3526,7 @@ function jsPDF(options) {

//multiline
maxWidth = options.maxWidth || 0;
renderMaxWidthOverflow = options.renderMaxWidthOverflow !== undefined ? options.renderMaxWidthOverflow : true;

if (maxWidth > 0) {
if (typeof text === "string") {
Expand All @@ -3537,6 +3538,10 @@ function jsPDF(options) {
}
}

if (!renderMaxWidthOverflow) {
text = [text[0]];
}
jeffsieu marked this conversation as resolved.
Show resolved Hide resolved

//creating Payload-Object to make text byRef
payload = {
text: text,
Expand Down
89 changes: 67 additions & 22 deletions src/modules/context2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import { console } from "../libs/console.js";
this.currentPoint = ctx.currentPoint || new Point();
this.miterLimit = ctx.miterLimit || 10.0;
this.lastPoint = ctx.lastPoint || new Point();
this.margin = ctx.margin || [0, 0, 0, 0];
this.prevPageLastElemOffset = ctx.prevPageLastElemOffset || 0;

this.ignoreClearRect =
typeof ctx.ignoreClearRect === "boolean" ? ctx.ignoreClearRect : true;
Expand Down Expand Up @@ -157,6 +159,20 @@ import { console } from "../libs/console.js";
}
});

/**
* @name margin
* @type {array}
* @default [0, 0, 0, 0]
*/
Object.defineProperty(this, "margin", {
get: function() {
return _ctx.margin;
},
set: function(value) {
_ctx.margin = value;
}
});

var _autoPaging = false;
/**
* @name autoPaging
Expand Down Expand Up @@ -1457,29 +1473,35 @@ import { console } from "../libs/console.js";
for (var i = min; i < max + 1; i++) {
this.pdf.setPage(i);

var topMargin = (i === min ? Math.max(this.posY, this.margin[0]) : this.margin[0]);
var firstPageHeight = this.pdf.internal.pageSize.height - Math.max(this.posY, this.margin[0]) - this.margin[2];
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[2];
var pageWidthMinusMargin = this.pdf.internal.pageSize.width - this.margin[1];
var previousPageHeightSum = i === 1 ? 0 : firstPageHeight + (i - 2) * pageHeightMinusMargin;

if (this.ctx.clip_path.length !== 0) {
var tmpPaths = this.path;
clipPath = JSON.parse(JSON.stringify(this.ctx.clip_path));
this.path = pathPositionRedo(
clipPath,
this.posX,
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY
Math.max(this.posX, this.margin[3]),
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
);
drawPaths.call(this, "fill", true);
this.path = tmpPaths;
}
var tmpRect = JSON.parse(JSON.stringify(xRect));
tmpRect = pathPositionRedo(
[tmpRect],
this.posX,
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY
Math.max(this.posX, this.margin[3]),
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
)[0];
this.pdf.addImage(
img,
"JPEG",
tmpRect.x,
tmpRect.y,
tmpRect.w,
Math.min(tmpRect.w, this.pdf.internal.pageSize.width - this.margin[1] - tmpRect.x),
tmpRect.h,
null,
null,
Expand All @@ -1504,7 +1526,7 @@ import { console } from "../libs/console.js";
var getPagesByPath = function(path, pageWrapX, pageWrapY) {
var result = [];
pageWrapX = pageWrapX || this.pdf.internal.pageSize.width;
pageWrapY = pageWrapY || this.pdf.internal.pageSize.height;
pageWrapY = pageWrapY || this.pdf.internal.pageSize.height - this.margin[0] - this.margin[2];

switch (path.type) {
default:
Expand Down Expand Up @@ -1653,22 +1675,27 @@ import { console } from "../libs/console.js";
this.lineWidth = lineWidth;
this.lineJoin = lineJoin;

var topMargin = (k === min ? Math.max(this.posY, this.margin[0]) : this.margin[0]);
var firstPageHeight = this.pdf.internal.pageSize.height - Math.max(this.posY, this.margin[0]) - this.margin[2];
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[2];
var previousPageHeightSum = k === 1 ? 0 : firstPageHeight + (k - 2) * pageHeightMinusMargin;

if (this.ctx.clip_path.length !== 0) {
var tmpPaths = this.path;
clipPath = JSON.parse(JSON.stringify(this.ctx.clip_path));
this.path = pathPositionRedo(
clipPath,
this.posX,
-1 * this.pdf.internal.pageSize.height * (k - 1) + this.posY
Math.max(this.posX, this.margin[3]),
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
);
drawPaths.call(this, rule, true);
this.path = tmpPaths;
}
tmpPath = JSON.parse(JSON.stringify(origPath));
this.path = pathPositionRedo(
tmpPath,
this.posX,
-1 * this.pdf.internal.pageSize.height * (k - 1) + this.posY
Math.max(this.posX, this.margin[3]),
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
);
if (isClip === false || k === 0) {
drawPaths.call(this, rule, isClip);
Expand Down Expand Up @@ -2007,28 +2034,34 @@ import { console } from "../libs/console.js";
sortPages(pages);

var clipPath, oldSize, oldLineWidth;
if (this.autoPaging === true) {
if (this.autoPaging) {
var min = pages[0];
var max = pages[pages.length - 1];
for (var i = min; i < max + 1; i++) {
this.pdf.setPage(i);

var topMargin = (i === min ? Math.max(this.posY, this.margin[0]) : this.margin[0]);
var firstPageHeight = this.pdf.internal.pageSize.height - Math.max(this.posY, this.margin[0]) - this.margin[2];
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[2];
var pageWidthMinusMargin = this.pdf.internal.pageSize.width - this.margin[1];
var previousPageHeightSum = i === 1 ? 0 : firstPageHeight + (i - 2) * pageHeightMinusMargin;

if (this.ctx.clip_path.length !== 0) {
var tmpPaths = this.path;
clipPath = JSON.parse(JSON.stringify(this.ctx.clip_path));
this.path = pathPositionRedo(
clipPath,
this.posX,
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY
Math.max(this.posX, this.margin[3]),
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
jeffsieu marked this conversation as resolved.
Show resolved Hide resolved
);
drawPaths.call(this, "fill", true);
this.path = tmpPaths;
}
var tmpRect = JSON.parse(JSON.stringify(textRect));
tmpRect = pathPositionRedo(
[tmpRect],
this.posX,
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY
Math.max(this.posX, this.margin[3]),
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
)[0];

if (options.scale >= 0.01) {
Expand All @@ -2037,12 +2070,24 @@ import { console } from "../libs/console.js";
oldLineWidth = this.lineWidth;
this.lineWidth = oldLineWidth * options.scale;
}
this.pdf.text(options.text, tmpRect.x, tmpRect.y, {
angle: options.angle,
align: textAlign,
renderingMode: options.renderingMode,
maxWidth: options.maxWidth
});

if (tmpRect.y <= pageHeightMinusMargin) {
if (tmpRect.y - tmpRect.h >= 0 && tmpRect.x <= pageWidthMinusMargin) {
this.pdf.text(options.text, tmpRect.x, tmpRect.y, {
angle: options.angle,
align: textAlign,
renderingMode: options.renderingMode,
maxWidth: options.maxWidth || pageWidthMinusMargin - tmpRect.x,
renderMaxWidthOverflow: false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is still the relict from the initial commit.

});
}
} else {
// This text is the last element of the page, but it got cut off due to the margin
// so we render it in the next page

// As a result, all other elements have their y offset increased
this.ctx.prevPageLastElemOffset += pageHeightMinusMargin - tmpRect.y + tmpRect.h;
}

if (options.scale >= 0.01) {
this.pdf.setFontSize(oldSize);
Expand All @@ -2056,7 +2101,7 @@ import { console } from "../libs/console.js";
oldLineWidth = this.lineWidth;
this.lineWidth = oldLineWidth * options.scale;
}
this.pdf.text(options.text, pt.x + this.posX, pt.y + this.posY, {
this.pdf.text(options.text, pt.x + Math.max(this.posX, this.margin[3]), pt.y + this.posY, {
angle: options.angle,
align: textAlign,
renderingMode: options.renderingMode,
Expand Down
1 change: 1 addition & 0 deletions src/modules/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ import { globalObject } from "../libs/globalObject.js";
pdf.context2d.autoPaging = true;
pdf.context2d.posX = this.opt.x;
pdf.context2d.posY = this.opt.y;
pdf.context2d.margin = this.opt.margin;

options.windowHeight = options.windowHeight || 0;
options.windowHeight =
Expand Down