dompdf.js 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/.versionrc +8 -0
  2. package/CHANGELOG.md +32 -543
  3. package/README.md +201 -155
  4. package/README_CN.md +233 -238
  5. package/dist/dompdf.esm.js +26010 -14837
  6. package/dist/dompdf.esm.js.map +1 -1
  7. package/dist/dompdf.js +40542 -29369
  8. package/dist/dompdf.js.map +1 -1
  9. package/dist/dompdf.min.js +67 -0
  10. package/dist/index.d.ts +7 -6
  11. package/dist/lib/dom/node-parser.js +0 -62
  12. package/dist/lib/dom/node-parser.js.map +1 -1
  13. package/dist/lib/index.js +41 -34
  14. package/dist/lib/index.js.map +1 -1
  15. package/dist/lib/render/canvas/pdf-renderer.js +358 -228
  16. package/dist/lib/render/canvas/pdf-renderer.js.map +1 -1
  17. package/dist/lib/render/paginate copy 2.js +178 -0
  18. package/dist/lib/render/paginate copy 2.js.map +1 -0
  19. package/dist/lib/render/paginate.js +68 -29
  20. package/dist/lib/render/paginate.js.map +1 -1
  21. package/dist/lib/utils/css-utils.js +19 -0
  22. package/dist/lib/utils/css-utils.js.map +1 -0
  23. package/dist/lib/utils/element-utils.js +25 -0
  24. package/dist/lib/utils/element-utils.js.map +1 -0
  25. package/dist/lib/utils/font-utils.js +70 -0
  26. package/dist/lib/utils/font-utils.js.map +1 -0
  27. package/dist/lib/utils/index.js +22 -0
  28. package/dist/lib/utils/index.js.map +1 -0
  29. package/dist/lib/utils/type-utils.js +40 -0
  30. package/dist/lib/utils/type-utils.js.map +1 -0
  31. package/dist/lib/utils/url-path.js +22 -0
  32. package/dist/lib/utils/url-path.js.map +1 -0
  33. package/dist/render/canvas/pdf-renderer.d.ts +29 -24
  34. package/dist/render/paginate copy 2.d.ts +3 -0
  35. package/dist/render/paginate.d.ts +1 -1
  36. package/dist/types/index.d.ts +7 -6
  37. package/dist/types/render/canvas/pdf-renderer.d.ts +29 -24
  38. package/dist/types/render/paginate copy 2.d.ts +3 -0
  39. package/dist/types/render/paginate.d.ts +1 -1
  40. package/dist/types/utils/css-utils.d.ts +2 -0
  41. package/dist/types/utils/element-utils.d.ts +7 -0
  42. package/dist/types/utils/font-utils.d.ts +9 -0
  43. package/dist/types/utils/index.d.ts +5 -0
  44. package/dist/types/utils/type-utils.d.ts +4 -0
  45. package/dist/types/utils/url-path.d.ts +7 -0
  46. package/dist/utils/css-utils.d.ts +2 -0
  47. package/dist/utils/element-utils.d.ts +7 -0
  48. package/dist/utils/font-utils.d.ts +9 -0
  49. package/dist/utils/index.d.ts +5 -0
  50. package/dist/utils/type-utils.d.ts +4 -0
  51. package/dist/utils/url-path.d.ts +7 -0
  52. package/fontconverter/FileSaver.js +2 -0
  53. package/fontconverter/filereader.js +432 -0
  54. package/fontconverter/fontconverter.html +66 -0
  55. package/fontconverter/pure-min.css +11 -0
  56. package/package.json +135 -135
  57. package/page_sizes.md +50 -0
  58. package/.vscode/settings.json +0 -5
  59. package/html2pdf-userscript.js +0 -936
@@ -51,17 +51,21 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
51
51
  }
52
52
  };
53
53
  Object.defineProperty(exports, "__esModule", { value: true });
54
- exports.CanvasRenderer = void 0;
54
+ exports.CanvasRenderer = exports.JsPdfContext2d = void 0;
55
55
  var jspdf_1 = require("jspdf");
56
+ require("jspdf/dist/polyfills.es.js");
56
57
  var bitwise_1 = require("../../core/bitwise");
57
58
  var bounds_1 = require("../../css/layout/bounds");
58
59
  var text_1 = require("../../css/layout/text");
59
60
  var line_height_1 = require("../../css/property-descriptors/line-height");
60
61
  var parser_1 = require("../../css/syntax/parser");
61
62
  var color_1 = require("../../css/types/color");
63
+ var gradient_1 = require("../../css/types/functions/gradient");
64
+ var image_1 = require("../../css/types/image");
62
65
  var length_percentage_1 = require("../../css/types/length-percentage");
63
66
  var select_element_container_1 = require("../../dom/elements/select-element-container");
64
67
  var textarea_element_container_1 = require("../../dom/elements/textarea-element-container");
68
+ // import {ReplacedElementContainer} from '../../dom/replaced-elements';
65
69
  var canvas_element_container_1 = require("../../dom/replaced-elements/canvas-element-container");
66
70
  var iframe_element_container_1 = require("../../dom/replaced-elements/iframe-element-container");
67
71
  var image_element_container_1 = require("../../dom/replaced-elements/image-element-container");
@@ -74,9 +78,18 @@ var bound_curves_1 = require("../bound-curves");
74
78
  var box_sizing_1 = require("../box-sizing");
75
79
  var effects_1 = require("../effects");
76
80
  var font_metrics_1 = require("../font-metrics");
81
+ // transformPath
82
+ var utils_1 = require("../../utils");
83
+ var path_1 = require("../path");
77
84
  var renderer_1 = require("../renderer");
78
85
  var stacking_context_1 = require("../stacking-context");
79
86
  var vector_1 = require("../vector");
87
+ var JsPdfContext2d = /** @class */ (function () {
88
+ function JsPdfContext2d() {
89
+ }
90
+ return JsPdfContext2d;
91
+ }());
92
+ exports.JsPdfContext2d = JsPdfContext2d;
80
93
  var CanvasRenderer = /** @class */ (function (_super) {
81
94
  __extends(CanvasRenderer, _super);
82
95
  function CanvasRenderer(context, options) {
@@ -84,58 +97,35 @@ var CanvasRenderer = /** @class */ (function (_super) {
84
97
  _this._activeEffects = [];
85
98
  _this.totalPages = 1;
86
99
  _this.canvas = options.canvas ? options.canvas : document.createElement('canvas');
87
- _this.ctx = _this.canvas.getContext('2d');
88
- var pxToPt = function (px) { return px * (72 / 96); };
89
- var pageWidth = pxToPt(options.width);
90
- var pageHeight = pxToPt(options.height);
91
- // const enc =
92
- // this.options.encryption &&
93
- // (this.options.encryption.userPassword ||
94
- // this.options.encryption.ownerPassword ||
95
- // (this.options.encryption.userPermissions && this.options.encryption.userPermissions.length))
96
- // ? this.options.encryption
97
- // : [];
98
- var encOptions = _this.options.encryption
99
- ? {
100
- userPassword: _this.options.encryption.userPassword,
101
- ownerPassword: _this.options.encryption.ownerPassword,
102
- userPermissions: _this.options.encryption.userPermissions
103
- }
104
- : undefined;
100
+ // this.ctx = this.canvas.getContext('2d') as CanvasRenderingContext2D;
101
+ _this.pxToPt = function (px) { return px * (72 / 96); };
102
+ var pageWidth = _this.pxToPt(options.width);
103
+ var pageHeight = _this.pxToPt(options.height);
104
+ // 如果 format 是数组,则将 px 转换为 pt
105
+ var format = Array.isArray(options.format) ? options.format.map(function (v) { return _this.pxToPt(v); }) : options.format;
105
106
  _this.jspdfCtx = new jspdf_1.jsPDF({
106
- orientation: pageWidth > pageHeight ? 'landscape' : 'portrait',
107
+ // orientation: pageWidth > pageHeight ? 'landscape' : 'landscape',
107
108
  unit: 'pt',
108
- format: options.pagination && options.format ? options.format : [pageHeight, pageWidth],
109
+ format: options.pagination && format ? format : [pageHeight, pageWidth],
109
110
  hotfixes: ['px_scaling'],
110
111
  putOnlyUsedFonts: options.putOnlyUsedFonts,
111
112
  compress: options.compress,
112
113
  precision: options.precision,
113
114
  floatPrecision: options.floatPrecision,
114
- encryption: encOptions
115
+ encryption: options.encryption
115
116
  });
116
- // orientation: pageWidth > pageHeight ? 'landscape' : 'portrait',
117
- // unit: 'pt',
118
- // format: options.pagination && options.format ? options.format : [pageHeight, pageWidth],
119
- // hotfixes: ['px_scaling'],
120
- // putOnlyUsedFonts: options.putOnlyUsedFonts,
121
- // compress: options.compress,
122
- // precision: options.precision,
123
- // floatPrecision: options.floatPrecision,
124
- // encryption: enc
125
- // });
126
117
  _this.context2dCtx = _this.jspdfCtx.context2d;
127
118
  _this.context2dCtx.scale(0.75, 0.75);
128
119
  _this.context2dCtx.translate(-options.x, -options.y);
129
120
  if (options.fontConfig) {
130
121
  try {
131
- _this.loadFont();
122
+ _this.addFontToJsPDF();
132
123
  }
133
124
  catch (error) {
134
125
  console.warn('Failed to set font:', error);
135
126
  _this.jspdfCtx.setFont('Helvetica');
136
127
  }
137
128
  }
138
- _this.pxToPt = pxToPt;
139
129
  if (!options.canvas) {
140
130
  _this.canvas.width = 10;
141
131
  _this.canvas.height = 10;
@@ -148,26 +138,55 @@ var CanvasRenderer = /** @class */ (function (_super) {
148
138
  _this.context.logger.debug("Canvas renderer initialized (".concat(options.width, "x").concat(options.height, ") with scale ").concat(options.scale));
149
139
  return _this;
150
140
  }
151
- CanvasRenderer.prototype.loadFont = function () {
152
- return __awaiter(this, void 0, void 0, function () {
153
- var fontData;
154
- return __generator(this, function (_a) {
155
- if (this.options.fontConfig.fontBase64) {
156
- fontData = this.options.fontConfig.fontBase64;
157
- }
158
- this.addFontToJsPDF(fontData);
159
- return [2 /*return*/];
160
- });
141
+ CanvasRenderer.prototype.addFontToJsPDF = function () {
142
+ var _this = this;
143
+ if ((0, utils_1.isEmptyValue)(this.options.fontConfig)) {
144
+ return;
145
+ }
146
+ var fonts = (0, utils_1.isObject)(this.options.fontConfig)
147
+ ? [this.options.fontConfig]
148
+ : this.options.fontConfig;
149
+ fonts.forEach(function (v) {
150
+ _this.jspdfCtx.addFileToVFS("".concat(v.fontFamily, ".ttf"), v.fontBase64);
151
+ _this.jspdfCtx.addFont("".concat(v.fontFamily, ".ttf"), v.fontFamily, 'normal');
152
+ _this.jspdfCtx.setFont(v.fontFamily);
161
153
  });
154
+ // console.log('render getFont', this.jspdfCtx.getFont());
155
+ this.context.logger.debug("setFont renderer initialized");
162
156
  };
163
- CanvasRenderer.prototype.addFontToJsPDF = function (fontData) {
164
- var fontFamily = this.options.fontConfig.fontFamily;
165
- if (!fontFamily) {
166
- return;
157
+ // reset all font
158
+ CanvasRenderer.prototype.resetJsPDFFont = function () {
159
+ var _this = this;
160
+ if ((0, utils_1.isArray)(this.options.fontConfig) && !(0, utils_1.isEmptyValue)(this.options.fontConfig)) {
161
+ this.options.fontConfig.forEach(function (v) {
162
+ v.fontFamily && _this.jspdfCtx.setFont(v.fontFamily);
163
+ });
167
164
  }
168
- this.jspdfCtx.addFileToVFS("".concat(fontFamily, ".ttf"), fontData);
169
- this.jspdfCtx.addFont("".concat(fontFamily, ".ttf"), fontFamily, 'normal');
170
- this.jspdfCtx.setFont(fontFamily);
165
+ };
166
+ // setFont form options
167
+ CanvasRenderer.prototype.setTextFont = function (styles) {
168
+ var _a, _b, _c;
169
+ // console.log(styles.fontWeight, styles.fontStyle, styles.fontFamily, 'styles');
170
+ if ((0, utils_1.isEmptyValue)(this.options.fontConfig)) {
171
+ return '';
172
+ }
173
+ var fontConfigRef = this.options.fontConfig;
174
+ // 处理字体图标场景
175
+ var isIconFont = fontConfigRef.find(function (v) { return v.iconFont; });
176
+ var otherFonts = fontConfigRef.filter(function (v) { return !v.iconFont; });
177
+ if (isIconFont && styles.fontFamily.some(function (family) { return family.includes(isIconFont.fontFamily); })) {
178
+ isIconFont.fontFamily && this.jspdfCtx.setFont(isIconFont.fontFamily);
179
+ return isIconFont.fontFamily;
180
+ }
181
+ // 除开字体图标场景
182
+ if (otherFonts.length === 1) {
183
+ var fontFamilyCustom_1 = (_a = otherFonts[0].fontFamily) !== null && _a !== void 0 ? _a : '';
184
+ fontFamilyCustom_1 && this.jspdfCtx.setFont(fontFamilyCustom_1);
185
+ return fontFamilyCustom_1;
186
+ }
187
+ var fontFamilyCustom = (_c = (_b = otherFonts.find(function (v) { return v.fontWeight === (styles.fontWeight > 500 ? 700 : 400) && v.fontStyle === styles.fontStyle; })) === null || _b === void 0 ? void 0 : _b.fontFamily) !== null && _c !== void 0 ? _c : '';
188
+ fontFamilyCustom && this.jspdfCtx.setFont(fontFamilyCustom);
189
+ return fontFamilyCustom;
171
190
  };
172
191
  CanvasRenderer.prototype.applyEffects = function (effects) {
173
192
  var _this = this;
@@ -177,27 +196,25 @@ var CanvasRenderer = /** @class */ (function (_super) {
177
196
  effects.forEach(function (effect) { return _this.applyEffect(effect); });
178
197
  };
179
198
  CanvasRenderer.prototype.applyEffect = function (effect) {
180
- this.ctx.save();
199
+ this.context2dCtx.save();
181
200
  if ((0, effects_1.isOpacityEffect)(effect)) {
182
- this.ctx.globalAlpha = effect.opacity;
201
+ this.context2dCtx.globalAlpha = effect.opacity;
183
202
  }
184
203
  if ((0, effects_1.isTransformEffect)(effect)) {
185
- this.ctx.translate(effect.offsetX, effect.offsetY);
186
- this.ctx.transform(effect.matrix[0], effect.matrix[1], effect.matrix[2], effect.matrix[3], effect.matrix[4], effect.matrix[5]);
187
- this.ctx.translate(-effect.offsetX, -effect.offsetY);
204
+ this.context2dCtx.translate(effect.offsetX, effect.offsetY);
205
+ this.context2dCtx.transform(effect.matrix[0], effect.matrix[1], effect.matrix[2], effect.matrix[3], effect.matrix[4], effect.matrix[5]);
206
+ this.context2dCtx.translate(-effect.offsetX, -effect.offsetY);
188
207
  }
189
208
  if ((0, effects_1.isClipEffect)(effect)) {
190
209
  this.path(effect.path);
191
- this.ctx.clip();
210
+ this.context2dCtx.clip();
192
211
  }
193
212
  this._activeEffects.push(effect);
194
213
  };
195
214
  CanvasRenderer.prototype.popEffect = function () {
196
215
  this._activeEffects.pop();
197
216
  this.context2dCtx.restore();
198
- if (this.options.fontConfig && this.options.fontConfig.fontFamily) {
199
- this.jspdfCtx.setFont(this.options.fontConfig.fontFamily);
200
- }
217
+ this.resetJsPDFFont();
201
218
  };
202
219
  CanvasRenderer.prototype.renderStack = function (stack) {
203
220
  return __awaiter(this, void 0, void 0, function () {
@@ -246,7 +263,8 @@ var CanvasRenderer = /** @class */ (function (_super) {
246
263
  var letters = (0, text_1.segmentGraphemes)(text.text);
247
264
  letters.reduce(function (left, letter) {
248
265
  _this.context2dCtx.fillText(letter, left, text.bounds.top + baseline);
249
- return left + _this.ctx.measureText(letter).width;
266
+ return left + _this.context2dCtx.measureText(letter);
267
+ // return left + this.context2dCtx.measureText(letter).width;
250
268
  }, text.bounds.left);
251
269
  }
252
270
  };
@@ -274,19 +292,19 @@ var CanvasRenderer = /** @class */ (function (_super) {
274
292
  return "#".concat(r.toString(16).padStart(2, '0')).concat(g.toString(16).padStart(2, '0')).concat(b
275
293
  .toString(16)
276
294
  .padStart(2, '0'));
277
- return (0, color_1.asString)(color);
295
+ // return asString(color);
278
296
  };
279
297
  CanvasRenderer.prototype.renderTextNode = function (text, styles) {
280
298
  return __awaiter(this, void 0, void 0, function () {
281
- var _a, font, fontFamily, fontSize, fontSizePt, _b, baseline, middle, paintOrder;
299
+ var _a, font, fontFamily, fontSize, fontFamilyFinal, fontSizePt, _b, baseline, middle, paintOrder;
282
300
  var _this = this;
283
301
  return __generator(this, function (_c) {
284
302
  _a = this.createFontStyle(styles), font = _a[0], fontFamily = _a[1], fontSize = _a[2];
285
- this.ctx.font = font;
286
- this.context2dCtx.font = this.options.fontConfig.fontFamily;
287
- this.ctx.direction = styles.direction === 1 /* DIRECTION.RTL */ ? 'rtl' : 'ltr';
288
- this.context2dCtx.direction = styles.direction === 1 /* DIRECTION.RTL */ ? 'rtl' : 'ltr';
289
- this.ctx.textAlign = 'left';
303
+ fontFamilyFinal = this.setTextFont(styles);
304
+ this.context2dCtx.font = fontFamilyFinal || font;
305
+ // console.log(fontFamilyFinal, styles, 'render getFont', this.jspdfCtx.getFont());
306
+ // jspdf context2d not supported ‘direction’
307
+ // this.context2dCtx.direction = styles.direction === DIRECTION.RTL ? 'rtl' : 'ltr';
290
308
  this.context2dCtx.textAlign = 'left';
291
309
  fontSizePt = styles.fontSize.number;
292
310
  this.jspdfCtx.setFontSize(fontSizePt);
@@ -338,27 +356,65 @@ var CanvasRenderer = /** @class */ (function (_super) {
338
356
  });
339
357
  });
340
358
  };
341
- CanvasRenderer.prototype.renderReplacedElement = function (container, curves, image) {
342
- if (image && container.intrinsicWidth > 0 && container.intrinsicHeight > 0) {
343
- var box = (0, box_sizing_1.contentBox)(container);
344
- var path = (0, bound_curves_1.calculatePaddingBoxPath)(curves);
345
- this.path(path);
346
- this.ctx.save();
347
- this.ctx.clip();
348
- this.ctx.drawImage(image, 0, 0, container.intrinsicWidth, container.intrinsicHeight, box.left, box.top, box.width, box.height);
349
- this.ctx.restore();
359
+ CanvasRenderer.prototype.renderReplacedJsPdfImage = function (container, image) {
360
+ var bounds = (0, box_sizing_1.contentBox)(container);
361
+ var x = this.pxToPt(bounds.left - this.options.x);
362
+ var y = this.pxToPt(bounds.top - this.options.y);
363
+ var width = this.pxToPt(bounds.width);
364
+ var height = this.pxToPt(bounds.height);
365
+ // fix: url is svg image export
366
+ if ((0, utils_1.getImageTypeByPath)(container.src, 'svg')) {
367
+ var canvas = document.createElement('canvas');
368
+ canvas.width = width;
369
+ canvas.height = height;
370
+ var ctx = canvas.getContext('2d');
371
+ if (ctx) {
372
+ ctx.clearRect(0, 0, width, height);
373
+ ctx.drawImage(image, 0, 0, width, height);
374
+ var dataURL = canvas.toDataURL('image/png', 0.8);
375
+ this.addImagePdf(dataURL, 'PNG', x, y, width, height);
376
+ }
377
+ }
378
+ else {
379
+ this.addImagePdf(image, 'JPEG', x, y, width, height);
380
+ }
381
+ };
382
+ CanvasRenderer.prototype.renderReplacedJsPdfSvg = function (container, image) {
383
+ var bounds = (0, box_sizing_1.contentBox)(container);
384
+ var x = this.pxToPt(bounds.left - this.options.x);
385
+ var y = this.pxToPt(bounds.top - this.options.y);
386
+ var width = this.pxToPt(bounds.width);
387
+ var height = this.pxToPt(bounds.height);
388
+ var canvas = document.createElement('canvas');
389
+ canvas.width = width;
390
+ canvas.height = height;
391
+ var ctx = canvas.getContext('2d');
392
+ if (ctx) {
393
+ ctx.clearRect(0, 0, width, height);
394
+ ctx.drawImage(image, 0, 0, width, height);
395
+ var dataURL = canvas.toDataURL('image/png', 0.8);
396
+ this.addImagePdf(dataURL, 'PNG', x, y, width, height);
350
397
  }
351
398
  };
399
+ CanvasRenderer.prototype.renderReplacedJsPdfCanvasImage = function (container) {
400
+ var bounds = (0, box_sizing_1.contentBox)(container);
401
+ var x = this.pxToPt(bounds.left - this.options.x);
402
+ var y = this.pxToPt(bounds.top - this.options.y);
403
+ var width = this.pxToPt(bounds.width);
404
+ var height = this.pxToPt(bounds.height);
405
+ var dataURL = container.canvas.toDataURL('image/png', 0.8);
406
+ this.addImagePdf(dataURL, 'PNG', x, y, width, height);
407
+ };
352
408
  CanvasRenderer.prototype.renderNodeContent = function (paint) {
353
409
  return __awaiter(this, void 0, void 0, function () {
354
- var container, curves, styles, _i, _a, child, image, bounds, x, y, width, height, e_1, bounds, x, y, width, height, dataURL, image, bounds, x, y, width, height, canvas, ctx, dataURL, e_2, size, _b, fontFamily, fontSize, baseline, bounds, x, textBounds, img, image, url, e_3, fontFamily, bounds;
410
+ var container, styles, _i, _a, child, image, e_1, image, e_2, size, _b, fontFamily, fontSize, baseline, bounds, x, textBounds, img, image, url, iconWidth, iconHeight, e_3, fontFamily, bounds;
355
411
  return __generator(this, function (_c) {
356
412
  switch (_c.label) {
357
413
  case 0:
358
414
  this.applyEffects(paint.getEffects(4 /* EffectTarget.CONTENT */));
359
415
  container = paint.container;
360
- curves = paint.curves;
361
416
  styles = container.styles;
417
+ this.resetJsPDFFont();
362
418
  _i = 0, _a = container.textNodes;
363
419
  _c.label = 1;
364
420
  case 1:
@@ -379,18 +435,7 @@ var CanvasRenderer = /** @class */ (function (_super) {
379
435
  return [4 /*yield*/, this.context.cache.match(container.src)];
380
436
  case 6:
381
437
  image = _c.sent();
382
- this.renderReplacedElement(container, curves, image);
383
- try {
384
- bounds = (0, box_sizing_1.contentBox)(container);
385
- x = this.pxToPt(bounds.left - this.options.x);
386
- y = this.pxToPt(bounds.top - this.options.y);
387
- width = this.pxToPt(bounds.width);
388
- height = this.pxToPt(bounds.height);
389
- this.addImagePdf(image, 'JPEG', x, y, width, height);
390
- }
391
- catch (err) {
392
- this.context.logger.error("Error adding image to PDF: ".concat(err));
393
- }
438
+ this.renderReplacedJsPdfImage(container, image);
394
439
  return [3 /*break*/, 8];
395
440
  case 7:
396
441
  e_1 = _c.sent();
@@ -398,15 +443,8 @@ var CanvasRenderer = /** @class */ (function (_super) {
398
443
  return [3 /*break*/, 8];
399
444
  case 8:
400
445
  if (container instanceof canvas_element_container_1.CanvasElementContainer) {
401
- this.renderReplacedElement(container, curves, container.canvas);
402
446
  try {
403
- bounds = (0, box_sizing_1.contentBox)(container);
404
- x = this.pxToPt(bounds.left - this.options.x);
405
- y = this.pxToPt(bounds.top - this.options.y);
406
- width = this.pxToPt(bounds.width);
407
- height = this.pxToPt(bounds.height);
408
- dataURL = container.canvas.toDataURL('image/png', 0.95);
409
- this.addImagePdf(dataURL, 'PNG', x, y, width, height);
447
+ this.renderReplacedJsPdfCanvasImage(container);
410
448
  }
411
449
  catch (err) {
412
450
  this.context.logger.error("Error adding canvas to PDF: ".concat(err));
@@ -419,27 +457,7 @@ var CanvasRenderer = /** @class */ (function (_super) {
419
457
  return [4 /*yield*/, this.context.cache.match(container.svg)];
420
458
  case 10:
421
459
  image = _c.sent();
422
- this.renderReplacedElement(container, curves, image);
423
- try {
424
- bounds = (0, box_sizing_1.contentBox)(container);
425
- x = this.pxToPt(bounds.left - this.options.x);
426
- y = this.pxToPt(bounds.top - this.options.y);
427
- width = this.pxToPt(bounds.width);
428
- height = this.pxToPt(bounds.height);
429
- canvas = document.createElement('canvas');
430
- canvas.width = container.intrinsicWidth || image.width;
431
- canvas.height = container.intrinsicHeight || image.height;
432
- ctx = canvas.getContext('2d');
433
- if (ctx) {
434
- ctx.clearRect(0, 0, canvas.width, canvas.height);
435
- ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
436
- dataURL = canvas.toDataURL('image/png');
437
- this.addImagePdf(dataURL, 'PNG', x, y, width, height);
438
- }
439
- }
440
- catch (err) {
441
- this.context.logger.error("Error adding SVG to PDF: ".concat(err));
442
- }
460
+ this.renderReplacedJsPdfSvg(container, image);
443
461
  return [3 /*break*/, 12];
444
462
  case 11:
445
463
  e_2 = _c.sent();
@@ -465,9 +483,6 @@ var CanvasRenderer = /** @class */ (function (_super) {
465
483
  this.context2dCtx.fillStyle = this.convertColor(input_element_container_1.INPUT_COLOR);
466
484
  this.context2dCtx.fill();
467
485
  this.context2dCtx.restore();
468
- if (this.options.fontConfig && this.options.fontConfig.fontFamily) {
469
- this.jspdfCtx.setFont(this.options.fontConfig.fontFamily);
470
- }
471
486
  }
472
487
  }
473
488
  else if (container.type === input_element_container_1.RADIO) {
@@ -523,7 +538,9 @@ var CanvasRenderer = /** @class */ (function (_super) {
523
538
  return [4 /*yield*/, this.context.cache.match(url)];
524
539
  case 14:
525
540
  image = _c.sent();
526
- this.context2dCtx.drawImage(image, container.bounds.left - (image.width + 10), container.bounds.top);
541
+ iconWidth = image.width;
542
+ iconHeight = image.height;
543
+ this.context2dCtx.drawImage(image, container.bounds.left - (image.width + 10), container.bounds.top, iconWidth, iconHeight);
527
544
  return [3 /*break*/, 16];
528
545
  case 15:
529
546
  e_3 = _c.sent();
@@ -543,11 +560,7 @@ var CanvasRenderer = /** @class */ (function (_super) {
543
560
  this.context2dCtx.textAlign = 'left';
544
561
  }
545
562
  _c.label = 18;
546
- case 18:
547
- if (this.options.fontConfig && this.options.fontConfig.fontFamily) {
548
- this.jspdfCtx.setFont(this.options.fontConfig.fontFamily);
549
- }
550
- return [2 /*return*/];
563
+ case 18: return [2 /*return*/];
551
564
  }
552
565
  });
553
566
  });
@@ -671,32 +684,36 @@ var CanvasRenderer = /** @class */ (function (_super) {
671
684
  this.formatPath(paths.slice(0).reverse());
672
685
  this.context2dCtx.closePath();
673
686
  };
674
- CanvasRenderer.prototype.path = function (paths) {
675
- this.context2dCtx.beginPath();
676
- this.formatPath(paths);
677
- this.context2dCtx.closePath();
687
+ CanvasRenderer.prototype.path = function (paths, ctx2d) {
688
+ var contextCtx = ctx2d ? ctx2d : this.context2dCtx;
689
+ contextCtx.beginPath();
690
+ this.formatPath(paths, contextCtx);
691
+ contextCtx.closePath();
678
692
  };
679
- CanvasRenderer.prototype.formatPath = function (paths) {
680
- var _this = this;
693
+ CanvasRenderer.prototype.formatPath = function (paths, ctx2d) {
694
+ var contextCtx = ctx2d ? ctx2d : this.context2dCtx;
681
695
  paths.forEach(function (point, index) {
682
696
  var start = (0, bezier_curve_1.isBezierCurve)(point) ? point.start : point;
683
697
  if (index === 0) {
684
- _this.context2dCtx.moveTo(start.x, start.y);
698
+ contextCtx.moveTo(start.x, start.y);
685
699
  }
686
700
  else {
687
- _this.context2dCtx.lineTo(start.x, start.y);
701
+ contextCtx.lineTo(start.x, start.y);
688
702
  }
689
703
  if ((0, bezier_curve_1.isBezierCurve)(point)) {
690
- _this.context2dCtx.bezierCurveTo(point.startControl.x, point.startControl.y, point.endControl.x, point.endControl.y, point.end.x, point.end.y);
704
+ contextCtx.bezierCurveTo(point.startControl.x, point.startControl.y, point.endControl.x, point.endControl.y, point.end.x, point.end.y);
691
705
  }
692
706
  });
693
707
  };
694
- CanvasRenderer.prototype.renderRepeat = function (path, pattern, offsetX, offsetY) {
695
- this.path(path);
696
- this.ctx.fillStyle = pattern;
697
- this.context2dCtx.translate(offsetX, offsetY);
698
- this.context2dCtx.fill();
699
- this.context2dCtx.translate(-offsetX, -offsetY);
708
+ CanvasRenderer.prototype.renderRepeat = function (boxs, ctx, path, pattern) {
709
+ // renderRepeat(boxs: Bounds, ctx: CanvasRenderingContext2D, path: Path[], pattern: CanvasPattern | CanvasGradient, offsetX: number, offsetY: number): void {
710
+ var contextCtx = ctx;
711
+ this.path(path, contextCtx);
712
+ contextCtx.fillStyle = pattern;
713
+ contextCtx.translate(0, 0);
714
+ // contextCtx.fill();
715
+ contextCtx.fillRect(0, 0, boxs.width, boxs.height); // 绘制填充矩形(此时坐标系已偏移)
716
+ contextCtx.translate(-0, -0);
700
717
  };
701
718
  CanvasRenderer.prototype.resizeImage = function (image, width, height) {
702
719
  var _a;
@@ -712,54 +729,166 @@ var CanvasRenderer = /** @class */ (function (_super) {
712
729
  return canvas;
713
730
  };
714
731
  CanvasRenderer.prototype.renderBackgroundImage = function (container) {
732
+ var _a, _b;
715
733
  return __awaiter(this, void 0, void 0, function () {
716
- var index, _i, _a, backgroundImage, image, url, e_4, _b, path, x, y, width, height, pattern, xPt, yPt, widthPt, heightPt;
717
- return __generator(this, function (_c) {
718
- switch (_c.label) {
734
+ var index, _loop_1, this_1, _i, _c, backgroundImage;
735
+ return __generator(this, function (_d) {
736
+ switch (_d.label) {
719
737
  case 0:
720
738
  index = container.styles.backgroundImage.length - 1;
721
- _i = 0, _a = container.styles.backgroundImage.slice(0).reverse();
722
- _c.label = 1;
739
+ _loop_1 = function (backgroundImage) {
740
+ var image, url, _e, path, x, y, width, height, boxs, ownerDocument, canvas, ctx, repeatStr, xPt, yPt, widthPt, heightPt, resizeImg, pattern, pathTs, dataURL, xPt, yPt, widthPt, heightPt, e_4, _f, path, x, y, width, height, _g, lineLength, x0, x1, y0, y1, boxs, canvas, ctx, gradient_2, pattern, pathTs, dataURL, xPt, yPt, widthPt, heightPt, _h, path, left, top_1, width, height, position, x, y, _j, rx, ry, ownerDocument, canvas, ctx, radialGradient_1, pathTs, midX, midY, f, invF, dataURL, xPt, yPt, widthPt, heightPt;
741
+ return __generator(this, function (_k) {
742
+ switch (_k.label) {
743
+ case 0:
744
+ if (!(backgroundImage.type === 0 /* CSSImageType.URL */)) return [3 /*break*/, 5];
745
+ image = void 0;
746
+ url = backgroundImage.url;
747
+ _k.label = 1;
748
+ case 1:
749
+ _k.trys.push([1, 3, , 4]);
750
+ return [4 /*yield*/, this_1.context.cache.match(url)];
751
+ case 2:
752
+ image = _k.sent();
753
+ if (image) {
754
+ _e = (0, background_1.calculateBackgroundRendering)(container, index, [
755
+ image.width,
756
+ image.height,
757
+ image.width / image.height
758
+ ]), path = _e[0], x = _e[1], y = _e[2], width = _e[3], height = _e[4];
759
+ boxs = (0, box_sizing_1.contentBox)(container);
760
+ ownerDocument = (_a = this_1.canvas.ownerDocument) !== null && _a !== void 0 ? _a : document;
761
+ canvas = ownerDocument.createElement('canvas');
762
+ canvas.width = Math.max(1, boxs.width);
763
+ canvas.height = Math.max(1, boxs.height);
764
+ ctx = canvas.getContext('2d');
765
+ ctx.save();
766
+ repeatStr = (0, utils_1.getBackgroundRepeat)(container.styles.backgroundRepeat[0]);
767
+ if (container.styles.backgroundRepeat[0] === 1 /* BACKGROUND_REPEAT.NO_REPEAT */) {
768
+ xPt = this_1.pxToPt(x - this_1.options.x);
769
+ yPt = this_1.pxToPt(y - this_1.options.y);
770
+ widthPt = this_1.pxToPt(width);
771
+ heightPt = this_1.pxToPt(height);
772
+ this_1.addImagePdf(image, 'JPEG', xPt, yPt, widthPt, heightPt);
773
+ }
774
+ else {
775
+ resizeImg = this_1.resizeImage(image, width, height);
776
+ pattern = ctx.createPattern(resizeImg, repeatStr);
777
+ pathTs = (0, path_1.transformPath)(path, -x, -y, 0, 0);
778
+ this_1.renderRepeat(boxs, ctx, pathTs, pattern);
779
+ dataURL = canvas.toDataURL('image/jpeg', 0.8);
780
+ // console.log(dataURL, 'dataURL', image)
781
+ ctx.restore();
782
+ xPt = this_1.pxToPt(boxs.left - this_1.options.x);
783
+ yPt = this_1.pxToPt(boxs.top - this_1.options.y);
784
+ widthPt = this_1.pxToPt(boxs.width);
785
+ heightPt = this_1.pxToPt(boxs.height);
786
+ this_1.addImagePdf(dataURL, 'JPEG', xPt, yPt, widthPt, heightPt);
787
+ }
788
+ }
789
+ return [3 /*break*/, 4];
790
+ case 3:
791
+ e_4 = _k.sent();
792
+ this_1.context.logger.error("Error loading background-image ".concat(url));
793
+ return [3 /*break*/, 4];
794
+ case 4: return [3 /*break*/, 6];
795
+ case 5:
796
+ if ((0, image_1.isLinearGradient)(backgroundImage)) {
797
+ _f = (0, background_1.calculateBackgroundRendering)(container, index, [null, null, null]), path = _f[0], x = _f[1], y = _f[2], width = _f[3], height = _f[4];
798
+ _g = (0, gradient_1.calculateGradientDirection)(backgroundImage.angle, width, height), lineLength = _g[0], x0 = _g[1], x1 = _g[2], y0 = _g[3], y1 = _g[4];
799
+ boxs = (0, box_sizing_1.contentBox)(container);
800
+ canvas = document.createElement('canvas');
801
+ canvas.width = width;
802
+ canvas.height = height;
803
+ ctx = canvas.getContext('2d');
804
+ gradient_2 = ctx.createLinearGradient(x0, y0, x1, y1);
805
+ (0, gradient_1.processColorStops)(backgroundImage.stops, lineLength).forEach(function (colorStop) {
806
+ return gradient_2.addColorStop(colorStop.stop, (0, color_1.asString)(colorStop.color));
807
+ });
808
+ ctx.fillStyle = gradient_2;
809
+ ctx.fillRect(0, 0, width, height);
810
+ if (width > 0 && height > 0) {
811
+ pattern = ctx.createPattern(canvas, 'repeat');
812
+ pathTs = (0, path_1.transformPath)(path, -x, -y, 0, 0);
813
+ this_1.renderRepeat(boxs, ctx, pathTs, pattern);
814
+ }
815
+ dataURL = canvas.toDataURL('image/jpeg', 0.8);
816
+ // console.log(dataURL, 'dataURL', image)
817
+ ctx.restore();
818
+ xPt = this_1.pxToPt(x - this_1.options.x);
819
+ yPt = this_1.pxToPt(y - this_1.options.y);
820
+ widthPt = this_1.pxToPt(width);
821
+ heightPt = this_1.pxToPt(height);
822
+ this_1.addImagePdf(dataURL, 'JPEG', xPt, yPt, widthPt, heightPt);
823
+ }
824
+ else if ((0, image_1.isRadialGradient)(backgroundImage)) {
825
+ _h = (0, background_1.calculateBackgroundRendering)(container, index, [
826
+ null,
827
+ null,
828
+ null
829
+ ]), path = _h[0], left = _h[1], top_1 = _h[2], width = _h[3], height = _h[4];
830
+ position = backgroundImage.position.length === 0 ? [length_percentage_1.FIFTY_PERCENT] : backgroundImage.position;
831
+ x = (0, length_percentage_1.getAbsoluteValue)(position[0], width);
832
+ y = (0, length_percentage_1.getAbsoluteValue)(position[position.length - 1], height);
833
+ _j = (0, gradient_1.calculateRadius)(backgroundImage, x, y, width, height), rx = _j[0], ry = _j[1];
834
+ ownerDocument = (_b = this_1.canvas.ownerDocument) !== null && _b !== void 0 ? _b : document;
835
+ canvas = ownerDocument.createElement('canvas');
836
+ canvas.width = Math.max(1, width);
837
+ canvas.height = Math.max(1, height);
838
+ ctx = canvas.getContext('2d');
839
+ if (rx > 0 && ry > 0) {
840
+ radialGradient_1 = ctx.createRadialGradient(x, y, 0, x, y, rx);
841
+ (0, gradient_1.processColorStops)(backgroundImage.stops, rx * 2).forEach(function (colorStop) {
842
+ return radialGradient_1.addColorStop(colorStop.stop, (0, color_1.asString)(colorStop.color));
843
+ });
844
+ pathTs = (0, path_1.transformPath)(path, -left, -top_1, 0, 0);
845
+ this_1.path(pathTs, ctx);
846
+ ctx.fillStyle = radialGradient_1;
847
+ if (rx !== ry) {
848
+ midX = 0 + 0.5 * width;
849
+ midY = 0 + 0.5 * height;
850
+ f = ry / rx;
851
+ invF = 1 / f;
852
+ ctx.save();
853
+ ctx.translate(midX, midY);
854
+ ctx.transform(1, 0, 0, f, 0, 0);
855
+ ctx.translate(-midX, -midY);
856
+ ctx.fillRect(0, invF * (0 - midY) + midY, width, height * invF);
857
+ // ctx.fillRect(left, invF * (top - midY) + midY, width, height * invF);
858
+ ctx.restore();
859
+ }
860
+ else {
861
+ ctx.fill();
862
+ }
863
+ }
864
+ dataURL = canvas.toDataURL('image/jpeg', 0.8);
865
+ xPt = this_1.pxToPt(left - this_1.options.x);
866
+ yPt = this_1.pxToPt(top_1 - this_1.options.y);
867
+ widthPt = this_1.pxToPt(width);
868
+ heightPt = this_1.pxToPt(height);
869
+ this_1.addImagePdf(dataURL, 'JPEG', xPt, yPt, widthPt, heightPt);
870
+ }
871
+ _k.label = 6;
872
+ case 6:
873
+ index--;
874
+ return [2 /*return*/];
875
+ }
876
+ });
877
+ };
878
+ this_1 = this;
879
+ _i = 0, _c = container.styles.backgroundImage.slice(0).reverse();
880
+ _d.label = 1;
723
881
  case 1:
724
- if (!(_i < _a.length)) return [3 /*break*/, 8];
725
- backgroundImage = _a[_i];
726
- if (!(backgroundImage.type === 0 /* CSSImageType.URL */)) return [3 /*break*/, 6];
727
- image = void 0;
728
- url = backgroundImage.url;
729
- _c.label = 2;
882
+ if (!(_i < _c.length)) return [3 /*break*/, 4];
883
+ backgroundImage = _c[_i];
884
+ return [5 /*yield**/, _loop_1(backgroundImage)];
730
885
  case 2:
731
- _c.trys.push([2, 4, , 5]);
732
- return [4 /*yield*/, this.context.cache.match(url)];
886
+ _d.sent();
887
+ _d.label = 3;
733
888
  case 3:
734
- image = _c.sent();
735
- return [3 /*break*/, 5];
736
- case 4:
737
- e_4 = _c.sent();
738
- this.context.logger.error("Error loading background-image ".concat(url));
739
- return [3 /*break*/, 5];
740
- case 5:
741
- if (image) {
742
- _b = (0, background_1.calculateBackgroundRendering)(container, index, [
743
- image.width,
744
- image.height,
745
- image.width / image.height
746
- ]), path = _b[0], x = _b[1], y = _b[2], width = _b[3], height = _b[4];
747
- pattern = this.ctx.createPattern(this.resizeImage(image, width, height), 'repeat');
748
- this.renderRepeat(path, pattern, x, y);
749
- xPt = this.pxToPt(x - this.options.x);
750
- yPt = this.pxToPt(y - this.options.y);
751
- widthPt = this.pxToPt(width);
752
- heightPt = this.pxToPt(height);
753
- this.addImagePdf(image, 'JPEG', xPt, yPt, widthPt, heightPt);
754
- }
755
- _c.label = 6;
756
- case 6:
757
- index--;
758
- _c.label = 7;
759
- case 7:
760
889
  _i++;
761
890
  return [3 /*break*/, 1];
762
- case 8: return [2 /*return*/];
891
+ case 4: return [2 /*return*/];
763
892
  }
764
893
  });
765
894
  });
@@ -817,9 +946,7 @@ var CanvasRenderer = /** @class */ (function (_super) {
817
946
  backgroundPaintingArea = calculateBackgroundCurvedPaintingArea((0, background_1.getBackgroundValueForIndex)(styles.backgroundClip, 0), paint.curves);
818
947
  foreignobjectrendering = paint.container.foreignobjectrendering;
819
948
  if (!(hasBackground || styles.boxShadow.length)) return [3 /*break*/, 2];
820
- if (this.options.fontConfig && this.options.fontConfig.fontFamily) {
821
- this.jspdfCtx.setFont(this.options.fontConfig.fontFamily);
822
- }
949
+ // console.log('render getFont', this.jspdfCtx.getFont());
823
950
  if (!foreignobjectrendering) {
824
951
  this.context2dCtx.save();
825
952
  this.path(backgroundPaintingArea);
@@ -833,9 +960,7 @@ var CanvasRenderer = /** @class */ (function (_super) {
833
960
  case 1:
834
961
  _a.sent();
835
962
  this.context2dCtx.restore();
836
- if (this.options.fontConfig && this.options.fontConfig.fontFamily) {
837
- this.jspdfCtx.setFont(this.options.fontConfig.fontFamily);
838
- }
963
+ this.resetJsPDFFont();
839
964
  _a.label = 2;
840
965
  case 2:
841
966
  side = 0;
@@ -881,7 +1006,7 @@ var CanvasRenderer = /** @class */ (function (_super) {
881
1006
  };
882
1007
  CanvasRenderer.prototype.renderDashedDottedBorder = function (color, width, side, curvePoints, style) {
883
1008
  return __awaiter(this, void 0, void 0, function () {
884
- var strokePaths, boxPaths, startX, startY, endX, endY, length, dashLength, spaceLength, useLineDash, multiplier, numberOfDashes, minSpace, maxSpace, path1, path2, x1, y1, x2, y2, path1, path2;
1009
+ var strokePaths, boxPaths, startX, startY, endX, endY, length, dashLength, spaceLength, useLineDash, multiplier, numberOfDashes, minSpace, maxSpace, path1, path2, path1, path2;
885
1010
  return __generator(this, function (_a) {
886
1011
  this.context2dCtx.save();
887
1012
  strokePaths = (0, border_1.parsePathForBorderStroke)(curvePoints, side);
@@ -913,7 +1038,6 @@ var CanvasRenderer = /** @class */ (function (_super) {
913
1038
  length = Math.abs(startY - endY);
914
1039
  }
915
1040
  this.context2dCtx.beginPath();
916
- this.jspdfCtx.setDrawColor(this.convertColor(color));
917
1041
  if (style === 3 /* BORDER_STYLE.DOTTED */) {
918
1042
  this.formatPath(strokePaths);
919
1043
  }
@@ -946,45 +1070,40 @@ var CanvasRenderer = /** @class */ (function (_super) {
946
1070
  }
947
1071
  if (useLineDash) {
948
1072
  if (style === 3 /* BORDER_STYLE.DOTTED */) {
949
- this.jspdfCtx.setLineDashPattern([0, dashLength + spaceLength], 0);
1073
+ this.context2dCtx.setLineDash([0, dashLength + spaceLength]);
950
1074
  }
951
1075
  else {
952
- this.jspdfCtx.setLineDashPattern([dashLength, spaceLength], 0);
1076
+ this.context2dCtx.setLineDash([dashLength, spaceLength]);
953
1077
  }
954
1078
  }
955
1079
  if (style === 3 /* BORDER_STYLE.DOTTED */) {
956
- this.jspdfCtx.setLineCap('round');
957
- this.jspdfCtx.setLineWidth(width);
1080
+ this.context2dCtx.lineCap = 'round';
1081
+ this.context2dCtx.lineWidth = width;
958
1082
  }
959
1083
  else {
960
- this.jspdfCtx.setLineWidth(width * 2 + 1.1);
1084
+ this.context2dCtx.lineWidth = width * 2 + 1.1;
961
1085
  }
962
- this.jspdfCtx.stroke();
963
- this.jspdfCtx.setLineDashPattern([], 0);
1086
+ this.context2dCtx.strokeStyle = (0, color_1.asString)(color);
1087
+ this.context2dCtx.stroke();
1088
+ this.context2dCtx.setLineDash([]);
1089
+ // dashed round edge gap
964
1090
  if (style === 2 /* BORDER_STYLE.DASHED */) {
965
1091
  if ((0, bezier_curve_1.isBezierCurve)(boxPaths[0])) {
966
1092
  path1 = boxPaths[3];
967
1093
  path2 = boxPaths[0];
968
- x1 = this.pxToPt(path1.end.x);
969
- y1 = this.pxToPt(path1.end.y);
970
- x2 = this.pxToPt(path2.start.x);
971
- y2 = this.pxToPt(path2.start.y);
972
- try {
973
- this.jspdfCtx.line(x1, y1, x2, y2);
974
- this.jspdfCtx.stroke();
975
- }
976
- catch (error) {
977
- console.warn('Failed to draw dashed border:', error);
978
- }
1094
+ this.context2dCtx.beginPath();
1095
+ this.formatPath([new vector_1.Vector(path1.end.x, path1.end.y), new vector_1.Vector(path2.start.x, path2.start.y)]);
1096
+ this.context2dCtx.stroke();
979
1097
  }
980
1098
  if ((0, bezier_curve_1.isBezierCurve)(boxPaths[1])) {
981
1099
  path1 = boxPaths[1];
982
1100
  path2 = boxPaths[2];
983
- this.jspdfCtx.lines([[path1.end.x, path1.end.y, path2.start.x, path2.start.y]], path1.end.x, path1.end.y);
984
- this.jspdfCtx.stroke();
1101
+ this.context2dCtx.beginPath();
1102
+ this.formatPath([new vector_1.Vector(path1.end.x, path1.end.y), new vector_1.Vector(path2.start.x, path2.start.y)]);
1103
+ this.context2dCtx.stroke();
985
1104
  }
986
1105
  }
987
- this.jspdfCtx.restoreGraphicsState();
1106
+ this.context2dCtx.restore();
988
1107
  return [2 /*return*/];
989
1108
  });
990
1109
  });
@@ -1023,22 +1142,32 @@ var CanvasRenderer = /** @class */ (function (_super) {
1023
1142
  _a.sent();
1024
1143
  this.applyEffects([]);
1025
1144
  if (cfg === null || cfg === void 0 ? void 0 : cfg.header) {
1026
- headerText = String(cfg.header.content)
1027
- .replace('${currentPage}', String(pageNum))
1028
- .replace('${totalPages}', String(this.totalPages));
1029
- this.jspdfCtx.setFontSize(this.safe(this.pxToPt(cfg.header.contentFontSize), 1));
1030
- this.setTextColorFromString(cfg.header.contentColor);
1031
- headerPos = this.computeContentPosition(cfg.header.contentPosition, pageW, pageH, cfg.header.height, mt, mb, 'header', cfg.header.padding);
1032
- this.textPdf(headerText, headerPos.x, headerPos.y, headerPos.align);
1145
+ if (typeof cfg.header.content === 'function') {
1146
+ cfg.header.content(this, String(pageNum));
1147
+ }
1148
+ else {
1149
+ headerText = String(cfg.header.content)
1150
+ .replace('${currentPage}', String(pageNum))
1151
+ .replace('${totalPages}', String(this.totalPages));
1152
+ this.jspdfCtx.setFontSize(this.safe(this.pxToPt(cfg.header.contentFontSize), 1));
1153
+ this.setTextColorFromString(cfg.header.contentColor);
1154
+ headerPos = this.computeContentPosition(cfg.header.contentPosition, pageW, pageH, cfg.header.height, mt, mb, 'header', cfg.header.padding);
1155
+ this.textPdf(headerText, headerPos.x, headerPos.y, headerPos.align);
1156
+ }
1033
1157
  }
1034
1158
  if (cfg === null || cfg === void 0 ? void 0 : cfg.footer) {
1035
- footerText = String(cfg.footer.content)
1036
- .replace('${currentPage}', String(pageNum))
1037
- .replace('${totalPages}', String(this.totalPages));
1038
- this.jspdfCtx.setFontSize(this.safe(this.pxToPt(cfg.footer.contentFontSize), 1));
1039
- this.setTextColorFromString(cfg.footer.contentColor);
1040
- footerPos = this.computeContentPosition(cfg.footer.contentPosition, pageW, pageH, cfg.footer.height, mt, mb, 'footer', cfg.footer.padding);
1041
- this.textPdf(footerText, footerPos.x, footerPos.y, footerPos.align);
1159
+ if (typeof cfg.footer.content === 'function') {
1160
+ cfg.footer.content(this, String(pageNum));
1161
+ }
1162
+ else {
1163
+ footerText = String(cfg.footer.content)
1164
+ .replace('${currentPage}', String(pageNum))
1165
+ .replace('${totalPages}', String(this.totalPages));
1166
+ this.jspdfCtx.setFontSize(this.safe(this.pxToPt(cfg.footer.contentFontSize), 1));
1167
+ this.setTextColorFromString(cfg.footer.contentColor);
1168
+ footerPos = this.computeContentPosition(cfg.footer.contentPosition, pageW, pageH, cfg.footer.height, mt, mb, 'footer', cfg.footer.padding);
1169
+ this.textPdf(footerText, footerPos.x, footerPos.y, footerPos.align);
1170
+ }
1042
1171
  }
1043
1172
  return [2 /*return*/];
1044
1173
  }
@@ -1152,7 +1281,8 @@ var CanvasRenderer = /** @class */ (function (_super) {
1152
1281
  var sy = this.safe(y);
1153
1282
  var sw = Math.max(1, this.safe(w, 1));
1154
1283
  var sh = Math.max(1, this.safe(h, 1));
1155
- this.jspdfCtx.addImage(img, format, sx, sy, sw, sh);
1284
+ // draw a big image,jsPDF:Error: Invalid image dimensions: width and height must be positive numbers.
1285
+ this.jspdfCtx.addImage(img, format, sx, sy, sw, sh, '', 'FAST');
1156
1286
  };
1157
1287
  CanvasRenderer.prototype.output = function () {
1158
1288
  return __awaiter(this, void 0, void 0, function () {