0

I am creating a pdf using python library PyFPDF and one of my data string contains a "Greek small letter μ" known as mu. I am getting the above mentioned error. If we remove this greek letter from the data string, the code works perfect. I need help parsing this greek letter into pdf output. My code is given below:


    str = "2-3 μg/day"
    pdf = FPDF('P', 'mm', 'A4')
    
    # Add a page  
    pdf.add_page()
    
    # Set fonts and color
    pdf.set_font("Times", "B", size = 24)
    pdf.set_text_color(0, 0, 0)
    
    # insert the texts in pdf
    pdf.write(h = 10, txt = str)
    
    # save the pdf with name .pdf
    pdf.output("output.pdf")

Thanks in advance.

Alpha Geek
  • 21
  • 4

1 Answers1

1

Unsure why PyFPDF is more pedantic than FPDF editor, however your text in theory requires 2 Fonts (Times and Symbol).

"Symbol" is normally used for Greek alphabets and some symbols like: Ω, φ, ≠, ©......
pdf.set_font("Symbol",....

Here using Demo shows one common text insert works with both in tandem by substituting Arial.

enter image description here

<?PHP

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage('P', 'A4');
$pdf->SetAutoPageBreak(true, 10);
$pdf->SetFont('Arial', '', 12);
$pdf->SetTopMargin(10);
$pdf->SetLeftMargin(10);
$pdf->SetRightMargin(10);


/* --- Text --- */
$pdf->SetFont('', 'B', 12);
$pdf->Text(21, 31, '2-3 μg/day');


$pdf->Output('created_pdf.pdf','I');
?>

For Times and symbol use (with your own desired offsets)

enter image description here

/* --- Text --- */
$pdf->SetFont('Times', '', 12);
$pdf->Text(19, 32, '2-3');
/* --- Text --- */
$pdf->SetFont('Symbol', '', 12);
$pdf->Text(28, 32, 'μ');
/* --- Text --- */
$pdf->SetFont('Times', '', 12);
$pdf->Text(35, 32, 'g/day');

Thus in your case

str = "2-3 μg/day"
pdf = FPDF('P', 'mm', 'A4')

# Add a page  
pdf.add_page()

# Set fonts and color
pdf.set_font("Times", "B", size = 24)
pdf.set_text_color(0, 0, 0)
# insert the texts in pdf
pdf.write(h = 10, txt = "2-3")

# Set font
pdf.set_font("Symbol", "B", size = 24)
# insert the texts in pdf
pdf.write(h = 15, txt = "μ")

# Set font
pdf.set_font("Times", "B", size = 24)
# insert the texts in pdf
pdf.write(h = 20, txt = "g/day")

# save the pdf with name .pdf
pdf.output("output.pdf")

Otherwise use a font that includes text and symbols like 'Arial' or 'DejaVu'

pdf.set_font('Arial', 'B', 24)
pdf.write(h = 10, txt = "2-3 μg/day")
K J
  • 8,045
  • 3
  • 14
  • 36