ng2-pdfjs-viewer
Version:
<p align="center"> <a href="https://www.npmjs.com/package/ng2-pdfjs-viewer"> <img src="https://img.shields.io/npm/dm/ng2-pdfjs-viewer.svg?style=flat" alt="downloads"> </a> <a href="https://badge.fury.io/js/ng2-pdfjs-viewer"> <img src="h
444 lines (439 loc) • 16.9 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, Component, ViewChild, Input, Output, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
class PdfJsViewerComponent {
iframe;
static lastID = 0;
viewerId = `ng2-pdfjs-viewer-ID${++PdfJsViewerComponent.lastID}`;
onBeforePrint = new EventEmitter();
onAfterPrint = new EventEmitter();
onDocumentLoad = new EventEmitter();
onPageChange = new EventEmitter();
viewerFolder;
externalWindow = false;
target = '_blank';
showSpinner = true;
downloadFileName;
openFile = true;
download = true;
startDownload;
viewBookmark = true;
print = true;
startPrint;
fullScreen = true;
//@Input() public showFullScreen: boolean;
find = true;
zoom;
nameddest;
pagemode;
lastPage;
rotatecw;
rotateccw;
cursor;
scroll;
spread;
locale;
useOnlyCssZoom = false;
errorOverride = false;
errorAppend = true;
errorMessage;
diagnosticLogs = true;
externalWindowOptions;
viewerTab;
_src;
_page;
set page(_page) {
this._page = _page;
if (this.PDFViewerApplication) {
this.PDFViewerApplication.page = this._page;
}
else {
if (this.diagnosticLogs)
console.warn("Document is not loaded yet!!!. Try to set page# after full load. Ignore this warning if you are not setting page# using '.' notation. (E.g. pdfViewer.page = 5;)");
}
}
get page() {
if (this.PDFViewerApplication) {
return this.PDFViewerApplication.page;
}
else {
if (this.diagnosticLogs)
console.warn("Document is not loaded yet!!!. Try to retrieve page# after full load.");
}
}
set pdfSrc(_src) {
this._src = _src;
}
get pdfSrc() {
return this._src;
}
get PDFViewerApplicationOptions() {
let pdfViewerOptions = null;
if (this.externalWindow) {
if (this.viewerTab) {
pdfViewerOptions = this.viewerTab.PDFViewerApplicationOptions;
}
}
else {
if (this.iframe.nativeElement.contentWindow) {
pdfViewerOptions = this.iframe.nativeElement.contentWindow.PDFViewerApplicationOptions;
}
}
return pdfViewerOptions;
}
get PDFViewerApplication() {
let pdfViewer = null;
if (this.externalWindow) {
if (this.viewerTab) {
pdfViewer = this.viewerTab.PDFViewerApplication;
}
}
else {
if (this.iframe.nativeElement.contentWindow) {
pdfViewer = this.iframe.nativeElement.contentWindow.PDFViewerApplication;
}
}
return pdfViewer;
}
receiveMessage(viewerEvent) {
if (viewerEvent.data && viewerEvent.data.viewerId && viewerEvent.data.event) {
let viewerId = viewerEvent.data.viewerId;
let event = viewerEvent.data.event;
let param = viewerEvent.data.param;
if (this.viewerId == viewerId) {
if (this.onBeforePrint && event == "beforePrint") {
this.onBeforePrint.emit();
}
else if (this.onAfterPrint && event == "afterPrint") {
this.onAfterPrint.emit();
}
else if (this.onDocumentLoad && event == "pagesLoaded") {
this.onDocumentLoad.emit(param);
}
else if (this.onPageChange && event == "pageChange") {
this.onPageChange.emit(param);
}
}
}
}
ngOnInit() {
window.addEventListener("message", this.receiveMessage.bind(this), false);
if (!this.externalWindow) { // Load pdf for embedded views
this.loadPdf();
}
}
refresh() {
this.loadPdf();
}
relaseUrl; // Avoid memory leask with `URL.createObjectURL`
loadPdf() {
if (!this._src) {
return;
}
// console.log(`Tab is - ${this.viewerTab}`);
// if (this.viewerTab) {
// console.log(`Status of window - ${this.viewerTab.closed}`);
// }
if (this.externalWindow && (typeof this.viewerTab === 'undefined' || this.viewerTab.closed)) {
this.viewerTab = window.open('', this.target, this.externalWindowOptions || '');
if (this.viewerTab == null) {
if (this.diagnosticLogs)
console.error("ng2-pdfjs-viewer: For 'externalWindow = true'. i.e opening in new tab to work, pop-ups should be enabled.");
return;
}
if (this.showSpinner) {
this.viewerTab.document.write(`
<style>
.loader {
position: fixed;
left: 40%;
top: 40%;
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}
spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
<div class="loader"></div>
`);
}
}
this.relaseUrl?.();
let fileUrl;
//if (typeof this.src === "string") {
// fileUrl = this.src;
//}
if (this._src instanceof Blob) {
const url = URL.createObjectURL(this._src);
fileUrl = encodeURIComponent(url);
this.relaseUrl = () => URL.revokeObjectURL(url);
}
else if (this._src instanceof Uint8Array) {
let blob = new Blob([this._src], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
this.relaseUrl = () => URL.revokeObjectURL(url);
fileUrl = encodeURIComponent(url);
}
else {
fileUrl = this._src;
}
let viewerUrl;
if (this.viewerFolder) {
viewerUrl = `${this.viewerFolder}/web/viewer.html`;
}
else {
viewerUrl = `assets/pdfjs/web/viewer.html`;
}
viewerUrl += `?file=${fileUrl}`;
if (typeof this.viewerId !== 'undefined') {
viewerUrl += `&viewerId=${this.viewerId}`;
}
if (typeof this.onBeforePrint !== 'undefined') {
viewerUrl += `&beforePrint=true`;
}
if (typeof this.onAfterPrint !== 'undefined') {
viewerUrl += `&afterPrint=true`;
}
if (typeof this.onDocumentLoad !== 'undefined') {
viewerUrl += `&pagesLoaded=true`;
}
if (typeof this.onPageChange !== 'undefined') {
viewerUrl += `&pageChange=true`;
}
if (this.downloadFileName) {
if (!this.downloadFileName.endsWith(".pdf")) {
this.downloadFileName += ".pdf";
}
viewerUrl += `&fileName=${this.downloadFileName}`;
}
if (typeof this.openFile !== 'undefined') {
viewerUrl += `&openFile=${this.openFile}`;
}
if (typeof this.download !== 'undefined') {
viewerUrl += `&download=${this.download}`;
}
if (this.startDownload) {
viewerUrl += `&startDownload=${this.startDownload}`;
}
if (typeof this.viewBookmark !== 'undefined') {
viewerUrl += `&viewBookmark=${this.viewBookmark}`;
}
if (typeof this.print !== 'undefined') {
viewerUrl += `&print=${this.print}`;
}
if (this.startPrint) {
viewerUrl += `&startPrint=${this.startPrint}`;
}
if (typeof this.fullScreen !== 'undefined') {
viewerUrl += `&fullScreen=${this.fullScreen}`;
}
// if (this.showFullScreen) {
// viewerUrl += `&showFullScreen=${this.showFullScreen}`;
// }
if (typeof this.find !== 'undefined') {
viewerUrl += `&find=${this.find}`;
}
if (this.lastPage) {
viewerUrl += `&lastpage=${this.lastPage}`;
}
if (this.rotatecw) {
viewerUrl += `&rotatecw=${this.rotatecw}`;
}
if (this.rotateccw) {
viewerUrl += `&rotateccw=${this.rotateccw}`;
}
if (this.cursor) {
viewerUrl += `&cursor=${this.cursor}`;
}
if (this.scroll) {
viewerUrl += `&scroll=${this.scroll}`;
}
if (this.spread) {
viewerUrl += `&spread=${this.spread}`;
}
if (this.locale) {
viewerUrl += `&locale=${this.locale}`;
}
if (this.useOnlyCssZoom) {
viewerUrl += `&useOnlyCssZoom=${this.useOnlyCssZoom}`;
}
if (this._page || this.zoom || this.nameddest || this.pagemode)
viewerUrl += "#";
if (this._page) {
viewerUrl += `&page=${this._page}`;
}
if (this.zoom) {
viewerUrl += `&zoom=${this.zoom}`;
}
if (this.nameddest) {
viewerUrl += `&nameddest=${this.nameddest}`;
}
if (this.pagemode) {
viewerUrl += `&pagemode=${this.pagemode}`;
}
if (this.errorOverride || this.errorAppend) {
viewerUrl += `&errorMessage=${this.errorMessage}`;
if (this.errorOverride) {
viewerUrl += `&errorOverride=${this.errorOverride}`;
}
if (this.errorAppend) {
viewerUrl += `&errorAppend=${this.errorAppend}`;
}
}
if (this.externalWindow) {
this.viewerTab.location.href = viewerUrl;
}
else {
this.iframe.nativeElement.contentWindow ? this.iframe.nativeElement.contentWindow.location.replace(viewerUrl) : this.iframe.nativeElement.src = viewerUrl;
}
// console.log(`
// pdfSrc = ${this.pdfSrc}
// fileUrl = ${fileUrl}
// externalWindow = ${this.externalWindow}
// downloadFileName = ${this.downloadFileName}
// viewerFolder = ${this.viewerFolder}
// openFile = ${this.openFile}
// download = ${this.download}
// startDownload = ${this.startDownload}
// viewBookmark = ${this.viewBookmark}
// print = ${this.print}
// startPrint = ${this.startPrint}
// fullScreen = ${this.fullScreen}
// find = ${this.find}
// lastPage = ${this.lastPage}
// rotatecw = ${this.rotatecw}
// rotateccw = ${this.rotateccw}
// cursor = ${this.cursor}
// scrollMode = ${this.scroll}
// spread = ${this.spread}
// page = ${this.page}
// zoom = ${this.zoom}
// nameddest = ${this.nameddest}
// pagemode = ${this.pagemode}
// pagemode = ${this.errorOverride}
// pagemode = ${this.errorAppend}
// pagemode = ${this.errorMessage}
// `);
}
ngOnDestroy() {
this.relaseUrl?.();
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.1", ngImport: i0, type: PdfJsViewerComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.1.1", type: PdfJsViewerComponent, isStandalone: false, selector: "ng2-pdfjs-viewer", inputs: { viewerId: "viewerId", viewerFolder: "viewerFolder", externalWindow: "externalWindow", target: "target", showSpinner: "showSpinner", downloadFileName: "downloadFileName", openFile: "openFile", download: "download", startDownload: "startDownload", viewBookmark: "viewBookmark", print: "print", startPrint: "startPrint", fullScreen: "fullScreen", find: "find", zoom: "zoom", nameddest: "nameddest", pagemode: "pagemode", lastPage: "lastPage", rotatecw: "rotatecw", rotateccw: "rotateccw", cursor: "cursor", scroll: "scroll", spread: "spread", locale: "locale", useOnlyCssZoom: "useOnlyCssZoom", errorOverride: "errorOverride", errorAppend: "errorAppend", errorMessage: "errorMessage", diagnosticLogs: "diagnosticLogs", externalWindowOptions: "externalWindowOptions", page: "page", pdfSrc: "pdfSrc" }, outputs: { onBeforePrint: "onBeforePrint", onAfterPrint: "onAfterPrint", onDocumentLoad: "onDocumentLoad", onPageChange: "onPageChange" }, viewQueries: [{ propertyName: "iframe", first: true, predicate: ["iframe"], descendants: true, static: true }], ngImport: i0, template: `<iframe title="ng2-pdfjs-viewer" [hidden]="externalWindow || (!externalWindow && !pdfSrc)" #iframe width="100%" height="100%"></iframe>`, isInline: true });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.1", ngImport: i0, type: PdfJsViewerComponent, decorators: [{
type: Component,
args: [{
selector: 'ng2-pdfjs-viewer',
standalone: false,
template: `<iframe title="ng2-pdfjs-viewer" [hidden]="externalWindow || (!externalWindow && !pdfSrc)" #iframe width="100%" height="100%"></iframe>`
}]
}], propDecorators: { iframe: [{
type: ViewChild,
args: ['iframe', { static: true }]
}], viewerId: [{
type: Input
}], onBeforePrint: [{
type: Output
}], onAfterPrint: [{
type: Output
}], onDocumentLoad: [{
type: Output
}], onPageChange: [{
type: Output
}], viewerFolder: [{
type: Input
}], externalWindow: [{
type: Input
}], target: [{
type: Input
}], showSpinner: [{
type: Input
}], downloadFileName: [{
type: Input
}], openFile: [{
type: Input
}], download: [{
type: Input
}], startDownload: [{
type: Input
}], viewBookmark: [{
type: Input
}], print: [{
type: Input
}], startPrint: [{
type: Input
}], fullScreen: [{
type: Input
}], find: [{
type: Input
}], zoom: [{
type: Input
}], nameddest: [{
type: Input
}], pagemode: [{
type: Input
}], lastPage: [{
type: Input
}], rotatecw: [{
type: Input
}], rotateccw: [{
type: Input
}], cursor: [{
type: Input
}], scroll: [{
type: Input
}], spread: [{
type: Input
}], locale: [{
type: Input
}], useOnlyCssZoom: [{
type: Input
}], errorOverride: [{
type: Input
}], errorAppend: [{
type: Input
}], errorMessage: [{
type: Input
}], diagnosticLogs: [{
type: Input
}], externalWindowOptions: [{
type: Input
}], page: [{
type: Input
}], pdfSrc: [{
type: Input
}] } });
class PdfJsViewerModule {
static forRoot() {
return {
ngModule: PdfJsViewerModule,
};
}
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.1", ngImport: i0, type: PdfJsViewerModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.1.1", ngImport: i0, type: PdfJsViewerModule, declarations: [PdfJsViewerComponent], imports: [CommonModule], exports: [PdfJsViewerComponent] });
static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.1.1", ngImport: i0, type: PdfJsViewerModule, imports: [CommonModule] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.1", ngImport: i0, type: PdfJsViewerModule, decorators: [{
type: NgModule,
args: [{
imports: [CommonModule],
declarations: [PdfJsViewerComponent],
exports: [PdfJsViewerComponent],
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { PdfJsViewerComponent, PdfJsViewerModule };
//# sourceMappingURL=ng2-pdfjs-viewer.mjs.map