diff --git a/lms/lms/doctype/lms_certificate/lms_certificate.py b/lms/lms/doctype/lms_certificate/lms_certificate.py index 3c5f4de8..8d01b63d 100644 --- a/lms/lms/doctype/lms_certificate/lms_certificate.py +++ b/lms/lms/doctype/lms_certificate/lms_certificate.py @@ -6,6 +6,7 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import add_years, nowdate from frappe.utils.pdf import get_pdf +from weasyprint import HTML, CSS from lms.lms.utils import is_certified @@ -50,7 +51,18 @@ def create_certificate(course): @frappe.whitelist() -def get_certificate_pdf(html): +def get_certificate_pdf(html_str): + + html = HTML(string=html_str) + css = CSS( + string=""" + @page { + size: A4 landscape; + }""" + ) + main = html.render(stylesheets=[css]) + pdf = main.write_pdf() + frappe.local.response.filename = "certificate.pdf" - frappe.local.response.filecontent = get_pdf(html, {"orientation": "LandScape"}) + frappe.local.response.filecontent = pdf frappe.local.response.type = "pdf" diff --git a/lms/lms/print_format/__init__.py b/lms/lms/print_format/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lms/lms/print_format/certificate/__init__.py b/lms/lms/print_format/certificate/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lms/templates/certificate.html b/lms/templates/certificate.html index 245970cb..c07aaf40 100644 --- a/lms/templates/certificate.html +++ b/lms/templates/certificate.html @@ -2,8 +2,7 @@
-
+
diff --git a/lms/www/courses/certificate.html b/lms/www/courses/certificate.html index 85b164d7..68e486e1 100644 --- a/lms/www/courses/certificate.html +++ b/lms/www/courses/certificate.html @@ -15,11 +15,7 @@ {{ course.title }}
- {% if custom_template %} - {{ custom_template }} - {% else %} - {% include "lms/templates/certificate.html" %} - {% endif %} + {% include "lms/templates/certificate.html" %}
diff --git a/lms/www/courses/certificate.js b/lms/www/courses/certificate.js index b4118f31..263e262f 100644 --- a/lms/www/courses/certificate.js +++ b/lms/www/courses/certificate.js @@ -1,6 +1,6 @@ frappe.ready(() => { $("#export-as-pdf").click((e) => { - export_as_png(e); + export_as_pdf(e); }); }); @@ -8,7 +8,7 @@ const export_as_pdf = (e) => { var formData = new FormData(); //Push the HTML content into an element - formData.append("html", $("#certificate-card").html()); + formData.append("html_str", $("#certificate-card").html()); var blob = new Blob([], { type: "text/xml" }); formData.append("blob", blob); diff --git a/lms/www/courses/certificate.py b/lms/www/courses/certificate.py index 404b1b4b..8321862b 100644 --- a/lms/www/courses/certificate.py +++ b/lms/www/courses/certificate.py @@ -1,6 +1,6 @@ import frappe from frappe.utils.jinja import render_template - +from frappe.utils import get_url from lms.lms.utils import get_instructors @@ -31,7 +31,9 @@ def get_context(context): "User", context.certificate.member, ["full_name"], as_dict=True ) - context.logo = frappe.db.get_single_value("Website Settings", "banner_image") + context.logo = get_url( + frappe.db.get_single_value("Website Settings", "banner_image"), full_address=True + ) template_name = frappe.db.get_single_value( "LMS Settings", "custom_certificate_template" ) diff --git a/requirements.txt b/requirements.txt index 293d21a8..abb581a8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ websocket_client markdown beautifulsoup4 lxml +cairocffi