Skip to content

Commit 64904b0

Browse files
committed
Upgrade PDFJS
1 parent 236729d commit 64904b0

6 files changed

Lines changed: 33 additions & 53 deletions

File tree

ynr/apps/elections/templates/elections/sopn_for_ballot.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,16 @@ <h2>
8080
{% endif %}
8181

8282
{% if object.sopn.uploaded_file.url|slice:"-3:" == "pdf" %}
83+
<script type="module">
84+
import pdfjsDist from '{% static "official_documents/js/pdf.min.js" %}'
8385

84-
<script src="{% static "official_documents/js/pdf.min.js" %}"></script>
85-
<script>
8686
pdfjsLib.GlobalWorkerOptions.workerSrc = '{% static "official_documents/js/pdf.worker.min.js" %}'
87-
</script>
88-
{% javascript 'sopn_viewer' %}
89-
<script>
9087
SOPN_VIEWER.ShowSOPNInline(
9188
'{{ object.sopn.uploaded_file.url }}',
9289
'{{ object.ballot_paper_id }}'
9390
)
9491
</script>
92+
{% javascript 'sopn_viewer' %}
9593
{% else %}
9694
<div><img src="{{ object.sopn.uploaded_file.url }}"
9795
alt="Image of the candidate nomination data for {{ object.post.label }}"></div>

ynr/apps/elections/templates/elections/sopn_for_election.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ <h1>SOPN for {{ object.name }}</h1>
2828
{% endif %}
2929
<div id="sopn-{{ object.slug }}" class="pdf_container"></div>
3030

31-
<script src="{% static "official_documents/js/pdf.min.js" %}"></script>
32-
<script>
31+
<script type="module">
32+
import pdfjsDist from '{% static "official_documents/js/pdf.min.js" %}'
33+
3334
pdfjsLib.GlobalWorkerOptions.workerSrc = '{% static "official_documents/js/pdf.worker.min.js" %}'
34-
</script>
35-
{% javascript 'sopn_viewer' %}
36-
<script>
3735
SOPN_VIEWER.ShowSOPNInline(
38-
'{{ object.electionsopn.uploaded_file.url }}',
39-
'{{ object.slug }}',
36+
'{{ object.sopn.uploaded_file.url }}',
37+
'{{ object.ballot_paper_id }}'
4038
)
4139
</script>
40+
{% javascript 'sopn_viewer' %}
41+
4242
{% endblock %}

ynr/apps/moderation_queue/templates/moderation_queue/includes/inline-lock-table-view.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,16 @@
7878
<div id="sopn-{{ ballot.ballot_paper_id }}" class="pdf_container"
7979
style="max-width:100% !important; width: 100% !important;"></div>
8080
{% if ballot.sopn.uploaded_file.url|slice:"-3:" == "pdf" %}
81+
<script type="module">
82+
import pdfjsDist from '{% static "official_documents/js/pdf.min.js" %}'
8183

82-
<script src="{% static "official_documents/js/pdf.min.js" %}"></script>
83-
<script>
8484
pdfjsLib.GlobalWorkerOptions.workerSrc = '{% static "official_documents/js/pdf.worker.min.js" %}'
85-
</script>
86-
{% javascript 'sopn_viewer' %}
87-
<script>
8885
SOPN_VIEWER.ShowSOPNInline(
8986
'{{ ballot.sopn.uploaded_file.url }}',
9087
'{{ ballot.ballot_paper_id }}'
9188
)
9289
</script>
90+
{% javascript 'sopn_viewer' %}
9391
{% else %}
9492
<div><img src="{{ ballot.sopn.uploaded_file.url }}"
9593
alt="Image of the candidate nomination data for {{ ballot.post.label }}"></div>

ynr/apps/official_documents/static/official_documents/js/pdf.min.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ynr/apps/official_documents/static/official_documents/js/pdf.worker.min.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ynr/apps/official_documents/static/official_documents/js/sopn_viewer.js

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,46 +58,25 @@ var SOPN_VIEWER = (function () {
5858
});
5959
}
6060

61-
function show_google_viewer(sopn_url, container) {
62-
/*
63-
Show the Google document viewer if PDFJS can't deal with this document for whatever reason
64-
*/
65-
var google_frame = document.createElement("iframe");
66-
google_frame.setAttribute("frameborder", 0);
67-
google_frame.setAttribute("allowfullscreen", true);
68-
google_frame.className = "document_viewer";
69-
var url = "https://docs.google.com/viewer?url=https://candidates.democracyclub.org.uk";
70-
url = url + encodeURI(sopn_url) + "&embedded=true";
71-
google_frame.setAttribute("src", url);
72-
container.append(google_frame);
73-
}
74-
7561
function ShowSOPNInline(sopn_url, ballot_paper_id, options) {
7662
// The container element
7763
var this_pdf_container = document.getElementById("sopn-" + ballot_paper_id);
7864

79-
try {
80-
var loadingTask = pdfjsLib.getDocument(sopn_url);
65+
var loadingTask = pdfjsLib.getDocument(sopn_url);
8166

82-
loadingTask.promise.then(function (pdf) {
83-
for(var page = 1; page <= pdf.numPages; page++) {
84-
load_pages(pdf, this_pdf_container, page);
85-
}
67+
loadingTask.promise.then(function (pdf) {
68+
for (var page = 1; page <= pdf.numPages; page++) {
69+
load_pages(pdf, this_pdf_container, page);
70+
}
8671

87-
}).then(null, function (error) {
72+
}).then(null, function (error) {
8873

89-
if (error.name === "MissingPDFException") {
90-
this_pdf_container.innerHTML = "<h3>PDF file not found</h3>";
91-
}
74+
if (error.name === "MissingPDFException") {
75+
this_pdf_container.innerHTML = "<h3>PDF file not found</h3>";
76+
}
9277

93-
if (error.name === "InvalidPDFException") {
94-
show_google_viewer(sopn_url, this_pdf_container);
95-
}
96-
console.log(error)
97-
});
98-
} catch (e) {
99-
show_google_viewer(sopn_url, this_pdf_container);
100-
}
78+
console.log(error)
79+
});
10180

10281
}
10382

0 commit comments

Comments
 (0)