1- import { Component , Input , Output , ViewChild , EventEmitter , ElementRef } from '@angular/core' ;
1+ import { Component , Input , Output , OnInit , ViewChild , EventEmitter , ElementRef } from '@angular/core' ;
22
33@Component ( {
44 selector : 'ng2-pdfjs-viewer' ,
55 template : `<iframe title="ng2-pdfjs-viewer" [hidden]="externalWindow || (!externalWindow && !pdfSrc)" #iframe width="100%" height="100%"></iframe>`
66} )
7- export class PdfJsViewerComponent {
7+ export class PdfJsViewerComponent implements OnInit {
88 @ViewChild ( 'iframe' , { static : true } ) iframe : ElementRef ;
99 @Input ( ) public viewerId : string ;
1010 @Output ( ) onBeforePrint : EventEmitter < any > = new EventEmitter ( ) ;
@@ -40,12 +40,12 @@ export class PdfJsViewerComponent {
4040 @Input ( ) public errorAppend : boolean = true ;
4141 @Input ( ) public errorMessage : string ;
4242 @Input ( ) public diagnosticLogs : boolean = true ;
43-
43+
4444 @Input ( ) public externalWindowOptions : string ;
4545 public viewerTab : any ;
4646 private _src : string | Blob | Uint8Array ;
4747 private _page : number ;
48-
48+
4949 @Input ( )
5050 public set page ( _page : number ) {
5151 this . _page = _page ;
@@ -134,6 +134,7 @@ export class PdfJsViewerComponent {
134134 this . loadPdf ( ) ;
135135 }
136136
137+ private relaseUrl ?: ( ) => void ; // Avoid memory leask with `URL.createObjectURL`
137138 private loadPdf ( ) {
138139 if ( ! this . _src ) {
139140 return ;
@@ -179,15 +180,20 @@ export class PdfJsViewerComponent {
179180 }
180181 }
181182
183+ this . relaseUrl ?.( ) ;
182184 let fileUrl ;
183185 //if (typeof this.src === "string") {
184186 // fileUrl = this.src;
185187 //}
186188 if ( this . _src instanceof Blob ) {
187- fileUrl = encodeURIComponent ( URL . createObjectURL ( this . _src ) ) ;
189+ const url = URL . createObjectURL ( this . _src ) ;
190+ fileUrl = encodeURIComponent ( url ) ;
191+ this . relaseUrl = ( ) => URL . revokeObjectURL ( url ) ;
188192 } else if ( this . _src instanceof Uint8Array ) {
189193 let blob = new Blob ( [ this . _src ] , { type : "application/pdf" } ) ;
190- fileUrl = encodeURIComponent ( URL . createObjectURL ( blob ) ) ;
194+ const url = createObjectURL ( blob ) ;
195+ this . relaseUrl = ( ) => URL . revokeObjectURL ( url ) ;
196+ fileUrl = encodeURIComponent ( url ) ;
191197 } else {
192198 fileUrl = this . _src ;
193199 }
@@ -274,7 +280,7 @@ export class PdfJsViewerComponent {
274280 if ( this . useOnlyCssZoom ) {
275281 viewerUrl += `&useOnlyCssZoom=${ this . useOnlyCssZoom } ` ;
276282 }
277-
283+
278284 if ( this . _page || this . zoom || this . nameddest || this . pagemode ) viewerUrl += "#"
279285 if ( this . _page ) {
280286 viewerUrl += `&page=${ this . _page } ` ;
@@ -334,4 +340,4 @@ export class PdfJsViewerComponent {
334340 // pagemode = ${this.errorMessage}
335341 // `);
336342 }
337- }
343+ }
0 commit comments