import os # to handle our font dir and path to font files.
import reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
folder = os.path.dirname(reportlab.__file__) # get the folder with path
folder_font = os.path.join(folder, 'fonts') # add fonts folder to path
print(folder_font) # print the full path of font dir
custom_font = os.path.join(folder_font, 'Arabic.ttf') # Path to the font file
c_font = TTFont('Arabic', custom_font)
pdfmetrics.registerFont(c_font)
my_path = 'D:\\testing\\my_pdf\\my_pdf.pdf' # output file path
c = canvas.Canvas(my_path, pagesize=letter, bottomup=0)
c.setFont('Arabic', 16)
msg = 'Welcome to plus2net'
c.drawString(200, 200, msg) # write text in page
c.showPage() # saves current page
c.save() # stores the file and closes the canvas
By following these steps, you can successfully embed and use custom TrueType fonts in your PDF documents using Python ReportLab. This technique allows for greater flexibility and customization when creating PDF files.
Full code is hereimport os # to handle our font dir and path to font files.
import reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
folder = os.path.dirname(reportlab.__file__) # get the folder with path
folder_font = os.path.join(folder, 'fonts') # add fonts folder to path
print(folder_font) # print the full path of font dir
custom_font=os.path.join(folder_font,'Arabic.ttf') # Path to the font file
c_font=TTFont('Arabic',custom_font)
pdfmetrics.registerFont(c_font)
my_path='D:\\testing\\my_pdf\\my_pdf.pdf'# output file path
c = canvas.Canvas(my_path,pagesize=letter,bottomup=0)
c.setFont('Arabic', 16)
msg ='Welcome to plus2net'
c.drawString(200,200,msg) # write text in page
c.showPage() # saves current page
c.save() # stores the file and close the canvas
First, we import the required modules. These modules help us handle file paths, generate PDFs, and work with custom fonts and styles.
import os # to handle our font directory and path to font files.
import reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Paragraph
Explanation:
We locate the folder where ReportLab's files are stored, find the fonts directory, and register our custom font.
folder = os.path.dirname(reportlab.__file__) # Get the directory where ReportLab is installed
folder_font = os.path.join(folder, 'fonts') # Path to the fonts directory
print(folder_font) # Optionally, print the font directory path
custom_font = os.path.join(folder_font, 'AmrHindi Regular.ttf') # Path to the custom font file
c_font = TTFont('Hindi', custom_font) # Register the font with an alias name 'Hindi'
pdfmetrics.registerFont(c_font) # Register the font in ReportLab
Explanation:
We specify the output file path and create a canvas object that will be used to draw elements onto the PDF.
my_path = 'D:\\testing\\my_pdf\\my_pdf.pdf' # Path to save the generated PDF
c = canvas.Canvas(my_path, pagesize=letter, bottomup=1) # Create a canvas for the PDF with Letter size
Explanation:
We define a custom paragraph style to apply to the text in the PDF. This style includes the font, background color, border, padding, and alignment.
my_Style = ParagraphStyle('My Para style',
fontName='Hindi',
backColor='#F1F1F1',
fontSize=10,
borderColor='#FFFF00',
borderWidth=2,
borderPadding=(20, 20, 20),
leading=20,
alignment=0)
Explanation:
We define a custom paragraph style to apply to the text in the PDF. This style includes the font, background color, border, padding, and alignment.
my_Style = ParagraphStyle('My Para style',
fontName='Hindi',
backColor='#F1F1F1',
fontSize=10,
borderColor='#FFFF00',
borderWidth=2,
borderPadding=(20, 20, 20),
leading=20,
alignment=0)
Explanation:
Finally, we save the PDF and close the canvas, finalizing the document.
c.save() # Save and close the PDF document
Explanation:
import os # to handle our font dir and path to font files.
import reportlab
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter,A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Paragraph
folder = os.path.dirname(reportlab.__file__) # get the folder with path
folder_font = os.path.join(folder, 'fonts') # add fonts folder to path
print(folder_font) # print the full path of font dir
custom_font=os.path.join(folder_font,'AmrHindi Regular.ttf') # Path to the font file
c_font=TTFont('Hindi',custom_font)
pdfmetrics.registerFont(c_font)
my_path='D:\\testing\\my_pdf\\my_pdf.pdf'# output file path
c = canvas.Canvas(my_path,pagesize=letter,bottomup=1)
my_Style=ParagraphStyle('My Para style',
fontName='Hindi',
backColor='#F1F1F1',
fontSize=10,
borderColor='#FFFF00',
borderWidth=2,
borderPadding=(20,20,20),
leading=20,
alignment=0
)
width,height=A4
p1=Paragraph('''<b>About this online classes </b><BR/>\
Welcome to our online classes.<BR/> \
You can learn Python, PHP, database like MySQL \
SQLite and many more \
<font face="Hindi" color="blue">We are creating \
PDF by using ReportLab</font> \
<i>This is part of our Final report.</i>''',my_Style)
p1.wrapOn(c,300,50)
p1.drawOn(c,width-550,height-350)
c.save() # stores the file and close the canvas
PDF Table from different data sources to PDF file