Show a pdf document on a webpage but with substituted links

Viewed 296

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);
};
0 Answers
Related