Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
34 changes: 23 additions & 11 deletions src/plugin/hyperlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,43 @@ var orig = {
};

Worker.prototype.toContainer = function toContainer() {
var self = this;
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;

function storeLinkInfo(container, externalPage) {
var links = container.querySelectorAll('a');
var containerRect = unitConvert(container.getBoundingClientRect(), this.prop.pageSize.k);
linkInfo = [];
var containerRect = unitConvert(container.getBoundingClientRect(), self.prop.pageSize.k);

// Loop through each anchor tag.
Array.prototype.forEach.call(links, function(link) {
// Treat each client rect as a separate link (for text-wrapping).
// Treat each client rect as a separ nmate 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);
var clientRect = unitConvert(clientRects[i], self.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;
var page = externalPage || (Math.floor(clientRect.top / self.prop.pageSize.inner.height) + 1);
var top = self.opt.margin[0] + clientRect.top % self.prop.pageSize.inner.height;
var left = self.opt.margin[1] + clientRect.left;

linkInfo.push({ page, top, left, clientRect, link });
}
}, this);
});
}

if (self.opt.enableLinks) {
// Find all anchor tags and get the container's bounds for reference.
linkInfo = [];

if (Array.isArray(self.opt.enableLinks)) {
self.opt.enableLinks.forEach(function({id, page}) {
storeLinkInfo(document.getElementById(id), page);
});
} else {
storeLinkInfo(self.prop.container);
}
}
});
};
Expand Down
19 changes: 16 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Worker.template = {
},
opt: {
filename: 'file.pdf',
trimPages: false,
margin: [0,0,0,0],
image: { type: 'jpeg', quality: 0.95 },
enableLinks: true,
Expand Down Expand Up @@ -212,10 +213,22 @@ Worker.prototype.toPdf = function toPdf() {
pageCtx.drawImage(canvas, 0, page*pxPageHeight, w, h, 0, 0, w, h);

// Add the page to the PDF.
if (page) this.prop.pdf.addPage();
var pageWidth = this.prop.pageSize.inner.width;
const [mTop, mLeft, mBottom, mRight] = opt.margin || [];
const adjustedPageWidth = pageWidth + mLeft + mRight;
const adjustedPageHeight = pageHeight + mTop + mBottom;

this.prop.pdf.addPage(
...(opt.trimPages ? [[adjustedPageWidth, adjustedPageHeight], adjustedPageWidth > adjustedPageHeight ? 'l' : 'p'] : [])
);

var imgData = pageCanvas.toDataURL('image/' + opt.image.type, opt.image.quality);
this.prop.pdf.addImage(imgData, opt.image.type, opt.margin[1], opt.margin[0],
this.prop.pageSize.inner.width, pageHeight);
this.prop.pdf.addImage(imgData, opt.image.type, mLeft, mTop, pageWidth, pageHeight);
}

if (!this.prop._firstPageDeleted) {
this.prop.pdf.deletePage(1);
this.prop._firstPageDeleted = true;
}
});
};
Expand Down
2 changes: 1 addition & 1 deletion type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module "html2pdf.js" {
type?: "jpeg" | "png" | "webp";
quality?: number;
};
enableLinks?: boolean;
enableLinks?: boolean | { id: string, page: number }[];
html2canvas?: object;
jsPDF?: {
unit?: string;
Expand Down