question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using pdf_viewer with Angular CLI (pseudo-webpack)

See original GitHub issue

Hello,

I am developping an Angular 2+ application with angular-cli (so not a full Webpack) and pdfjs-dist.

I would like to use the pdf_viewer.js component as demonstrated in this example: https://github.com/mozilla/pdf.js/blob/master/examples/components/simpleviewer.js

Successful use of the core

I successfully used pdfjs-dist alone to render pages “manually” (without the PDFViewer component), like this:

import { Component, OnInit } from '@angular/core';

var PDFJS = require('pdfjs-dist');

@Component({/* ... */})
export class PdfViewerComponent implements OnInit{
	
	ngOnInit() {
		PDFJS.workerSrc = "../node_modules/pdfjs-dist/build/pdf.worker.min.js";
		var pagesWrapper = document.getElementById('ghsPV-pages-wrapper');
	}

	private renderPdf(pdfString: string) {
		
		PDFJS.getDocument({ data: pdfString }).then(pdf => {

			var pagesWrapper = document.getElementById('ghsPV-pages-wrapper');

			for (let p = 1; p <= pdf.numPages ; p++) {
				pdf.getPage(p).then(page => {

					var svgHolder = document.createElement('div');
					svgHolder.classList.add('ghsPV-svgHolder');
					svgHolder.style.backgroundColor = 'white';
					svgHolder.style.margin = '1em';

					var viewport = page.getViewport(1);
					svgHolder.style.width = viewport.width + 'px';
					svgHolder.style.height = viewport.height + 'px';
					
					page.getOperatorList().then(opList => {
						var svgGfx = new PDFJS.SVGGraphics(page.commonObjs, page.objs);
						svgGfx.getSVG(opList, viewport).then(svg => {
							pagesWrapper.appendChild(svgHolder);
							svgHolder.appendChild(svg);
						});
					});
				});
			}
		});
	}
}

First fail of importing both scripts

However, I didn’t manage to use the pdf_viewer correctly, this is my last try:

import { Component, OnInit } from '@angular/core';

require('pdfjs-dist');
var PDFJS = require('pdfjs-dist/web/pdf_viewer.js').PDFJS;

@Component({/* ... */})
export class PdfViewerComponent implements OnInit {

	private pagesWrapper: HTMLElement;
	private pdfViewer: any;
	private pdfLinkService: any;

	ngOnInit() {

		PDFJS.workerSrc = "../node_modules/pdfjs-dist/build/pdf.worker.min.js";

		this.pagesWrapper = document.getElementById('ghsPV-pages-wrapper');

		// Hyperlinks:
		this.pdfLinkService = new PDFJS.PDFLinkService();
		this.pdfViewer = new PDFJS.PDFViewer({
			container: this.pagesWrapper,
			linkService: this.pdfLinkService,
		});
		this.pdfLinkService.setViewer(this.pdfViewer);

		// Scale :
		this.pagesWrapper.addEventListener('pagesinit', function () {
			this.pdfViewer.currentScaleValue = 'page-width';
		});
	}
	
	private renderPdf(pdfString: string) {
		PDFJS.getDocument({ data: pdfString }).then(pdf => {
			this.pdfViewer.setDocument(pdfString);
			this.pdfLinkService.setDocument(pdfString, null);
		});
	}
}

Here, I have this error message, probably because my PDFJS variable lacks the core features:

ERROR Error: Uncaught (in promise): TypeError: pdfDocument.getPage is not a function
TypeError: pdfDocument.getPage is not a function
    at PDFViewer.setDocument (pdf_viewer.js:2470)
    at eval (ghs-pdf-viewer.component.ts:123)
    at ZoneDelegate.invoke (zone.js:392)
    at Object.onInvoke (core.js:4629)
    at ZoneDelegate.invoke (zone.js:391)
    at Zone.run (zone.js:142)
    at eval (zone.js:873)
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.js:4620)
    at ZoneDelegate.invokeTask (zone.js:424)
    at PDFViewer.setDocument (pdf_viewer.js:2470)
    at eval (ghs-pdf-viewer.component.ts:123)
    at ZoneDelegate.invoke (zone.js:392)
    at Object.onInvoke (core.js:4629)
    at ZoneDelegate.invoke (zone.js:391)
    at Zone.run (zone.js:142)
    at eval (zone.js:873)
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.js:4620)
    at ZoneDelegate.invokeTask (zone.js:424)
    at resolvePromise (zone.js:824)
    at eval (zone.js:876)
    at ZoneDelegate.invokeTask (zone.js:425)
    at Object.onInvokeTask (core.js:4620)
    at ZoneDelegate.invokeTask (zone.js:424)
    at Zone.runTask (zone.js:192)
    at drainMicroTaskQueue (zone.js:602)
    at <anonymous>

Second fail with .angular-cli.json

I also tried to include both JavaScript files via .angular-cli.json:

{
	"apps": [
		{
			"assets": [
				"assets",
				"manifest.json"
			],
			"styles": [
				"styles.scss",
				"../node_modules/pdfjs-dist/web/pdf_viewer.css"
			],
			"scripts": [
				"../node_modules/pdfjs-dist/build/pdf.js",
				"../node_modules/pdfjs-dist/web/pdf_viewer.js"
			]
		}
	]
}

And just declare PDFJS in my component:

declare var PDFJS: any;

In this case, I have no error message, but the PDFJS.getDocument() promise is never executed, so nothing happens.

[TL;DR] Question

So my question is: what is the correct method of importing both pdf.js and pdf_viewer.js scripts in Angular? I did check the webpack example but this example only uses the core pdf.js, not pdf_viewer.js.

As a side note, and I think it’s related to how I should use PDFJS with Angular: my PDFJS.workerSrc does not work either (404 Not Found), so if anyone has an idea…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
VadimDezcommented, Dec 22, 2017

@LeoDupont If interested, I’ve built a wrapper for angular 2+ apps https://github.com/VadimDez/ng2-pdf-viewer

1reaction
MstfTkrslncommented, Jun 11, 2018

@lahcen-hazam I copied prebuilt version in my assets folder. Then i navigate …\assets\viewer.html?file=mypdf.pdf with file parameter then it’s working. I couldn’t find another way to accomplish this. the documentation for angular 2.x was not enoughf.
check also here

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to give pdf.js worker in Angular CLI application
Let me conclude with some self-advertising: ngx-extended-pdf-viewer is a widget making pdf.js available to Angular without the pain. There are ...
Read more >
Inline PDF Viewer in an Angular App? Now you can
This article by Aneesh Lal Gopalakrishnan describes the easiest way to integrate an inline pdf viewer into an angular application.
Read more >
How to Build an Angular PDF Viewer with ng2-pdf-viewer
Go to your terminal and install the Angular command-line interface (CLI). ... Run the command below to install the ng2-pdf-viewer library via npm...
Read more >
Rendering PDF pages with PDF.js and Vue - rossta.net
This tutorial demonstrates how to create Vue.js components that can render PDFs along with tools like webpack, PDF.js, and the canvas ...
Read more >
The React Handbook – Learn React for Beginners
You can get this ebook in PDF, ePub and Mobi format at reacthandbook.com ... Moving from Angular 1 to 2 was like moving...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found