Inserting hyperlinks in an embedded PDF, Javascript

1k Views Asked by At

Brief background -

I am sending a pdf file from backend(node server) and displaying it in front end(Angular). Node response on the back end would look like,

fs.readFile(__dirname + filePath , function (err,data){
    res.writeHead(200, {'Content-Type': 'application/pdf'});
    res.end(data);
});

The received file on the front end is embedded into the

< object > or < embed > tag.

The source code would look something like

<object data="report.pdf" type="application/pdf" width="100%" height="100%"></object>

Question -

I got to get a function working where, when a user clicks over a paragraph in a pdf which is embedded in website, I need some action to be performed. Just like event and a listener. Say, on clicking a paragraph within PDF, it should behave like a hyperlink.

The worst case I can think of is to convert PDF to JSON/ text, insert hyperlink when converting it back to PDF (using packages like PDFKit). But how would I get a interoperable PDF, event redirection to java script. Where you can highlight the paragraph searched, or redirect to a different page, or any other function in java script.

The closest I could get is - Getting notified when the user clicks a link in an embedded PDF. But that thread is 7 years old and I believe there should be some node packages or other alternatives which I am not able to find.

Roughly - I want to get JS and PDF dynamic interaction. For example - search in embedded PDF, so JS would navigate pdf to that search, highlight the search, makes the highlighted part clickable, on click should redirect to a different page (just like href tag)

0

There are 0 best solutions below