4

I've got a pdf file with a lot of external links inside. These links lead to specific time positions in youtube videos, but this does not really matter for the question. I've also got a webpage with a pdf viewer, it is based on the example examples/components/simpleviewer.html from the pdf.js repository. I want to display these links not in the original form, but to make them call some javascript code, so that I will be able to react on clicking links by myself and don't actually follow to the original links destinations.

First I was trying to extend the PDFViewer class from the web/pdf_viewer.js in the pdfjs-dist npm package, but I didn't manage to find out what to override there and how. So I made a dirty tweak by patching the AnnotationLayer class. Now everything works, but this solution may lead to problems in future. I want to find the supposed way to achieve the same.

Here is my solution in typescript:

let AnnotationLayer = (<any>pdfjsLib).AnnotationLayer;
let old_annotations_renderer = AnnotationLayer.render;
AnnotationLayer.render = (parameters:any) => {
    for (let annotation of parameters.annotations) {
        if (is_video_url(annotation.url))
            annotation.url = `javascript:console.log('click on ${annotation.url}')`;
    }
    old_annotations_renderer(parameters);
};
Ilya Posov
  • 138
  • 3
  • 9
  • 1
    why don't you proxy those links over a server with a redirect? Than you could also distribute the pdf. Or do you have an example for a reaction? You may also intercept the dom, ie. all clicks, like https://stackoverflow.com/questions/5934211/getting-notified-when-the-user-clicks-a-link-in-an-embedded-pdf or here: https://stackoverflow.com/questions/2136461/use-javascript-to-intercept-all-document-link-clicks – pce Jul 20 '20 at 19:16
  • I've got a youtube player embedded at the same page, and to react, I should move the video to the time position taken from the link. So, if the pdf is open inside some pdf viewer, links lead to the youtube site, and if the pdf is inside my page, links move the video inside the player. So, I'm not able to use a server for redirection. – Ilya Posov Jul 21 '20 at 19:44
  • Your links to another questions may be a solution and I will consider them, but I was looking for a way to use pdfjs. The thing is that I use the pdf viewer, written as the example to the pdfjs, so I believe there exists a way to control how annotations (with links) are rendered by this viewer. – Ilya Posov Jul 21 '20 at 19:51
  • Now I see, that suggestions in the linked questions are just workarounds, no better than my current solutions. If I intercept all document links clicks, I will later need to add some logic to tell pdf links from other links. I thought it was possible to fix the viewer example to make it render links differently. – Ilya Posov Jul 24 '20 at 19:37
  • whats wrong with workarrounds, your question asks for a fetaure that is definitly not part of the pdf specs ;-) – pce Jul 25 '20 at 13:28
  • This sounds reasonable, this is definitely not a PDF feature, so one should not think, that this may be easily achieved. But as far as I understand, pdf.js suggest users to write there own PDF viewers and provides an example viewer that may be examined and either reused or rewritten. The question was, may I somehow reuse their viewer and patch it to render stuff differently. Unfortunately, I am not able to find documentation or help about their example viewer, so I hoped someone here on stackoverflow had experience with this viewer. – Ilya Posov Aug 04 '20 at 11:13

0 Answers0