py-pdf / fpdf2

Simple PDF generation for Python
https://py-pdf.github.io/fpdf2/
GNU Lesser General Public License v3.0
993 stars 227 forks source link

How can I use fonts without downloading or installing on my system? #1165

Closed atfot closed 1 week ago

atfot commented 1 month ago

Can I use my github repository or google drive as a directory of my font? Or can I just use googleapi call to use google fonts to it? Or does this library strictly needs fonts from the local directory?

Lucas-C commented 1 month ago

Hi @atfot

This library loads font files from the filesystem. If you want to use Google fonts, you will have to download them first, but this is probably not too difficult. You can for example clone this GitHub repository: https://github.com/google/fonts Or use this wrapper from Pypi: https://pypi.org/project/googlefonts-installer/ Or else simply download them with some Python code: https://gist.github.com/fedir/5098500

Does this answer clarify the situation for you? Do you have other requests?

atfot commented 1 month ago

Hi @atfot

This library loads font files from the filesystem. If you want to use Google fonts, you will have to download them first, but this is probably not too difficult. You can for example clone this GitHub repository: https://github.com/google/fonts Or use this wrapper from Pypi: https://pypi.org/project/googlefonts-installer/ Or else simply download them with some Python code: https://gist.github.com/fedir/5098500

Does this answer clarify the situation for you? Do you have other requests?

Hello @Lucas-C . Thank you for the fast apply. I really appreciate it. I don't know if it's because I'm really new to programming, but I have a little silly question. Can I download the ttf file to the temporary directory using tempfile library and make a pdf? Is it possible?

Lucas-C commented 1 month ago

Can I download the ttf file to the temporary directory using tempfile library and make a pdf? Is it possible?

That's not a silly question, and yes that is totally doable! You can combine tempfile with the excellent Pypi library requests package, or else the standard urllib.request module. There are example of doing so in this SO thread: https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3

atfot commented 1 month ago

Can I download the ttf file to the temporary directory using tempfile library and make a pdf? Is it possible?

That's not a silly question, and yes that is totally doable! You can combine tempfile with the excellent Pypi library requests package, or else the standard urllib.request module. There are example of doing so in this SO thread: https://stackoverflow.com/questions/7243750/download-file-from-web-in-python-3

@Lucas-C I think tempfile method doesn't work on this scenario. It shows TypeError: can only concatenate str (not "int") to str on the pdf.add_font(). I think it needs certain names made with only string and temporary names like e8dfghejg9k.ttf doesn't work in here. Do you have any other ideas?

Lucas-C commented 1 month ago

You edited your comment but originally you mentioned the detailed error (which was great!):

pdf.add_font('NotoSansKR-Regular.ttf','',regular_font_dir.name, uni=True)

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.

The problem is simply that you are not passing the right arguments to .add_font() 🙂

Try this instead:

pdf.add_font(fname='NotoSansKR-Regular.ttf')

Check our documentation for more details: https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.add_font

Lucas-C commented 1 month ago

Hi @atfot I hope you were able to do what you wanted. Unless you have other questions, we will probably close this issue soon.