Skip to content

Commit

Permalink
Added font-face to weasyprint
Browse files Browse the repository at this point in the history
  • Loading branch information
manjurulhoque committed Nov 3, 2024
1 parent e0591ac commit 74c06f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
58 changes: 56 additions & 2 deletions resume_cv/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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
from django.shortcuts import render, redirect
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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions static/js/custom-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', `<link rel="stylesheet" href="${url_default_css_template}">`);
Expand Down

0 comments on commit 74c06f7

Please sign in to comment.