11import Worker from '../worker.js' ;
22import { unitConvert } from '../utils.js' ;
33
4+ // Add hyperlink functionality to the PDF creation.
5+
6+ // Main link array, and refs to original functions.
7+ var linkInfo = [ ] ;
48var orig = {
9+ toContainer : Worker . prototype . toContainer ,
510 toPdf : Worker . prototype . toPdf ,
611} ;
712
8- Worker . prototype . toPdf = function toPdf ( ) {
9- // Add hyperlink functionality to the PDF creation.
10- return orig . toPdf . call ( this ) . then ( function ( ) {
11- // Add hyperlinks if the option is enabled.
13+ Worker . prototype . toContainer = function toContainer ( ) {
14+ return orig . toContainer . call ( this ) . then ( function toContainer_hyperlink ( ) {
15+ // Retrieve hyperlink info if the option is enabled.
1216 if ( this . opt . enableLinks ) {
1317 // Find all anchor tags and get the container's bounds for reference.
1418 var container = this . prop . container ;
1519 var links = container . querySelectorAll ( 'a' ) ;
1620 var containerRect = unitConvert ( container . getBoundingClientRect ( ) , this . prop . pageSize . k ) ;
21+ linkInfo = [ ] ;
1722
1823 // Loop through each anchor tag.
1924 Array . prototype . forEach . call ( links , function ( link ) {
@@ -28,13 +33,26 @@ Worker.prototype.toPdf = function toPdf() {
2833 var top = this . opt . margin [ 0 ] + clientRect . top % this . prop . pageSize . inner . height ;
2934 var left = this . opt . margin [ 1 ] + clientRect . left ;
3035
31- this . prop . pdf . setPage ( page ) ;
32- this . prop . pdf . link ( left , top , clientRect . width , clientRect . height , { url : link . href } ) ;
36+ linkInfo . push ( { page, top, left, clientRect, link } ) ;
3337 }
34- } ) ;
38+ } , this ) ;
39+ }
40+ } ) ;
41+ } ;
42+
43+ Worker . prototype . toPdf = function toPdf ( ) {
44+ return orig . toPdf . call ( this ) . then ( function toPdf_hyperlink ( ) {
45+ // Add hyperlinks if the option is enabled.
46+ if ( this . opt . enableLinks ) {
47+ // Attach each anchor tag based on info from toContainer().
48+ linkInfo . forEach ( function ( l ) {
49+ this . prop . pdf . setPage ( l . page ) ;
50+ this . prop . pdf . link ( l . left , l . top , l . clientRect . width , l . clientRect . height ,
51+ { url : l . link . href } ) ;
52+ } , this ) ;
3553
3654 // Reset the active page of the PDF to the final page.
37- var nPages = this . prop . pdf . putTotalPages ( ) . internal . getNumberOfPages ( ) ;
55+ var nPages = this . prop . pdf . internal . getNumberOfPages ( ) ;
3856 this . prop . pdf . setPage ( nPages ) ;
3957 }
4058 } ) ;
0 commit comments