So I was making an app using streamlit and when I tried to use tempfile, it shows this error-
2024-05-13 11:10:51.538 Uncaught app exception
Traceback (most recent call last):
File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 600, in _run_script
exec(code, module.__dict__)
File "/mount/src/neri/pages/korean_instruction_2.py", line 91, in <module>
pdf.add_page()
File "/home/adminuser/venv/lib/python3.11/site-packages/fpdf/fpdf.py", line 904, in add_page
self.header()
File "/mount/src/neri/pages/korean_instruction_2.py", line 69, in header
regular_font(8)
File "/mount/src/neri/pages/korean_instruction_2.py", line 47, in regular_font
pdf.add_font('NotoSansKR-Regular.ttf','',regular_font_dir.name, uni=True)
File "/home/adminuser/venv/lib/python3.11/site-packages/fpdf/fpdf.py", line 1810, in add_font
raise ValueError(
ValueError: Unsupported font file extension: . add_font() used to accept .pkl file as input, but for security reasons this feature is deprecated since v2.5.1 and has been removed in v2.5.3.
If you don’t mind, could you help me out? Does tempfile makes the file in the temp directory into pkl file while using Streamlit?
Below is my code-
import streamlit as st
import requests
import tempfile
from fpdf import FPDF
from streamlit import session_state as sss
if st.button('try_1'):
def regular_font(arg):
if 'regular_font_dir' not in sss:
response_regular = requests.get("mygithubdir/NotoSansKR-Regular.ttf", stream=True)
regular_font_dir = tempfile.NamedTemporaryFile(delete=False)
with open(regular_font_dir.name, 'wb') as f:
for chunk in response_regular.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pdf.add_font('NotoSansKR-Regular.ttf','',regular_font_dir.name, uni=True)
pdf.set_font('NotoSansKR-Regular.ttf', '', arg)
sss.regular_font_dir=True
else:
pdf.add_font('NotoSansKR-Regular.ttf','',regular_font_dir.name, uni=True)
pdf.set_font('NotoSansKR-Regular.ttf', '', arg)
def thick_font(arg):
if 'thick_font_dir' not in sss:
response_thick = requests.get("mygithubdir/NotoSansKR-SemiBold.ttf", stream=True)
thick_font_dir = tempfile.NamedTemporaryFile(delete=False)
with open(thick_font_dir.name, 'wb') as f:
for chunk in response_regular.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
pdf.add_font('NotoSansKR-SemiBold.ttf','',thick_font_dir.name, uni=True)
pdf.set_font('NotoSansKR-SemiBold.ttf', '', arg)
sss.thick_font_dir=True
else:
pdf.add_font('NotoSansKR-SemiBold.ttf','',thick_font_dir.name, uni=True)
pdf.set_font('NotoSansKR-SemiBold.ttf', '', arg)
class PDF(FPDF):
def header(self):
regular_font(8)
# Title
self.cell(30, 10, '', 0, 0, 'C')
# Line break
self.ln(20)
# Page footer
def footer(self):
# Position at 1.5 cm from bottom
self.set_y(-15)
regular_font(8)
# Page number
self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
# Margin
m = 10
# Page width: Width of A4 is 210mm
pw = 210 - 2*m
# Cell height
ch = 50
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
thick_font(15)