PDFDocument = require ('pdfkit');
const fs = require('fs');
// Create a document
doc = new PDFDocument
// Pipe its output somewhere, like to a file or HTTP response
// See below for browser usage
doc.pipe (fs.createWriteStream('output.pdf'))
// Embed a font, set the font size, and render some text
doc.font('fonts/PalatinoBold.ttf')
.fontSize(25)
.text('Some text with an embedded font!', 100, 100)
// Add another page
doc.addPage()
.fontSize(25)
.text('Here is some vector graphics...', 100, 100)
// Draw a triangle
doc.save()
.moveTo(100, 150)
.lineTo(100, 250)
.lineTo(200, 250)
.fill("//FF3300")
// Apply some transforms and render an SVG path with the 'even-odd' fill rule
doc.scale(0.6)
.translate(470, -380)
.path('M 250,75 L 323,301 131,161 369,161 177,301 z')
.fill('red', 'even-odd')
.restore()
// Add some text with annotations
doc.addPage()
.fillColor("blue")
.text('Here is a link!', 100, 100)
.underline(100, 100, 160, 27, "//0000FF")
.link(100, 100, 160, 27, 'http://google.com/')
// Finalize PDF file
doc.end()
trying to run the code from pdfkit site get the error
Error: ENOENT: no such file or directory, open 'fonts/PalatinoBold.ttf'
at Object.fs.openSync (fs.js:646:18)
at Object.fs.readFileSync (fs.js:551:33)
at Object.fontkit.openSync (/root/node_modules/fontkit/index.js:43:19)
at Function.PDFFont.open (/root/node_modules/pdfkit/js/font.js:14:24)
at PDFDocument.font (/root/node_modules/pdfkit/js/mixins/fonts.js:39:28)
at Object.<anonymous> (/root/nodeFiles/pdfCreator.js:15:5)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
It might be worth base 64 encoding the font fil and loading it that way as a work around maybe?
Same issue.
Hi all
I was just running into this same issue and believe I found the solution
After specifying the fonts path relative to __dirname, the font loaded.
e.g.
let fontpath = (__dirname+'/../fonts/TimesNewRoman.ttf');
doc.font(fontpath);
Hope this helps anyone having the same problem!
@DillonStreator thanks man it worked
@DillonStreator, didn't work for me... =(
My folders are like:
/fonts
......./TimesNewRoman.ttf
/entities
......../rect
............../ index.js
So I had to do :
let fontpath = (__dirname+'/../../fonts/TimesNewRoman.ttf');
doc.font(fontpath);
But the same issue happened..
what can I do?
Most helpful comment
Hi all
I was just running into this same issue and believe I found the solution
After specifying the fonts path relative to
__dirname, the font loaded.e.g.
Hope this helps anyone having the same problem!