This commit is contained in:
David Štaleker
2025-07-18 05:33:16 +02:00
parent 401a367e5d
commit db0cc8d3de
14776 changed files with 9251484 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
import Worker from '../worker.js';
import { unitConvert } from '../utils.js';
// Add hyperlink functionality to the PDF creation.
// Main link array, and refs to original functions.
var linkInfo = [];
var orig = {
toContainer: Worker.prototype.toContainer,
toPdf: Worker.prototype.toPdf,
};
Worker.prototype.toContainer = function toContainer() {
return orig.toContainer.call(this).then(function toContainer_hyperlink() {
// Retrieve hyperlink info if the option is enabled.
if (this.opt.enableLinks) {
// Find all anchor tags and get the container's bounds for reference.
var container = this.prop.container;
var links = container.querySelectorAll('a');
var containerRect = unitConvert(container.getBoundingClientRect(), this.prop.pageSize.k);
linkInfo = [];
// Loop through each anchor tag.
Array.prototype.forEach.call(links, function(link) {
// Treat each client rect as a separate link (for text-wrapping).
var clientRects = link.getClientRects();
for (var i=0; i<clientRects.length; i++) {
var clientRect = unitConvert(clientRects[i], this.prop.pageSize.k);
clientRect.left -= containerRect.left;
clientRect.top -= containerRect.top;
var page = Math.floor(clientRect.top / this.prop.pageSize.inner.height) + 1;
var top = this.opt.margin[0] + clientRect.top % this.prop.pageSize.inner.height;
var left = this.opt.margin[1] + clientRect.left;
linkInfo.push({ page, top, left, clientRect, link });
}
}, this);
}
});
};
Worker.prototype.toPdf = function toPdf() {
return orig.toPdf.call(this).then(function toPdf_hyperlink() {
// Add hyperlinks if the option is enabled.
if (this.opt.enableLinks) {
// Attach each anchor tag based on info from toContainer().
linkInfo.forEach(function(l) {
this.prop.pdf.setPage(l.page);
this.prop.pdf.link(l.left, l.top, l.clientRect.width, l.clientRect.height,
{ url: l.link.href });
}, this);
// Reset the active page of the PDF to the final page.
var nPages = this.prop.pdf.internal.getNumberOfPages();
this.prop.pdf.setPage(nPages);
}
});
};

View File

@@ -0,0 +1,99 @@
// Import dependencies.
import { jsPDF } from 'jspdf';
// Get dimensions of a PDF page, as determined by jsPDF.
jsPDF.getPageSize = function(orientation, unit, format) {
// Decode options object
if (typeof orientation === 'object') {
var options = orientation;
orientation = options.orientation;
unit = options.unit || unit;
format = options.format || format;
}
// Default options
unit = unit || 'mm';
format = format || 'a4';
orientation = ('' + (orientation || 'P')).toLowerCase();
var format_as_string = ('' + format).toLowerCase();
// Size in pt of various paper formats
var pageFormats = {
'a0' : [2383.94, 3370.39], 'a1' : [1683.78, 2383.94],
'a2' : [1190.55, 1683.78], 'a3' : [ 841.89, 1190.55],
'a4' : [ 595.28, 841.89], 'a5' : [ 419.53, 595.28],
'a6' : [ 297.64, 419.53], 'a7' : [ 209.76, 297.64],
'a8' : [ 147.40, 209.76], 'a9' : [ 104.88, 147.40],
'a10' : [ 73.70, 104.88], 'b0' : [2834.65, 4008.19],
'b1' : [2004.09, 2834.65], 'b2' : [1417.32, 2004.09],
'b3' : [1000.63, 1417.32], 'b4' : [ 708.66, 1000.63],
'b5' : [ 498.90, 708.66], 'b6' : [ 354.33, 498.90],
'b7' : [ 249.45, 354.33], 'b8' : [ 175.75, 249.45],
'b9' : [ 124.72, 175.75], 'b10' : [ 87.87, 124.72],
'c0' : [2599.37, 3676.54], 'c1' : [1836.85, 2599.37],
'c2' : [1298.27, 1836.85], 'c3' : [ 918.43, 1298.27],
'c4' : [ 649.13, 918.43], 'c5' : [ 459.21, 649.13],
'c6' : [ 323.15, 459.21], 'c7' : [ 229.61, 323.15],
'c8' : [ 161.57, 229.61], 'c9' : [ 113.39, 161.57],
'c10' : [ 79.37, 113.39], 'dl' : [ 311.81, 623.62],
'letter' : [612, 792],
'government-letter' : [576, 756],
'legal' : [612, 1008],
'junior-legal' : [576, 360],
'ledger' : [1224, 792],
'tabloid' : [792, 1224],
'credit-card' : [153, 243]
};
// Unit conversion
switch (unit) {
case 'pt': var k = 1; break;
case 'mm': var k = 72 / 25.4; break;
case 'cm': var k = 72 / 2.54; break;
case 'in': var k = 72; break;
case 'px': var k = 72 / 96; break;
case 'pc': var k = 12; break;
case 'em': var k = 12; break;
case 'ex': var k = 6; break;
default:
throw ('Invalid unit: ' + unit);
}
// Dimensions are stored as user units and converted to points on output
if (pageFormats.hasOwnProperty(format_as_string)) {
var pageHeight = pageFormats[format_as_string][1] / k;
var pageWidth = pageFormats[format_as_string][0] / k;
} else {
try {
var pageHeight = format[1];
var pageWidth = format[0];
} catch (err) {
throw new Error('Invalid format: ' + format);
}
}
// Handle page orientation
if (orientation === 'p' || orientation === 'portrait') {
orientation = 'p';
if (pageWidth > pageHeight) {
var tmp = pageWidth;
pageWidth = pageHeight;
pageHeight = tmp;
}
} else if (orientation === 'l' || orientation === 'landscape') {
orientation = 'l';
if (pageHeight > pageWidth) {
var tmp = pageWidth;
pageWidth = pageHeight;
pageHeight = tmp;
}
} else {
throw('Invalid orientation: ' + orientation);
}
// Return information (k is the unit conversion ratio from pts)
var info = { 'width': pageWidth, 'height': pageHeight, 'unit': unit, 'k': k };
return info;
};
export default jsPDF;

View File

@@ -0,0 +1,134 @@
import Worker from '../worker.js';
import { objType, createElement } from '../utils.js';
/* Pagebreak plugin:
Adds page-break functionality to the html2pdf library. Page-breaks can be
enabled by CSS styles, set on individual elements using selectors, or
avoided from breaking inside all elements.
Options on the `opt.pagebreak` object:
mode: String or array of strings: 'avoid-all', 'css', and/or 'legacy'
Default: ['css', 'legacy']
before: String or array of CSS selectors for which to add page-breaks
before each element. Can be a specific element with an ID
('#myID'), all elements of a type (e.g. 'img'), all of a class
('.myClass'), or even '*' to match every element.
after: Like 'before', but adds a page-break immediately after the element.
avoid: Like 'before', but avoids page-breaks on these elements. You can
enable this feature on every element using the 'avoid-all' mode.
*/
// Refs to original functions.
var orig = {
toContainer: Worker.prototype.toContainer
};
// Add pagebreak default options to the Worker template.
Worker.template.opt.pagebreak = {
mode: ['css', 'legacy'],
before: [],
after: [],
avoid: []
};
Worker.prototype.toContainer = function toContainer() {
return orig.toContainer.call(this).then(function toContainer_pagebreak() {
// Setup root element and inner page height.
var root = this.prop.container;
var pxPageHeight = this.prop.pageSize.inner.px.height;
// Check all requested modes.
var modeSrc = [].concat(this.opt.pagebreak.mode);
var mode = {
avoidAll: modeSrc.indexOf('avoid-all') !== -1,
css: modeSrc.indexOf('css') !== -1,
legacy: modeSrc.indexOf('legacy') !== -1
};
// Get arrays of all explicitly requested elements.
var select = {};
var self = this;
['before', 'after', 'avoid'].forEach(function(key) {
var all = mode.avoidAll && key === 'avoid';
select[key] = all ? [] : [].concat(self.opt.pagebreak[key] || []);
if (select[key].length > 0) {
select[key] = Array.prototype.slice.call(
root.querySelectorAll(select[key].join(', ')));
}
});
// Get all legacy page-break elements.
var legacyEls = root.querySelectorAll('.html2pdf__page-break');
legacyEls = Array.prototype.slice.call(legacyEls);
// Loop through all elements.
var els = root.querySelectorAll('*');
Array.prototype.forEach.call(els, function pagebreak_loop(el) {
// Setup pagebreak rules based on legacy and avoidAll modes.
var rules = {
before: false,
after: mode.legacy && legacyEls.indexOf(el) !== -1,
avoid: mode.avoidAll
};
// Add rules for css mode.
if (mode.css) {
// TODO: Check if this is valid with iFrames.
var style = window.getComputedStyle(el);
// TODO: Handle 'left' and 'right' correctly.
// TODO: Add support for 'avoid' on breakBefore/After.
var breakOpt = ['always', 'page', 'left', 'right'];
var avoidOpt = ['avoid', 'avoid-page'];
rules = {
before: rules.before || breakOpt.indexOf(style.breakBefore || style.pageBreakBefore) !== -1,
after: rules.after || breakOpt.indexOf(style.breakAfter || style.pageBreakAfter) !== -1,
avoid: rules.avoid || avoidOpt.indexOf(style.breakInside || style.pageBreakInside) !== -1
};
}
// Add rules for explicit requests.
Object.keys(rules).forEach(function(key) {
rules[key] = rules[key] || select[key].indexOf(el) !== -1;
});
// Get element position on the screen.
// TODO: Subtract the top of the container from clientRect.top/bottom?
var clientRect = el.getBoundingClientRect();
// Avoid: Check if a break happens mid-element.
if (rules.avoid && !rules.before) {
var startPage = Math.floor(clientRect.top / pxPageHeight);
var endPage = Math.floor(clientRect.bottom / pxPageHeight);
var nPages = Math.abs(clientRect.bottom - clientRect.top) / pxPageHeight;
// Turn on rules.before if the el is broken and is at most one page long.
if (endPage !== startPage && nPages <= 1) {
rules.before = true;
}
}
// Before: Create a padding div to push the element to the next page.
if (rules.before) {
var pad = createElement('div', {style: {
display: 'block',
height: pxPageHeight - (clientRect.top % pxPageHeight) + 'px'
}});
el.parentNode.insertBefore(pad, el);
}
// After: Create a padding div to fill the remaining page.
if (rules.after) {
var pad = createElement('div', {style: {
display: 'block',
height: pxPageHeight - (clientRect.bottom % pxPageHeight) + 'px'
}});
el.parentNode.insertBefore(pad, el.nextSibling);
}
});
});
};