Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom font #64

Open
GreoN5 opened this issue Jan 25, 2023 · 2 comments
Open

Custom font #64

GreoN5 opened this issue Jan 25, 2023 · 2 comments

Comments

@GreoN5
Copy link

GreoN5 commented Jan 25, 2023

Is there anything specific when adding a custom path to a font?

f.e. when I use this code snippet:
document.font(path to font)
my pdf does not seem to accept the font, but when I do add the one from the docs:
document.font("Helvetica-Bold")
it works fine.

Anyone have the same problem?

@LuckyForce
Copy link

As taken from the PDFKit Guide:
fyi this question is a PDFKit Question in general and not for the PDFKit-Table Plugin in specific.

Fonts
The PDF format defines 14 standard fonts that can be used in PDF documents. PDFKit
supports each of them out of the box. Besides Symbol and Zapf Dingbats this includes 4
styles (regular, bold, italic/oblique, bold+italic) of Helvetica, Courier, and Times. To switch
between standard fonts, call the font method with the corresponding Label:
'Courier'
'Courier-Bold'
'Courier-Oblique'
'Courier-BoldOblique'
'Helvetica'
'Helvetica-Bold'
'Helvetica-Oblique'
'Helvetica-BoldOblique'
'Symbol'
'Times-Roman'
'Times-Bold'
'Times-Italic'
'Times-BoldItalic'
'ZapfDingbats'
The PDF format also allows fonts to be embedded right in the document. PDFKit supports
embedding TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collection (.ttc),
and Datafork TrueType (.dfont) fonts.
To change the font used to render text, just call the font method. If you are using a standard
PDF font, just pass the name to the font method. Otherwise, pass the path to the font file, or
a Buffer containing the font data. If the font is a collection font (.ttc and .dfont files),
meaning that it contains multiple styles in the same file, you should pass the name of the
style to be extracted from the collection.
Here is an example showing how to set the font in each case.
// Set the font size
doc.fontSize(18);
// Using a standard PDF font
doc.font('Times-Roman')
.text('Hello from Times Roman!')
.moveDown(0.5);
// Using a TrueType font (.ttf)
doc.font('fonts/GoodDog.ttf')
.text('This is Good Dog!')
.moveDown(0.5);
// Using a collection font (.ttc or .dfont)
doc.font('fonts/Chalkboard.ttc', 'Chalkboard-Bold')
.text('This is Chalkboard, not Comic Sans.');
The output of this example looks like this:
Hello from Times Roman!
This is Good Dog!
This is Chalkboard, not Comic Sans.
Another nice feature of the PDFKit font support, is the ability to register a font file under a
name for use later rather than entering the path to the font every time you want to use it.
// Register a font
doc.registerFont('Heading Font', 'fonts/Chalkboard.ttc', 'Chalkboard-Bold');
// Use the font later
doc.font('Heading Font')
.text('This is a heading.');
That's about all there is too it for text in PDFKit. Let's move on now to images.

@vevlins
Copy link

vevlins commented Sep 4, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants