0

I have a problem with events, when open pdf in external window. They are not triggered even with 'viewerId' attribute. Here is my code:

HTML

 <a *ngIf="document.s3_link" class="document-title" (click)="openDocument(document)">{{ document.description }}</a
                    >
<ng2-pdfjs-viewer
        #externalPdfViewer
        viewerId="MyUniqueID"
        [externalWindow]="true"
        (onDocumentLoad)="highlightSearchTerm()"
    ></ng2-pdfjs-viewer>

TypeScript

openDocument(document): void {
        this.getDocumentBlob(document.s3_link).subscribe(res => {
            this.externalPdfViewer.pdfSrc = res
            this.externalPdfViewer.downloadFileName = document.description
            this.externalPdfViewer.refresh()
        })
    }

getDocumentBlob(link): Observable<any> {
        let headers = new HttpHeaders()
        headers = headers.set("Accept", "application/pdf")
        return this.http.get(link, { headers: headers, responseType: "blob" })
    }

    highlightSearchTerm() {
        this.externalPdfViewer.PDFViewerApplication.findController.executeCommand(
            "find",
            {
                caseSensitive: false,
                findPrevious: undefined,
                highlightAll: true,
                phraseSearch: true,
                query: this.initQuery,
            }
        )
    }
Community
  • 1
  • 1
  • When you say "an external window" you mean a different browser window? – RTYX Oct 08 '19 at 15:25
  • I mean different browser tab – Petro Prokopovych Oct 09 '19 at 14:28
  • I think you should give a better and more descriptive explanation of your issue. What is it exactly happening now and what are you exactly expecting? Otherwise, it's very hard to help. – RTYX Oct 09 '19 at 15:35
  • I need to know when pdf is loaded. There is (onDocumentLoad) event for it. It works when I open pdf is the same tab. But when I try to open in external tab via adding [externalWindow]="true" - all events stop working. – Petro Prokopovych Oct 10 '19 at 13:33
  • I'm having exactly this issue, using the Angular "ng2-pdfjs-viewer" control, and my control is exactly the same tab... but no events get kicked off. The pdf displays beautifully, but the onDocumentLoad() function doesn't get kicked off at all. – Mike Gledhill Aug 27 '21 at 14:44

1 Answers1

1

Found this in documentation in one of the issues.

When you are opening PDF in a new window, events cannot be emitted back to former window.

Please see this SO: Communication between tabs or windows

Documentation needs to be updated to reflect this. Using above techniques, it may be achieved, but that would require an improvement/implementation.

James
  • 169
  • 7