From 74c06f777fdc0d92f18422270214e6a90a4e007e Mon Sep 17 00:00:00 2001 From: Manjurul Hoque Rumi Date: Sun, 3 Nov 2024 22:55:47 +0600 Subject: [PATCH] Added font-face to weasyprint --- resume_cv/views.py | 58 +++++++++++++++++++++++++++++++++++-- static/js/custom-builder.js | 4 +-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/resume_cv/views.py b/resume_cv/views.py index e4aac09f..0d9e8d52 100644 --- a/resume_cv/views.py +++ b/resume_cv/views.py @@ -1,6 +1,7 @@ import json from django.contrib.auth.decorators import login_required +from django.templatetags.static import static from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponse, JsonResponse from django.middleware.csrf import get_token @@ -8,7 +9,8 @@ from django.urls import reverse_lazy from django.views import View from django.views.generic import ListView -from weasyprint import HTML +from weasyprint import HTML, CSS +from weasyprint.text.fonts import FontConfiguration from jobsapp.decorators import user_is_employee # Create your views here. @@ -123,7 +125,59 @@ def get_queryset(self): def download_resume(request, id): resume = ResumeCv.objects.get(id=id) if resume: - pdf_file = HTML(string=resume.content).write_pdf() + # Font is not working in pdf + font_config = FontConfiguration() + css = CSS(string=f''' + @font-face {{ + font-family: "Font Awesome 5 Brands"; + font-style: normal; + font-weight: 400; + src: url("{static('webfonts/fa-brands-400.eot')}"); + src: url("{static('webfonts/fa-brands-400.eot?#iefix')}") format("embedded-opentype"), + url("{static('webfonts/fa-brands-400.woff2')}") format("woff2"), + url("{static('webfonts/fa-brands-400.woff')}") format("woff"), + url("{static('webfonts/fa-brands-400.ttf')}") format("truetype"), + url("{static('webfonts/fa-brands-400.svg#fontawesome')}") format("svg"); + }} + @font-face {{ + font-family: "Font Awesome 5 Free"; + font-style: normal; + font-weight: 400; + src: url("{static('webfonts/fa-regular-400.eot')}"); + src: url("{static('webfonts/fa-regular-400.eot?#iefix')}") format("embedded-opentype"), + url("{static('webfonts/fa-regular-400.woff2')}") format("woff2"), + url("{static('webfonts/fa-regular-400.woff')}") format("woff"), + url("{static('webfonts/fa-regular-400.ttf')}") format("truetype"), + url("{static('webfonts/fa-regular-400.svg#fontawesome')}") format("svg"); + }} + @font-face {{ + font-family: "Font Awesome 5 Free"; + font-style: normal; + font-weight: 900; + src: url("{static('webfonts/fa-solid-900.eot')}"); + src: url("{static('webfonts/fa-solid-900.eot?#iefix')}") format("embedded-opentype"), + url("{static('webfonts/fa-solid-900.woff2')}") format("woff2"), + url("{static('webfonts/fa-solid-900.woff')}") format("woff"), + url("{static('webfonts/fa-solid-900.ttf')}") format("truetype"), + url("{static('webfonts/fa-solid-900.svg#fontawesome')}") format("svg"); + }} + .fa, .fas {{ + font-family: "Font Awesome 5 Free"; + font-weight: 900; + font-style: normal; + }} + .far {{ + font-family: "Font Awesome 5 Free"; + font-weight: 400; + font-style: normal; + }} + .fab {{ + font-family: "Font Awesome 5 Brands"; + font-weight: 400; + font-style: normal; + }}''', font_config=font_config) + + pdf_file = HTML(string=resume.content, encoding='utf-8').write_pdf(stylesheets=[css], font_config=font_config) response = HttpResponse(pdf_file, content_type="application/pdf") response["Content-Disposition"] = f'attachment; filename="{resume.name}.pdf"' return response diff --git a/static/js/custom-builder.js b/static/js/custom-builder.js index b814bc46..4d921c06 100644 --- a/static/js/custom-builder.js +++ b/static/js/custom-builder.js @@ -59,8 +59,8 @@ editor.on('load', function () { // Customize builder interface // remove class's state-hover,click... - var classs_state = document.getElementById("gjs-clm-up"); - if (classs_state) classs_state.remove(); + var class_state = document.getElementById("gjs-clm-up"); + if (class_state) class_state.remove(); // add default css on template font-awesome const head = editor.Canvas.getDocument().head; head.insertAdjacentHTML('beforeend', ``);