Note: You are currently viewing documentation for Moodle 4.0. Up-to-date documentation for the latest stable version of Moodle may be available here: Add fonts for embedding.

Add fonts for embedding: Difference between revisions

From MoodleDocs
mNo edit summary
m (→‎Converting locally: clean up, typos fixed: is probably dependant on → is probably dependent on)
 
(11 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{Certificate}}
{{Certificate}}
Additional fonts can be included in Moodle and used by the Certificate module, embedding them in the PDF (when using an "embedded" certificate type).
Additional fonts can be included in Moodle and used by the Certificate and Custom certificate module, embedding them in the PDF.
== Before adding custom TCPDF fonts ==
The default location for fonts that are included with TCPDF is lib/tcpdf/fonts/. If PDF_CUSTOM_FONT_PATH exists, this directory will be used instead of lib/tcpdf/fonts/, the default location is $CFG->dataroot.'/fonts/'.


If you are adding custom fonts you need to copy all fonts from lib/tcpdf/fonts/ to your PDF_CUSTOM_FONT_PATH and then add the extra fonts. Alternatively you may download all TCPDF fonts from http://www.tcpdf.org/download.php and extract them to PDF_CUSTOM_FONT_PATH directory.
From /lib/tcpdf/fonts/readme_moodle.txt:
:This directory contains just selected set of original tcpdf fonts in order to keep the standard Moodle distribution lightweight. You may want to manually download tcpdf package and extract its fonts/ directory into your $CFG->dataroot/fonts/. In such case, pdflib.php will use this directory. In the future, we plan to have fonts downloadable in a same way as languages are. (TODO MDL-18663).
== Add a font from TTF file ==
== Add a font from TTF file ==
Assuming you have a font licensed for converting and embedding...
Assuming you have a font licensed for converting and embedding...
=== Using the "TCPDF Fonts" plugin ===
Released November 15, 2021, the [https://moodle.org/plugins/tool_tcpdffonts TCPDF Fonts] plugin allows for easily switching to the custom TCPDF font directory (dataroot/fonts) and maintaining custom TCPDF fonts. It accepts .ttf, .otf, or .zip files.
=== Using an online converter ===
You can upload TTF (or OTF) fonts to http://fonts.palettize.me/ or https://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf, and download converted versions.
Fonts created this way seem to handle accented characters better than converting locally.
=== Converting locally ===
Fonts created this way may not handle accented characters correctly; this is probably dependent on the specific software versions. If you encounter this problem, you might want to try using an online converter (above).


1. Copy the .ttf file to /lib/tcpdf/fonts
1. Copy the .ttf file to /lib/tcpdf/fonts
Line 9: Line 24:
2. Create a "convertfont.php" file in /lib/tcpdf/fonts (and ensure the web server has permissions to run it), containing:
2. Create a "convertfont.php" file in /lib/tcpdf/fonts (and ensure the web server has permissions to run it), containing:
<code php>
<code php>
<?php
include('../tcpdf.php');
include('../tcpdf.php');
$pdf = new TCPDF_FONTS('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf = new TCPDF_FONTS('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->addTTFfont('./MyFont.ttf', 'TrueTypeUnicode');
$pdf->addTTFfont('./MyFont.ttf', 'TrueTypeUnicode');
</code>
</code>
Change <em>MyFont.ttf</em> to the name of the .ttf file you copied to the folder.
3. View the file at http://''hostname''/lib/tcpdf/fonts/convertfont.php (it should just show a blank page).
3. View the file at http://''hostname''/lib/tcpdf/fonts/convertfont.php (it should just show a blank page).


4. Verify that new files have been created in /lib/tcpdf:
4. Verify that new files have been created in /lib/tcpdf/fonts:
* myfont.ctg.z
* myfont.ctg.z
* myfont.php
*myfont.php
* myfont.z
*myfont.z


See below for using the font in a certificate.


If that did not work, make sure you changed the .ttf filename in "convertfont.php" and then purge all caches (http://''hostname''/admin/purgecaches.php) and try again.


== Add all TCPDF fonts ==
See below for using the font in a certificate.
 
==Using a new font (mod_certificate plugin only) ==
From /lib/tcpdf/fonts/readme_moodle.txt:
This sections applies to the old mod_certificate plugin, in the other certificate plugins the fonts will automatically be listed in the select drop-down.
 
:This directory contains just selected set of original tcpdf fonts in order to keep the standard Moodle distribution lightweight. You may want to manually download tcpdf package and extract its fonts/ directory into your $CFG->dataroot/fonts/. In such case, pdflib.php will use this directory.  In the future, we plan to have fonts downloadable in a same way as languages are. (TODO MDL-18663).
 
== Using a new font ==


Once added, there are two ways to use the new font:
Once added, there are two ways to use the new font:
# Go to the Certificate module setting page and set it as the new Serif or Sans-serif font. This will apply to all standard certificates.
#Go to the Certificate module setting page and set it as the new Serif or Sans-serif font. This will apply to all standard certificates.
# Create a custom certificate type in /mod/certificate/type, and replace the $fontserif and $fontsans variables, as needed.
#Create a custom certificate type in /mod/certificate/type, and replace the $fontserif and $fontsans variables, as needed.
 
* Replacing font for the whole file:
* Replacing font for the whole file:
<code php>
<code php>
Line 41: Line 54:
$fontsans = 'myfont'; // Add new code.
$fontsans = 'myfont'; // Add new code.
</code>
</code>
 
*Replacing font for just the student name:
* Replacing font for just the student name:
<code php>
<code php>
//certificate_print_text($pdf, $x, $y + 36, 'C', $fontsans, '', 30, fullname($USER)); // Comment out old code
//certificate_print_text($pdf, $x, $y + 36, 'C', $fontsans, '', 30, fullname($USER)); // Comment out old code''


certificate_print_text($pdf, $x, $y + 36, 'C', 'myfont', '', 30, fullname($USER)); // Add new code.
certificate_print_text($pdf, $x, $y + 36, 'C', 'myfont', '', 30, fullname($USER)); // Add new code.''
</code>
</code>
[[Category:Certificate]]
[[Category:Certificate]]
[[es:Añadir fonts para incrustar]]

Latest revision as of 14:51, 9 June 2022

Additional fonts can be included in Moodle and used by the Certificate and Custom certificate module, embedding them in the PDF.

Before adding custom TCPDF fonts

The default location for fonts that are included with TCPDF is lib/tcpdf/fonts/. If PDF_CUSTOM_FONT_PATH exists, this directory will be used instead of lib/tcpdf/fonts/, the default location is $CFG->dataroot.'/fonts/'.

If you are adding custom fonts you need to copy all fonts from lib/tcpdf/fonts/ to your PDF_CUSTOM_FONT_PATH and then add the extra fonts. Alternatively you may download all TCPDF fonts from http://www.tcpdf.org/download.php and extract them to PDF_CUSTOM_FONT_PATH directory.

From /lib/tcpdf/fonts/readme_moodle.txt:

This directory contains just selected set of original tcpdf fonts in order to keep the standard Moodle distribution lightweight. You may want to manually download tcpdf package and extract its fonts/ directory into your $CFG->dataroot/fonts/. In such case, pdflib.php will use this directory. In the future, we plan to have fonts downloadable in a same way as languages are. (TODO MDL-18663).

Add a font from TTF file

Assuming you have a font licensed for converting and embedding...

Using the "TCPDF Fonts" plugin

Released November 15, 2021, the TCPDF Fonts plugin allows for easily switching to the custom TCPDF font directory (dataroot/fonts) and maintaining custom TCPDF fonts. It accepts .ttf, .otf, or .zip files.

Using an online converter

You can upload TTF (or OTF) fonts to http://fonts.palettize.me/ or https://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf, and download converted versions.

Fonts created this way seem to handle accented characters better than converting locally.

Converting locally

Fonts created this way may not handle accented characters correctly; this is probably dependent on the specific software versions. If you encounter this problem, you might want to try using an online converter (above).

1. Copy the .ttf file to /lib/tcpdf/fonts

2. Create a "convertfont.php" file in /lib/tcpdf/fonts (and ensure the web server has permissions to run it), containing: <?php include('../tcpdf.php'); $pdf = new TCPDF_FONTS('P', 'mm', 'A4', true, 'UTF-8', false); $pdf->addTTFfont('./MyFont.ttf', 'TrueTypeUnicode'); Change MyFont.ttf to the name of the .ttf file you copied to the folder.

3. View the file at http://hostname/lib/tcpdf/fonts/convertfont.php (it should just show a blank page).

4. Verify that new files have been created in /lib/tcpdf/fonts:

  • myfont.ctg.z
  • myfont.php
  • myfont.z


If that did not work, make sure you changed the .ttf filename in "convertfont.php" and then purge all caches (http://hostname/admin/purgecaches.php) and try again.

See below for using the font in a certificate.

Using a new font (mod_certificate plugin only)

This sections applies to the old mod_certificate plugin, in the other certificate plugins the fonts will automatically be listed in the select drop-down.

Once added, there are two ways to use the new font:

  1. Go to the Certificate module setting page and set it as the new Serif or Sans-serif font. This will apply to all standard certificates.
  2. Create a custom certificate type in /mod/certificate/type, and replace the $fontserif and $fontsans variables, as needed.
  • Replacing font for the whole file:

// $fontsans = get_config('certificate', 'fontsans'); // Comment out old code

$fontsans = 'myfont'; // Add new code.

  • Replacing font for just the student name:

//certificate_print_text($pdf, $x, $y + 36, 'C', $fontsans, , 30, fullname($USER)); // Comment out old code

certificate_print_text($pdf, $x, $y + 36, 'C', 'myfont', , 30, fullname($USER)); // Add new code.