Open GreoN5 opened 1 year ago
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.
I am not sure if this is related to this issue. I am trying to render wide characters (Kanji in UTF-8). It renders fine in doc.text, but in doc.table the wide characters are not rendered correctly. I think this problem may be caused by pdfkit-table.
import fs from 'fs'
import PDFDocument from 'pdfkit-table'
const doc = new PDFDocument({ size: 'A4' })
doc.pipe(fs.createWriteStream('output.pdf'))
doc.font('assets/mplus-1p-regular.ttf')
doc.fontSize(10)
// Wide characters are rendered correctly in doc.text
doc.text('ハローワールド', 50, 50)
// Wide characters are not rendered correctly in doc.table!
doc.table({
title: 'タイトル',
headers: [ '名前', '性別', 'Address' ],
rows: Array.from({ length: 10 }, (_, i) => [ `名前#${i}`, `性別#${i}`, `Address#${i}` ]),
})
doc.end()
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?