-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Changes from 17 commits
d1f864b
16499ee
93aab85
184fcfa
d80fdf9
15264b0
d63fce6
d206850
56f2747
ccd252e
824be42
0d4f672
365b9bb
194c842
4f05e8c
14bacf1
e2dc83d
1643669
64d0959
08ad24d
c917272
f75ae8c
8931539
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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 ? this.posY + this.margin[0] : this.margin[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var firstPageHeight = this.pdf.internal.pageSize.height - this.posY - this.margin[0] - this.margin[2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[0] - 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -1653,22 +1675,27 @@ import { console } from "../libs/console.js"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.lineWidth = lineWidth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.lineJoin = lineJoin; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var topMargin = (k === min ? this.posY + this.margin[0] : this.margin[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var firstPageHeight = this.pdf.internal.pageSize.height - this.posY - this.margin[0] - this.margin[2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[0] - 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.posX + this.margin[3], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (isClip === false || k === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
drawPaths.call(this, rule, isClip); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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 === 1 ? this.posY + this.margin[0] : this.margin[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var firstPageHeight = this.pdf.internal.pageSize.height - this.posY - this.margin[0] - this.margin[2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[0] - 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.posX + this.margin[3], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-1 * previousPageHeightSum + topMargin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.posX + this.margin[3], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
)[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (options.scale >= 0.01) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var croppedText = this.pdf.splitTextToSize(options.text, options.maxWidth || pageWidthMinusMargin - tmpRect.x)[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.pdf.text(croppedText, tmpRect.x, tmpRect.y, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
angle: options.angle, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
align: textAlign, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
renderingMode: options.renderingMode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
renderMaxWidthOverflow: false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My previous comment regarding this code:
Was wrong, there are certain situations where one character can be attributed 2 pages (right on the edge of the bottom margin). The code was wrongfully pointing out characters as being out of range when they were not however and this is problematic when only one page was assigned to the character because in this situation the character will not be inserted in the pdf. This is because the comparison:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me, it looks like there is something severely broken. If you take the If I understand correctly, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (options.scale >= 0.01) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
this.pdf.setFontSize(oldSize); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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 + this.posX + this.margin[3], pt.y + this.posY, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
angle: options.angle, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
align: textAlign, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
renderingMode: options.renderingMode, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be 1 instead of min here as well.