Hi everyone
I have problem in registering a custom font for using in reportlab
here is my code snipit for doing so in views.py
import io
from django.http import FileResponse
import reportlab
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import letter
def pdf_view(request, **kwargs):
# registering custom font
reportlab_directory = os.path.dirname(reportlab.__file__)
print(f"reportlab _directory is:{reportlab_directory}")
font_folder = os.path.join(reportlab_directory, "fonts")
print(f"font folder is :{font_folder}")
custom_font_folder = os.path.join(font_folder, "Yekan.ttf")
print(f"custom font folder is:{custom_font_folder}")
custom_font = TTFont("yekan", custom_font_folder)
pdfmetrics.registerFont(custom_font)
# Create a file-like buffer to receive PDF data.
buffer = io.BytesIO()
# Create the PDF object, using the buffer as its "file."
p = canvas.Canvas(buffer, letter, bottomup=0)
# Draw things on the PDF. Here's where the PDF generation happens.
# See the ReportLab documentation for the full list of functionality.
textob = p.beginText()
textob.setTextOrigin(cm, cm)
textob.setFont("yekan-pesian", 14)
lines = ['جمع کل' + ' ' + 'قیمت واحد' + ' ' + 'تعداد' + ' ' + 'نام کالا',
'good name',
'---------------------------------------------------------------------']
bb_items = BuyBasket.objects.filter(customer=kwargs['c_id'])
for item in bb_items:
lines.append(str(item.good)+' '+str(item.quantity)+' '+str(item.price)+' '+str(item.total_price))
for line in lines:
textob.textLine(line)
# Close the PDF object cleanly, and we're done.
p.drawText(textob)
p.showPage()
p.save()
buffer.seek(0)
# FileResponse sets the Content-Disposition header so that browsers
# present the option to save the file.
return FileResponse(buffer, as_attachment=True, filename="bill.pdf")
all things looks to be set properly(when i print pathes to fonts directory its shows correct path) but
i got this error
TTFError at /orders/2/buy_basket/pdf/
Can't open file "/usr/local/lib/python3.10/site-packages/reportlab/fonts/Yekan.ttf"
any help would be appriciated
Thanks