feat: export certificate

This commit is contained in:
Jannat Patel
2022-08-08 16:40:40 +05:30
parent 6e741bd800
commit c58f5efcdd
5 changed files with 71 additions and 32 deletions

View File

@@ -42,3 +42,9 @@ def create_certificate(course):
})
certificate.save(ignore_permissions=True)
return certificate
@frappe.whitelist()
def get_certificate_pdf(html):
frappe.local.response.filename = "certificate.pdf"
frappe.local.response.filecontent = get_pdf(html, {"orientation": "LandScape"})
frappe.local.response.type = "pdf"

View File

@@ -829,7 +829,7 @@ pre {
.certificate-content {
background-color: #FFFFFF;
border-width: 100px;
border-width: 10px;
border-style: solid;
}

View File

@@ -1,30 +1,35 @@
<div class="certificate-card">
<div class="certificate-content" style="border-image: url(/assets/lms/images/border.png) 13% round;">
<img src="{{ logo }}" class="certificate-logo">
<div class="mt-16">
<div id="certificate-card" style="background: #FFFFFF; border-radius: 0.5rem; position: relative;
box-shadow: 0px 1px 2px rgba(25, 39, 52, 0.05), 0px 0px 4px rgba(25, 39, 52, 0.1); padding: 1rem;">
<div style="border: 10px solid var(--primary-color); display: flex; flex-direction: column; align-items: center; padding: 4rem;
justify-content: center; background-color: #FFFFFF;">
<img src="{{ logo }}" style="height: 1.5rem;">
<div style="margin-top: 4rem;">
{{ _("This certifies that") }}
</div>
<div class="certificate-heading"> {{ member.full_name }} </div>
<div class="mt-2"> {{ _("has successfully completed the course on") }}
<div style="font-size: 2rem; font-weight: 500; color: #192734;"> {{ member.full_name }} </div>
<div style="margin-top: 0.5rem;"> {{ _("has successfully completed the course on") }}
<b> {{ course.title }} </b> on {{ frappe.utils.format_date(certificate.issue_date, "medium") }}. </div>
<div class="certificate-footer">
<div style="display: flex; justify-content: center; margin: 4rem auto 0; width: fit-content;">
{% if instructors %}
<div class="">
<span class="certificate-footer-item"> {{ instructors }} </span>
<hr class="mt-2 mb-2">
<div class=""> {{ _("Course Instructor") }} </div>
<div>
<div style="color: #192734; font-weight: bold; font-family: cursive; font-size: 1.25rem;">
{{ instructors }}
</div>
<hr style="margin: 0.5rem 0;">
<div> {{ _("Course Instructor") }} </div>
</div>
{% endif %}
{% if certificate.expiry_date %}
<div class="ml-8">
<div class="certificate-footer-item">
<div style="margin-left: 2rem;">
<div style="color: #192734; font-weight: bold; font-family: cursive; font-size: 1.25rem;">
{{ frappe.utils.format_date(certificate.expiry_date, "medium") }}
</div>
<hr class="mt-2 mb-2">
<div class=""> {{ _("Expiry date") }} </div>
<hr style="margin: 0.5rem 0;">
<div> {{ _("Expiry date") }} </div>
</div>
{% endif %}

View File

@@ -5,10 +5,10 @@
<div class="common-page-style">
<div class="container certificate-page">
<!-- {% if certificate.member == frappe.session.user %}
<div class="button is-secondary pull-right mt-4" id="export-as-pdf" data-certificate="{{ certificate.name }}"
{% if certificate.member == frappe.session.user %}
<div class="button is-secondary pull-right" id="export-as-pdf" data-certificate="{{ certificate.name }}"
data-certificate-name="{{ member.full_name }} - {{ course.title }}">{{ _("Export") }}</div>
{% endif %} -->
{% endif %}
<div class="breadcrumb">
<a class="dark-links" href="/courses">{{ _("All Courses") }}</a>

View File

@@ -1,25 +1,53 @@
frappe.ready(() => {
$("#export-as-pdf").click((e) => {
export_as_pdf(e);
export_as_png(e);
})
});
const export_as_pdf = (e) => {
var formData = new FormData();
//Push the HTML content into an element
formData.append("html", $("#certificate-card").html());
var blob = new Blob([], { type: "text/xml"});
formData.append("blob", blob);
var xhr = new XMLHttpRequest();
xhr.open("POST", '/api/method/lms.lms.doctype.lms_certificate.lms_certificate.get_certificate_pdf');
xhr.setRequestHeader("X-Frappe-CSRF-Token", frappe.csrf_token);
xhr.responseType = "arraybuffer";
xhr.onload = function(success) {
if (this.status === 200) {
var blob = new Blob([success.currentTarget.response], {type: "application/pdf"});
var objectUrl = URL.createObjectURL(blob);
//Open report in a new window
window.open(objectUrl);
}
};
xhr.send(formData);
}
const export_as_png = (e) => {
let button = $(e.currentTarget);
button.text(__("Exporting..."));
html2canvas(document.querySelector('.certificate-card'), {
scrollY: -window.scrollY,
scrollX: 0
}).then(function(canvas) {
let dataURL = canvas.toDataURL('image/png');
let a = document.createElement('a');
a.href = dataURL;
a.download = button.attr("data-certificate-name");
a.click();
}).finally(() => {
button.text(__("Export"))
});
html2canvas(document.querySelector('#certificate-card'), {
scrollY: -window.scrollY,
scrollX: 0
}).then(function(canvas) {
let dataURL = canvas.toDataURL('image/png');
let a = document.createElement('a');
a.href = dataURL;
a.download = button.attr("data-certificate-name");
a.click();
}).finally(() => {
button.text(__("Export"))
});
}