jorisschellekens / borb

borb is a library for reading, creating and manipulating PDF files in python.
https://borbpdf.com/
Other
3.4k stars 147 forks source link

Set a global custom font #134

Closed stratosgear closed 2 years ago

stratosgear commented 2 years ago

Unless I am missing something, I have to always pass a font param in all paragraphs if I want my pdf to use something else except the default Helvetica font

Is there a way to globally set a font to be "courier" or even better, a specific custom google font, once, and all elements to use that font instead of Helvetica?

jorisschellekens commented 2 years ago

I would create a custom class that inherits from Paragraph and uses the Font you want. That seems like the easiest option given the current architecture.

Kind regards, Joris Schellekens

stratosgear commented 2 years ago

Sorry I am not familiar with the internal architecture of borb...

Your suggestion would only work if the only way to commit text to the PDF would be through the Paragraph entity. If there was, for (a really stupid) example, a set table title command, that did not use a Paragraph, then that text would use the still default Helvetica...?

So is the paragraph the only "entry point" for submitting text for a really global way of setting ALL text fonts used in the PDF?

Just making sure! Thanks, again!

jorisschellekens commented 2 years ago

To the best of my knowledge, Paragraph is only created inside borb on these occasions:

  1. As part of SmartArt
  2. When converting HTML to PDF
  3. When converting Markdown to PDF
  4. When applying OCR to a PDF
  5. When using the utility class TableUtil to create a Table

Those usecases would not use your modified version of Paragraph and would thus be stuck in Helvetica. Although for 2 and 3, you can easily pass a family of fonts to use.

Alternatively, you can overwrite the class method init on Paragraph dynamically at the start of your code.

In pseudocode:

old_init = Paragraph.__init__

def new_init():
    old_init
    set_font_to_courier

Paragraph.__init__ = new_init

Kind regards, Joris Schellekens

stratosgear commented 2 years ago

Thank you for your very detailed reply!

I will have to give it a try and I will let you know.

Thanks!