Skip to content

Commit

Permalink
enable server side rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
VadimDez committed Nov 23, 2017
1 parent aa0d1eb commit c965b9a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 28 deletions.
2 changes: 0 additions & 2 deletions build/pdf-viewer/pdf-viewer.component.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// <reference types="pdf" />
import { ElementRef, EventEmitter, OnChanges, SimpleChanges, OnInit } from '@angular/core';
import 'pdfjs-dist/web/compatibility';
import 'pdfjs-dist/web/pdf_viewer';
export declare class PdfViewerComponent implements OnChanges, OnInit {
private element;
static CSS_UNITS: number;
Expand Down
25 changes: 18 additions & 7 deletions build/pdf-viewer/pdf-viewer.component.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/pdf-viewer/pdf-viewer.component.js.map

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions dist/pdf-viewer.component.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// <reference types="pdf" />
import { ElementRef, EventEmitter, OnChanges, SimpleChanges, OnInit } from '@angular/core';
import 'pdfjs-dist/web/compatibility';
import 'pdfjs-dist/web/pdf_viewer';
export declare class PdfViewerComponent implements OnChanges, OnInit {
private element;
static CSS_UNITS: number;
Expand Down
27 changes: 20 additions & 7 deletions dist/pdf-viewer.component.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"clean": "gulp clean",
"compile": "gulp compile",
"build": "gulp build",
"start": "npm run build && concurrently --kill-others \"gulp watch\" \"lite-server\""
"start": "npm run build && concurrently --kill-others \"gulp watch\" \"lite-server\"",
"copy": "cp -R ./build/pdf-viewer/ ./dist/"
},
"repository": {
"type": "git",
Expand Down
30 changes: 22 additions & 8 deletions src/pdf-viewer/pdf-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
import {
Component, Input, Output, ElementRef, EventEmitter, OnChanges, SimpleChanges, OnInit, HostListener
} from '@angular/core';
import * as pdfjs from 'pdfjs-dist/build/pdf';
window['pdfjs-dist/build/pdf'] = pdfjs;
import 'pdfjs-dist/web/compatibility';
import 'pdfjs-dist/web/pdf_viewer';

PDFJS.verbosity = (<any>PDFJS).VERBOSITY_LEVELS.errors;
function isSSR() {
return typeof window === 'undefined';
}

if (!isSSR()) {
window['pdfjs-dist/build/pdf'] = require('pdfjs-dist/build/pdf');
require('pdfjs-dist/web/compatibility');
require('pdfjs-dist/web/pdf_viewer');

PDFJS.verbosity = (<any>PDFJS).VERBOSITY_LEVELS.errors;
}


@Component({
selector: 'pdf-viewer',
Expand Down Expand Up @@ -289,12 +296,15 @@ export class PdfViewerComponent implements OnChanges, OnInit {
@Output('on-progress') onProgress = new EventEmitter<PDFProgressData>();

constructor(private element: ElementRef) {
PDFJS.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${ (PDFJS as any).version }/pdf.worker.min.js`;
if (!isSSR()) {
PDFJS.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${ (PDFJS as any).version }/pdf.worker.min.js`;
}
}


ngOnInit() {
this.setupViewer();
if (!isSSR()) {
this.setupViewer();
}
}

@HostListener('window:resize', [])
Expand All @@ -313,6 +323,10 @@ export class PdfViewerComponent implements OnChanges, OnInit {
}

ngOnChanges(changes: SimpleChanges) {
if (isSSR()) {
return;
}

if ('src' in changes) {
this.loadPDF();
} else if (this._pdf) {
Expand Down

0 comments on commit c965b9a

Please sign in to comment.