Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: PDFObject and PDF.js support #466

Merged
merged 9 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ math:
# CDNJS, provided by cloudflare, maybe the best CDN, but not works in China
#cdn: //cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.css

# PDF Support
# Dependencies: https://github.com/theme-next/theme-next-pdf
pdf:
enable: false

# Default(true) will load PDFObject/PDF.js script on demand
# That is it only render those page who has 'pdf: true' in Front Matter.
# If you set it to false, it will load PDFObject/PDF.js srcipt EVERY PAGE.
per_page: true

pdfobject:
# Use 2.1.1 as default, cloudflare as default CDN
cdn: //cdnjs.cloudflare.com/ajax/libs/pdfobject/2.1.1/pdfobject.min.js
height: 500px

# Han Support
# Dependencies: https://github.com/theme-next/theme-next-han
han: false
Expand Down
1 change: 1 addition & 0 deletions layout/_layout.swig
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@
{% include '_third-party/exturl.swig' %}
{% include '_third-party/bookmark.swig' %}
{% include '_third-party/copy-code.swig' %}
{% include '_third-party/pdf.swig' %}
</body>
</html>
46 changes: 46 additions & 0 deletions layout/_third-party/pdf.swig
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% if theme.pdf.enable %}
{% set is_index_has_pdf = false %}

{# At home, check if there has `pdf: true` post #}
{% if is_home() %}
{% for post in page.posts %}
{% if post.pdf and not is_index_has_pdf %}
{% set is_index_has_pdf = true %}
{% endif %}
{% endfor %}
{% endif %}

{% if not theme.pdf.per_page or (is_index_has_pdf or page.pdf) %}
<script type="text/javascript" src="{{ theme.pdf.pdfobject.cdn }}"></script>
<script type="text/javascript">
var options = {
pdfOpenParams: {
navpanes: 0,
toolbar: 0,
statusbar: 0,
pagemode: "thumbs",
view: "FitV"
},
PDFJS_URL: "/lib/pdf/web/viewer.html",
height: "{{ theme.pdf.pdfobject.height }}"
};
$("#main a").each(function() {
var href = $(this).attr("href");
if (!href) return;
if (href.endsWith(".pdf") || href.endsWith(".pdf/")) {
var id = "pdf" + Math.floor(Math.random() * 10000);
$(this).after("<div id='" + id + "'></div>").remove();
PDFObject.embed(href, "#" + id, options);
}
});
</script>
<style>
.pdfobject-container {
position: relative;
overflow: auto;
width: 100%;
height: {{ theme.pdf.pdfobject.height }};
}
</style>
{% endif %}
{% endif %}