Xhtml2pdf fonts for latin characters to renders pdf template
I am rendering pdf template dynamically using xhtml2pdf library in Django. The problem is with softening marks like ā, š, ķ etc. I have tried to use all suggested fonts from documentation. I can get fonts but not this softened characters. it looks like preview of pdf
from django.template.loader import get_template
from xhtml2pdf import pisa
from io import BytesIO
def quote_preview_pdf(request, pk, pk2):
# subscription = stripeInfo(request)
user = request.user
user_company = UserCompany.objects.get(company_user=user)
client_company = user.clientcompanies_set.get(id=pk)
quote_nr = user_company.quotenumber_set.get(id=pk2)
quote_nr = user_company.quotenumber_set.get(id=pk2)
get_quoted_products = CompaniesQuote.objects.filter(quote_nr = quote_nr)
get_only_quoted_products = CompaniesQuote.objects.filter(quote_nr =
quote_nr).values_list('item__id', flat=True)
calc_totals = get_only_quoted_products.annotate(
calc_total_profit=(F('item__selling_price') * (1 - (F('discount') / 100)) * F('quantity'))
- (F('item__buying_price') * F('quantity')),
calc_total_sales_price = (F('item__selling_price') * F('quantity')),
calc_total_sales_price_disc = (F('item__selling_price') * (1 - (F('discount') / 100)) *
F('quantity')),
calc_total_sales_price_vat = (F('item__selling_price') * ( 1 + (F('item__vat') / 100)) *
F('quantity')),
calc_total_sales_price_vat_disc = (F('item__selling_price')* (1 - (F('discount') / 100)) *
( 1 + (F('item__vat') / 100)) * F('quantity')),
calc_total_vat = (F('item__selling_price')* (1 - (F('discount') / 100)) * ( 1 +
(F('item__vat') / 100)) * F('quantity')) - (F('item__selling_price') * (1 -
(F('discount') / 100)) * F('quantity'))
).aggregate(
thesum=Sum('calc_total_profit'),
sumsalles=Sum('calc_total_sales_price'),
sumsallesdiscount=Sum('calc_total_sales_price_disc'),
sumsallesvat=Sum('calc_total_sales_price_vat'),
sumsallesvatdiscount=Sum('calc_total_sales_price_vat_disc'),
sumvattotal = Sum('calc_total_vat'),
)
template_path = 'quote_preview_pdf.html'
context = {
'user_company': user_company,
'client_company': client_company,
'quote_nr':quote_nr,
'get_quoted_products':get_quoted_products,
'calc_totals':calc_totals,
}
# Create a Django response object, and specify content_type as pdf
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'filename="quote.pdf"'
# response['Content-Transfer-Encoding'] = 'binary'
# response['Content-Disposition'] = 'attachment; filename="quote.pdf"'
# find the template and render it.
template = get_template(template_path)
html = template.render(context)
pisa.pisaDocument(
src=BytesIO(html.encode('UTF-8')),
dest=response,
encoding='UTF-8'
)
# create a pdf
pisa_status = pisa.CreatePDF(
html, dest=response)
# if error then show some funy view
if pisa_status.err:
return HttpResponse('We had some errors <pre>' + html + '</pre>')
return response
For me it looks like it is not encoding UTF-8 because it change font. I have tried to play around with @font-face but no luck.
Here is my CSS for pdf
body {
font-family: 'Times New Roman';
}
@page{
size: letter portrait;
margin: 2cm;
}
It looks like this problem appears for a while but could not find the solved solution.