9

I have a create-react-app project in which I want to link to a pdf file I have saved in my project. I suppose my questions are twofold:

1) I'm not sure where exactly I need to save the PDF file (src folder vs public folder)

2) How do I properly link to the pdf file? Right now I have something like this:

<a href="/portfolio-revamp/public/documents/resume.pdf">Resume</a>

and it's not displaying - it changes the URL but doesn't take you to the PDF.

Any advice greatly appreciated!

4 Answers 4

10

You need to have the file in the public folder. If you want it to open in a browser, that's what it will do. If you want to force users to download, you can add the 'download' attribute to the anchor tag.

Sign up to request clarification or add additional context in comments.

3 Comments

I added the PDF to the public folder but it still doesn't link. I have the leading slash in my href because without it, each time I click the link it keeps adding to the URL. Still not sure if I need to do something else.
did you add the download attribute to the anchor tag as well?
I added the download tag to the anchor tag. I also had to import the pdf directly at the top of the file - that did the trick.
6
  1. Place the file somewhere in src/, for example src/static/my-file.pdf.
  2. Import the file at the top of your component, like import myFile from "./static/my-file.pdf"; (path is relative to src/).
  3. Reference the object in your a tag: <a href={myFile}>Link to My File</a>

4 Comments

This is a better approach than using the /public folder, which provides no compile-time errors if the path to the file is not specified correctly
Does this even work? Maybe in normal JavaScript because TypeScript throws an error at the import line: Cannot find module '../.../abc.pdf' or its corresponding type declarations. TS2307 ?
Ugly, but u can bypass TS error using // @ts-ignore
@Alex You have to add this to /src/react-app-env.d.ts file to make it work: declare module '*.pdf' { const src: string; export default src; }
2

I had this issue in two different areas of my project, in the App component in the src folder and in a link from a child Navigation component. I created a 'docs' folder in the src directory and used require syntax to link to my file and it worked for me.

In the instance of the navigation component I accessed the file by changing the directory up two levels.

<Link href={require("../../docs/FILE_NAME.PDF")} target="blank">
  Resume    
<Link>

In the instance of the App component I searched in the same directory.

<Link href={require("./docs/FILE_NAME.PDF")} target="blank">
  Resume
</Link>

Comments

2

Adding "download" helped me using Create React App

<a
  href="/pdfname.pdf"
  download
>
  Download Resume
</a>

pdf location:

in public folder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.