Skip to content

Commit

Permalink
Upgrade PDFJS
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Apr 8, 2024
1 parent 236729d commit 64904b0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 53 deletions.
8 changes: 3 additions & 5 deletions ynr/apps/elections/templates/elections/sopn_for_ballot.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,16 @@ <h2>
{% endif %}

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

<script src="{% static "official_documents/js/pdf.min.js" %}"></script>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = '{% static "official_documents/js/pdf.worker.min.js" %}'
</script>
{% javascript 'sopn_viewer' %}
<script>
SOPN_VIEWER.ShowSOPNInline(
'{{ object.sopn.uploaded_file.url }}',
'{{ object.ballot_paper_id }}'
)
</script>
{% javascript 'sopn_viewer' %}
{% else %}
<div><img src="{{ object.sopn.uploaded_file.url }}"
alt="Image of the candidate nomination data for {{ object.post.label }}"></div>
Expand Down
14 changes: 7 additions & 7 deletions ynr/apps/elections/templates/elections/sopn_for_election.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ <h1>SOPN for {{ object.name }}</h1>
{% endif %}
<div id="sopn-{{ object.slug }}" class="pdf_container"></div>

<script src="{% static "official_documents/js/pdf.min.js" %}"></script>
<script>
<script type="module">
import pdfjsDist from '{% static "official_documents/js/pdf.min.js" %}'

pdfjsLib.GlobalWorkerOptions.workerSrc = '{% static "official_documents/js/pdf.worker.min.js" %}'
</script>
{% javascript 'sopn_viewer' %}
<script>
SOPN_VIEWER.ShowSOPNInline(
'{{ object.electionsopn.uploaded_file.url }}',
'{{ object.slug }}',
'{{ object.sopn.uploaded_file.url }}',
'{{ object.ballot_paper_id }}'
)
</script>
{% javascript 'sopn_viewer' %}

{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,16 @@
<div id="sopn-{{ ballot.ballot_paper_id }}" class="pdf_container"
style="max-width:100% !important; width: 100% !important;"></div>
{% if ballot.sopn.uploaded_file.url|slice:"-3:" == "pdf" %}
<script type="module">
import pdfjsDist from '{% static "official_documents/js/pdf.min.js" %}'

<script src="{% static "official_documents/js/pdf.min.js" %}"></script>
<script>
pdfjsLib.GlobalWorkerOptions.workerSrc = '{% static "official_documents/js/pdf.worker.min.js" %}'
</script>
{% javascript 'sopn_viewer' %}
<script>
SOPN_VIEWER.ShowSOPNInline(
'{{ ballot.sopn.uploaded_file.url }}',
'{{ ballot.ballot_paper_id }}'
)
</script>
{% javascript 'sopn_viewer' %}
{% else %}
<div><img src="{{ ballot.sopn.uploaded_file.url }}"
alt="Image of the candidate nomination data for {{ ballot.post.label }}"></div>
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -58,46 +58,25 @@ var SOPN_VIEWER = (function () {
});
}

function show_google_viewer(sopn_url, container) {
/*
Show the Google document viewer if PDFJS can't deal with this document for whatever reason
*/
var google_frame = document.createElement("iframe");
google_frame.setAttribute("frameborder", 0);
google_frame.setAttribute("allowfullscreen", true);
google_frame.className = "document_viewer";
var url = "https://docs.google.com/viewer?url=https://candidates.democracyclub.org.uk";
url = url + encodeURI(sopn_url) + "&embedded=true";
google_frame.setAttribute("src", url);
container.append(google_frame);
}

function ShowSOPNInline(sopn_url, ballot_paper_id, options) {
// The container element
var this_pdf_container = document.getElementById("sopn-" + ballot_paper_id);

try {
var loadingTask = pdfjsLib.getDocument(sopn_url);
var loadingTask = pdfjsLib.getDocument(sopn_url);

loadingTask.promise.then(function (pdf) {
for(var page = 1; page <= pdf.numPages; page++) {
load_pages(pdf, this_pdf_container, page);
}
loadingTask.promise.then(function (pdf) {
for (var page = 1; page <= pdf.numPages; page++) {
load_pages(pdf, this_pdf_container, page);
}

}).then(null, function (error) {
}).then(null, function (error) {

if (error.name === "MissingPDFException") {
this_pdf_container.innerHTML = "<h3>PDF file not found</h3>";
}
if (error.name === "MissingPDFException") {
this_pdf_container.innerHTML = "<h3>PDF file not found</h3>";
}

if (error.name === "InvalidPDFException") {
show_google_viewer(sopn_url, this_pdf_container);
}
console.log(error)
});
} catch (e) {
show_google_viewer(sopn_url, this_pdf_container);
}
console.log(error)
});

}

Expand Down

0 comments on commit 64904b0

Please sign in to comment.