I am using GitHub Pages to host my website. I have a PDF file that I want visitors to be able to open directly in a browser. But when I upload the PDF file to GitHub Pages and link to it it opens in GitHub's viewer. Is there any way to open the PDF in a browser? I do not want to upload the document to dropbox or Google Drive as these services are not available in certain countries.
Using a raw URL would lead to download. But I would like to have the file open in the browser.
Instead of loading your PDF directly from GitHub, include it in your GitHub Pages branch as a static file. This can be done by simply putting the file somewhere in your source tree:
So put the PDF somewhere that makes sense, for example in
pdfs/foo.pdf
.To make a link to this PDF work both locally and on GitHub Pages, Jekyll recommends the following (note especially point #2):
So now you can link to your PDF with
{{ site.baseurl }}/pdfs/foo.pdf
.It's very easy to display Pdf in browser from Github static page, for that you need approach following process,
For example, to display pdf in Html web page
This is an instant demo. More details can be found Here.
Place the PDF file in a folder like; www.website/pdf/example.pdf this will stop the file downloading outside of the root directory.
The steps are:
1. Create a new file in main directory.
2. Name file /pdf/ and save it.
3. Upload example.pdf file to this folder
Result:: PDF is viewable at; www.website/pdf/example.pdf
4. Optional step to avoid the .pdf URL. Create a index.html file in this folder and create a blank html file with PDF embed tag:
Suppose your personal website is hosted in a Github page as follows:
The repository should be name as username.github.io. If you have a pdf file named document.pdf and you place it in the directory folder then you should be able to open it directly in the browser through the following link:
To allow the user to open the pdf in a new window in the browser, you may use the following HTML, where "PDF" points to the link:
You can link it this way
<a target='_blank' href={require('path/to/pdf/file')}>PDF Doc</a>
Then, when you push to GitHub page, you will see your pdf file path as so
https://userName.github.io/repoName/static/media/pdfFileName.hashNumber.pdf
I believe it works if you import the file instead of using require as well but I haven't tested with that.