Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening PDF in a browser with Github Pages

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.

like image 566
Yan Song Avatar asked Jun 10 '15 02:06

Yan Song


People also ask

How do I force a PDF to open in browser?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)

Can github show PDF?

We've been displaying 3D, map, and tabular files for a while now. We're now happy to add PDF documents to the list! Simply browse to a PDF document and we'll render it in your browser like any other file. From presentations to papers, we've got you covered.

Can you add PDF to github?

On GitHub.com, navigate to the main page of the repository. Above the list of files, using the Add file drop-down, click Upload files. Drag and drop the file or folder you'd like to upload to your repository onto the file tree.


Video Answer


2 Answers

Suppose your personal website is hosted in a Github page as follows:

https://username.github.io 

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:

https://username.github.io/folder/document.pdf 

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:

<a href="username.github.io/folder/document.pdf" target="_blank">PDF.</a> 
like image 91
ruifgmonteiro Avatar answered Sep 28 '22 04:09

ruifgmonteiro


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:

Every other directory and file except for those listed above—such as css and images folders, favicon.ico files, and so forth—will be copied verbatim to the generated site. There are plenty of sites already using Jekyll if you’re curious to see how they’re laid out.

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):

Sometimes it’s nice to preview your Jekyll site before you push your gh-pages branch to GitHub. However, the subdirectory-like URL structure GitHub uses for Project Pages complicates the proper resolution of URLs. Here is an approach to utilizing the GitHub Project Page URL structure (username.github.io/project-name/) whilst maintaining the ability to preview your Jekyll site locally.

  1. In _config.yml, set the baseurl option to /project-name – note the leading slash and the absence of a trailing slash.

  2. When referencing JS or CSS files, do it like this: {{ site.baseurl }}/path/to/css.css – note the slash immediately following the variable (just before “path”).

  3. When doing permalinks or internal links, do it like this: {{ site.baseurl }}{{ post.url }} – note that there is no slash between the two variables.

  4. Finally, if you’d like to preview your site before committing/deploying using jekyll serve, be sure to pass an empty string to the --baseurl option, so that you can view everything at localhost:4000 normally (without /project-name at the beginning): jekyll serve --baseurl ''

So now you can link to your PDF with {{ site.baseurl }}/pdfs/foo.pdf.

like image 45
Chris Avatar answered Sep 28 '22 03:09

Chris