0

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)

RobinHood
  • 33
  • 1
  • 8
  • the only way to achieve this is convert your pdf to an accessible form using some library. Not sure if PDFKit and libraries like mozilla's pdf.js has to ability to convert the PDF form to a normal form – karthick Sep 18 '18 at 19:01
  • looks like pdf.js has support for forms. So you should be able to access the search https://github.com/mozilla/pdf.js/blob/master/examples/acroforms/acroforms.js – karthick Sep 18 '18 at 19:03
  • If you display the PDF using the or tag, you'll be relying on the native operating system or browser level PDF viewer to display your PDF. Even if you manage to put the links in the PDF, you can't guarantee the viewer will execute them when clicked. Look into displaying the PDF using something like pdf.js which will also let you parse the text and add links on the fly. – joelgeraci Sep 18 '18 at 20:31

0 Answers0