Hello, Daniel and everyone!
I have resumed the activity of implementing AddFont and also _loadfont to allow the ability to add other fonts to FPDF (Pascal). For this purpose, I have been studying the implementation in PHP, but I have noticed that they use the MakeFont script to convert the font file to another file with a .php extension, as shown below:
https://www.fpdf.org/en/doc/addfont.htm
https://stackoverflow.com/questions/13361900/add-font-on-pdf-report-with-fpdf
https://www.fpdf.org/makefont/index.php (online converter, available on the project website)
https://www.fpdf.org/en/tutorial/tuto7.htm
https://github.com/Setasign/FPDF/tree/master/makefont
When we download the code directly from the FPDF/PHP website (fpdf.org), the MakeFont code (font file converter) comes with it, and it is perfectly possible to translate it to Pascal, but it would be inconvenient for developers who use FPDF/Pascal to first use the converter so they can incorporate their fonts into their projects, having to distribute the converted file together with their application, since the library generates the PDF file on the fly.
So, I leave a question: does the leadership of this project want to implement it in the same way, using a converter beforehand to extract the information needed to add the font and maintain fidelity to the initial PHP project? Or use another approach like fcl-pdf or the Python implementation of FPDF, which apparently does not use the converter like the PHP version. Of course, regardless of the approach, the code should be implemented with FPC/Delphi compatibility in mind.
For reference - Python implementation:
https://pyfpdf.readthedocs.io/en/latest/reference/add_font/index.html
https://github.com/reingart/pyfpdf/tree/master
For reference - fcl-pdf implementation:
To add fonts to an fcl-pdf document, you'll need to embed them within the PDF. This involves using the gTTFontCache to locate fonts and then adding them to the TPDFDocument. You'll also need to specify the font directory and ensure the fonts are accessible to the PDF generation process.
Here's a breakdown of the process:
-
Include the fpttf unit: Ensure the fpttf unit is included in your application.
-
Initialize the Font Cache: The gTTFontCache is global and needs initialization. Use gTTFontCache.ReadStandardFonts; to load standard fonts.
You can also add custom font directories using gTTFontCache.SearchPath.Add(), for example: gTTFontCache.SearchPath.Add('/usr/share/fonts/');.
-
Add the Font to the Document: Create a TPDFDocument instance and set its FontDirectory property to the location of your font files (e.g., C:\Windows\Fonts).
Use the AddFont() method to add the desired font, providing the font file path and the font name (e.g., FontID := Document.AddFont('arial.ttf', 'Arial');).
-
Set Font Properties: After adding the font, use Page.SetFont(FontID, fontSize) to apply it to a specific text element on a page.
-
Embed the Font: Ensure that you are embedding the fonts. This is typically done by adding the poNoEmbeddedFonts flag to the document options. Without embedding, the font may not be displayed correctly if the recipient doesn't have the font installed.
Example Code Snippet:
procedure TMainForm.TestButtonClick(Sender: TObject);
var
FontID: Integer;
Document: TPDFDocument;
Section: TPDFSection;
Page: TPDFPage;
begin
Document := TPDFDocument.Create(nil);
Document.FontDirectory := 'C:\Windows\Fonts';
Document.StartDocument;
FontID := Document.AddFont('arial.ttf', 'Arial');
// Optionally, embed fonts (e.g., Document.Options := Document.Options + [poNoEmbeddedFonts])
Section := Document.Sections.AddSection;
Page := Document.Pages.AddPage;
Section.AddPage(Page);
Page.SetFont(FontID, 11);
Page.WriteText(20, 20, 'This is some text using Arial');
Document.SaveToFile('output.pdf');
end;
I send my best regards to everyone, stating that I do not wish to hinder anyone's efforts, I would just like to help in the implementation of this functionality.
Hello, Daniel and everyone!
I have resumed the activity of implementing AddFont and also _loadfont to allow the ability to add other fonts to FPDF (Pascal). For this purpose, I have been studying the implementation in PHP, but I have noticed that they use the MakeFont script to convert the font file to another file with a .php extension, as shown below:
https://www.fpdf.org/en/doc/addfont.htm
https://stackoverflow.com/questions/13361900/add-font-on-pdf-report-with-fpdf
https://www.fpdf.org/makefont/index.php (online converter, available on the project website)
https://www.fpdf.org/en/tutorial/tuto7.htm
https://github.com/Setasign/FPDF/tree/master/makefont
When we download the code directly from the FPDF/PHP website (fpdf.org), the MakeFont code (font file converter) comes with it, and it is perfectly possible to translate it to Pascal, but it would be inconvenient for developers who use FPDF/Pascal to first use the converter so they can incorporate their fonts into their projects, having to distribute the converted file together with their application, since the library generates the PDF file on the fly.
So, I leave a question: does the leadership of this project want to implement it in the same way, using a converter beforehand to extract the information needed to add the font and maintain fidelity to the initial PHP project? Or use another approach like fcl-pdf or the Python implementation of FPDF, which apparently does not use the converter like the PHP version. Of course, regardless of the approach, the code should be implemented with FPC/Delphi compatibility in mind.
For reference - Python implementation:
https://pyfpdf.readthedocs.io/en/latest/reference/add_font/index.html
https://github.com/reingart/pyfpdf/tree/master
For reference - fcl-pdf implementation:
To add fonts to an fcl-pdf document, you'll need to embed them within the PDF. This involves using the gTTFontCache to locate fonts and then adding them to the TPDFDocument. You'll also need to specify the font directory and ensure the fonts are accessible to the PDF generation process.
Here's a breakdown of the process:
Include the fpttf unit: Ensure the fpttf unit is included in your application.
Initialize the Font Cache: The gTTFontCache is global and needs initialization. Use gTTFontCache.ReadStandardFonts; to load standard fonts.
You can also add custom font directories using gTTFontCache.SearchPath.Add(), for example: gTTFontCache.SearchPath.Add('/usr/share/fonts/');.
Add the Font to the Document: Create a TPDFDocument instance and set its FontDirectory property to the location of your font files (e.g., C:\Windows\Fonts).
Use the AddFont() method to add the desired font, providing the font file path and the font name (e.g., FontID := Document.AddFont('arial.ttf', 'Arial');).
Set Font Properties: After adding the font, use Page.SetFont(FontID, fontSize) to apply it to a specific text element on a page.
Embed the Font: Ensure that you are embedding the fonts. This is typically done by adding the poNoEmbeddedFonts flag to the document options. Without embedding, the font may not be displayed correctly if the recipient doesn't have the font installed.
Example Code Snippet:
procedure TMainForm.TestButtonClick(Sender: TObject);
var
FontID: Integer;
Document: TPDFDocument;
Section: TPDFSection;
Page: TPDFPage;
begin
Document := TPDFDocument.Create(nil);
Document.FontDirectory := 'C:\Windows\Fonts';
Document.StartDocument;
FontID := Document.AddFont('arial.ttf', 'Arial');
// Optionally, embed fonts (e.g., Document.Options := Document.Options + [poNoEmbeddedFonts])
Section := Document.Sections.AddSection;
Page := Document.Pages.AddPage;
Section.AddPage(Page);
Page.SetFont(FontID, 11);
Page.WriteText(20, 20, 'This is some text using Arial');
Document.SaveToFile('output.pdf');
end;
I send my best regards to everyone, stating that I do not wish to hinder anyone's efforts, I would just like to help in the implementation of this functionality.