Generate a PDF using node, pdfkit and fontawesome?

1.7k views Asked by At

I'm running a node server and wanted to add some icons to a dynamically created PDF using PDFkit - so I installed fontawesome npm install fontawesome and required it in the .js file creating the pdf var fa = require('fontawesome');

I then tried to use the icons with PDFKit:

doc.fontSize(40).fill('black').text(fa('bath'), 160, 80);

But, I don't see a bath icon - just two jibberish symbols in its place.

What do I have to do to render the fa icons? This is a server-side script without a front end, and is run using a cron that pings a oracle view for dynamic information as part of a daily email. (So, I'm not using any css or html to create the document)

Thanks for your help!

1

There are 1 answers

1
Niels van Rijn On

Try adding the fontawesome font and copy pasting the icon from fontawesome

var PDFDocument = require('pdfkit');
var doc = new PDFDocument();

doc.registerFont('Fontawesome', 'fonts/fontawesome-solid-900.ttf')

doc.font('Fontawesome')
   .fontSize(20)
   .text('', 10, 10);

doc.write('out.pdf');

You can copy paste font icons from here https://fontawesome.com/cheatsheet