If Verdana.ttf can't be loaded, the font file pointed by the FALLBACK_FONT_PATH environment variable will be loaded. This fixes width computation errors that can occur with non-latin characters when running with Docker.
32 lines
755 B
JavaScript
32 lines
755 B
JavaScript
'use strict';
|
|
|
|
var path = require('path');
|
|
var PDFDocument = require('pdfkit');
|
|
var doc = new PDFDocument({size:'A4', layout:'landscape'});
|
|
|
|
// Attempt to use a particular font.
|
|
// callback: (optional) takes an error if it failed.
|
|
function loadFont(path, callback) {
|
|
try {
|
|
doc = doc.font(path);
|
|
if (callback) { callback(null); }
|
|
} catch(err) {
|
|
doc = doc.font('Helvetica-Bold');
|
|
if (callback) { callback(err); }
|
|
}
|
|
}
|
|
|
|
loadFont(path.join(__dirname, '..', 'Verdana.ttf'), function (err) {
|
|
if (err && process.env.FALLBACK_FONT_PATH) {
|
|
loadFont(process.env.FALLBACK_FONT_PATH);
|
|
}
|
|
});
|
|
doc = doc.fontSize(11);
|
|
|
|
function measure(str) {
|
|
return doc.widthOfString(str);
|
|
}
|
|
|
|
module.exports = measure;
|
|
module.exports.loadFont = loadFont;
|