I hope you are well.
I'm trying to write the following to a pdf using the fpdf2 library:
GHI (kWh $m^-2 day^{-1}$)
But I'm not getting the expected result, the text is written in the pdf in the same way as the code, that is: GHI (kWh $m^-2 day^{-1}$)
Here is a basic example:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("helvetica", "B", 16)
pdf.cell(40, 10, r"GHI (kWh $m^-2 day^{-1}$)")
pdf.output("test.pdf")
Could someone let me know how I can write this text in the pdf so that it is rendered correctly?
Thank you very much in advance,
Robson
fpdf2
now supports subscript & superscript, as well as <sub>
& <sup>
HTML tags: https://github.com/PyFPDF/fpdf2/blob/master/CHANGELOG.md#added-1
You can also render mathematical formulas using Google Charts API or Matplotlib: https://pyfpdf.github.io/fpdf2/Maths.html#mathematical-formulas
from io import BytesIO
from urllib.parse import quote
from urllib.request import urlopen
from fpdf import FPDF
formula = r"GHI (kWh $m^-2 day^{-1}$)"
height = 170
url = f"https://chart.googleapis.com/chart?cht=tx&chs={height}&chl={quote(formula)}"
with urlopen(url) as img_file:
img = BytesIO(img_file.read())
pdf = FPDF()
pdf.add_page()
pdf.image(img, w=30)
pdf.output("equation-with-gcharts.pdf")