44

When viewing a PDF file on GitHub, there seems to be no way to have the browser simply display the file as opposed to downloading it.

None of these links work, and these are the only ones I'm able to get to (or derive from the behaviour of working with source code):

2
  • Has this been reported anywhere officially to GitHub? Maybe they can just change it. I opened a discussion here but not sure that is the right place.
    – Albert
    Feb 20 at 19:08
  • @Albert Not to my knowledge. Feb 20 at 21:22

7 Answers 7

30

Use Google Docs viewer with a url like:

https://docs.google.com/viewer?url=${link_to_raw_pdf}

e.g.

https://docs.google.com/viewer?url=https://raw.githubusercontent.com/degoes-consulting/lambdaconf-2015/master/speakers/jdegoes/intro-purescript/presentation.pdf

https://docs.google.com/viewer?url=https://raw.githubusercontent.com/degoes-consulting/lambdaconf-2015/master/speakers/jdegoes/intro-purescript/presentation.pdf

3
  • 1
    Very useful trick. +1. I would some details about the correct breakdown of the GitHub address (Namely how to extract the address of a certain PDF). Thank You.
    – Royi
    Aug 1, 2017 at 18:38
  • 1
    Copy the link from the "Download" button. i.e. right-click, copy link address. Aug 1, 2017 at 23:05
  • 1
    The right-click->"copy link address" approach no longer works (it gives a link to a URL that redirects to the actual raw PDF url, which Google Docs' viewer doesn't understand). Instead, just click the button to download it, open Chrome's Downloads page, right click the raw.githubusercontent.com link displayed there, and press "Copy link address". The URL obtained this way works with the Google Docs viewer url.
    – Venryx
    Apr 13, 2021 at 4:58
23

Github uses content-disposition: attachment for PDFs, which downloads the file automatically on github.com. You can host the file on a static page provided by GitHub which can link to this file and update as you update the PDF. For that, you can see github pages.

Summary

  • Create a repo named USERNAME.github.io.
  • In that repo, click Settings then click Automatic Page Generator, then complete the wizard to publish a User Page.
  • Go back to your content repo and click Settings then click Automatic Page Generator, then complete the wizard to publish a Project Page. (Logically, the project page's files would go in a special directory but GitHub puts them in a gh-pages branch. Don't merge that branch to/from master.)
  • Sync the gh-pages branch to your computer then copy in your PDFs:

    git checkout gh-pages
    git checkout master -- 'docs/*.pdf'

  • Commit the changes and push them to GitHub.
  • Now go back to the repo on GitHub and use the Automatic Page Generator to edit the Project Page, adding links to these PDFs of the form:

    [Intro.pdf](http://USERNAME.github.io/REPONAME/docs/Intro.pdf).

  • Republish the Project Page.
5
  • Using Pages is actually a pretty clever work-around! If there is no other way around this by noon tomorrow, this will be good enough. :) Aug 17, 2013 at 4:53
  • Have you ever found a way to do this without using Github Pages? The problem we're facing is the fact that Github Pages are necessarily public, while the PDFs we wish to include are private.
    – Joost
    Feb 25, 2015 at 12:41
  • @Joost no, I haven't tried to make PDFs private. Your best bet with github would be to create a private repo that is a github pages repo and then make the URL hard to discover (which is not really private, just harder to find). Mar 4, 2015 at 18:57
  • 3
    Recently GitHub introduced PDF Viewing. Apr 3, 2015 at 13:50
  • 1
    An alternative is to use Google Docs as in docs.google.com/viewer?url=https://raw.githubusercontent.com/… May 23, 2015 at 1:32
13

Jupyter's nbviewer handles the task. Just paste the URL to your GitHub hosted PDF into http://nbviewer.jupyter.org/.

For example, here is a PDF that I host on GitHub rendered in the browser.

2
  • 1
    This looks promising but I keep getting 503 errors when I try to use it Nov 29, 2016 at 15:36
  • Excellent solution!
    – NFT Master
    Jul 21, 2020 at 15:18
7

As of March 17, 2015 GitHub now renders PDF documents inline in the web interface.

Reference: PDF Viewing

2
  • 6
    It does but it's not as good as the experience in Chrome May 23, 2015 at 1:31
  • @StevenShaw perhaps, but i don't use chrome :) the same principle applies for safari i suppose, but given my original Q, this still answers the question :) May 23, 2015 at 2:27
4

The answer by @MuhammadUsman works, but is more complicated than necessary, now that Github pages has a dedicated "Pages" panel in the settings for each repository.

Steps:

  1. Open your repo's settings, and click the "Pages" panel.
  2. Click the "Source" dropdown, and select "main"/"master", then save.
  3. Your files are now directly accessible (to be viewed by the default Chrome pdf viewer) at: https://USERNAME.github.io/REPO_NAME/PATH_TO_FILE.pdf
2

As a simple visitor of a GitHub repository, the following Firefox extension works for me to overwrite the content-disposition: attachment header that GitHub sends according to the main answer.

Firefox extension “Display inline” | The extension's source on GitHub

0

I try to convert PDF to SVG so that it can be displayed correctly. There are some examples here.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.