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 l
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
andimages
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.
In
_config.yml
, set thebaseurl
option to/project-name
– note the leading slash and the absence of a trailing slash.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”).When doing permalinks or internal links, do it like this:
{{ site.baseurl }}{{ post.url }}
– note that there is no slash between the two variables.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 atlocalhost: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
.