0

I'm trying to find a way to open PDF files directly from its location. I know I can use this to open files.

import pdf from './file.pdf
<a href={Pdf} target = "_blank">Open Pdf</a>

But this is not going to work for me, I'm building an item list app with React and Node for a small company I work for, and every new item needs to have a certificate that is in PDF. User needs to be able to add PDF file when creating a new item and open it when needed. They want the app to be live on the internet, but all certificates to be on their local server, so only people that are connected to their network can see them. Is this possible to do?

I was thinking just to save the path to pdf file in the database and then use that path to display it in app. Something like this.

<a href="//10.0.0.246/share/cert/scan/file.pdf" target = "_blank">Open certificate</a>

But I can't make it work right now, even when the file is in the same folder as my app.

sofronijev
  • 47
  • 1
  • 9

1 Answers1

0

you can use require function.

<a href={require("//10.0.0.246/share/cert/scan/file.pdf")} target = "_blank">Open certificate</a>
wangdev87
  • 8,611
  • 3
  • 8
  • 31
  • Thank you, this is it! So simple. I just have one question, when I try to use a file that is not in scr folder I get this error `Relative imports outside of src/ are not supported.`, will I be able to use any file when the app is deployed online, is this just create-react-app thing? – sofronijev Oct 30 '20 at 11:09
  • check this link https://stackoverflow.com/questions/44114436/the-create-react-app-imports-restriction-outside-of-src-directory – wangdev87 Oct 30 '20 at 11:11
  • In the end, I couldn't get it to work as I need it. I made an empty create-react-app and ejected it like the answer in the link said but still got an error like before. Also, I get an error when I use a variable in require because I need to map thru an array of items and display them, this is the error `Critical dependency: the request of a dependency is an expression`, and I also get an error if there isn't a path to the certificate or if its wrong. – sofronijev Oct 30 '20 at 15:38
  • i'm not sure how your new codebase is, but i just want to tell you that you need use `require` for the non-import pdf file. – wangdev87 Oct 30 '20 at 15:40
  • `require` works if pdf is in scr folder and like this `require("//10.0.0.246/share/cert/scan/file.pdf")`, but if I try to do this `require(url)` where `let url = "//10.0.0.246/share/cert/scan/file.pdf" `, It doesn't work anymore. – sofronijev Oct 30 '20 at 15:53