I have a set of pdf files sitting in a folder in my project.
All I need to do is to create a link to these PDF files and view them in browser or download it.
I use React Router 4 and React 16 and create-react-app bootstrapping tool.
Regardless of how I link it (Link component or a tag) it still goes to my last Route in my router config.
I have been googling for last two hours... But no luck..
Is there any way to tell router not to route for PDF/XLS files?
Thanks
Step 1: Create a React application using the following command. Step 2: After creating your project folder (i.e. my-app), move to it by using the following command. Step 3: After creating the React application, Install the react-pdf package using the following command. Step 4: Add sample.pdf file to my-app/src folder which you want to display.
So, in this article, I will evaluate 5 PDF Viewer Libraries for React with feature comparisons to help you choose the best one for your requirement. 1. React-pdf/renderer — Specialized in rendering and creating PDFs. React-pdf/renderer is a widely used library for creating PDF files on the browser and server.
React-file-viewer- Supports many file formats. The React-file-viewer is a extendable file viewer for web applications which supports PDFs, images, csv, xslx, and docx. This library provides a component named FileViewer which is used to display the content, and it accepts several props:
We can integrate react-pdf using the following approach. Step 1: Create a React application using the following command. Step 2: After creating your project folder (i.e. my-app), move to it by using the following command. Step 3: After creating the React application, Install the react-pdf package using the following command.
I tried squizz's way, but the problem I'm finding is that if you click the link more than once or twice, it will revert to going back to going to the last route.
You can make a folder in src
to hold your pdf's, and link to them normally, ex:
import pdf from '../files/myfile.pdf'
render () {
<a href={pdf}>Click here for my pdf</a>
}
The only problem is, you will get a hash appended to your file, so it will come out as myfile.d2e24234.pdf or something. I think it has to do with file-loader...currently trying to figure it out.
EDIT
The answer if you don't want to use src is to delete the service worker from the create-react-app project. For some reason it affects react-router's handling of server routes.
I made a github issue here to read about it: https://github.com/facebookincubator/create-react-app/issues/3608
Ran into this same issue and fixed it by resorting to old-fashioned html:
<a href="docs/document.pdf">
Document
</a>
And copying the pdf to my publicly served folder with CopyWebpackPlugin in webpack.config:
plugins: [
new CopyWebpackPlugin([
{
from: 'docs/document.pdf',
to: path.resolve(BUILD_PATH + "/docs/document.pdf")
},
])
]
I met the same issue, I think it's because of the way CRA handles queries : I ended up putting my PDF files in the public folder, and link to them using :
{process.env.PUBLIC_URL + '/myfile.pdf'}
as src to my tags.
Not the best way I guess, but works fine enough...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With