question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

running example code with node 8.11.2 get "Error: ENOENT: no such file or directory, open 'fonts/PalatinoBold.ttf"

See original GitHub issue

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)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

19reactions
dillonstreatorcommented, Sep 19, 2018

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!

0reactions
devsflkcommented, Feb 17, 2022

This solve my issue put your image like this

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve Error: ENOENT: no such file or directory
Run install your dependencies, clean your cache, and develop your project by: npm install; gatsby clean; gatsby develop. If the error ...
Read more >
palatino-Bold, palatino-Roman, palatino-Italic belongs to ...
URWPalatino is the Palatino clone shipped with TeX Live and MiKTeX. There are licensing problem in using the Adobe fonts. – egreg. Jan...
Read more >
SUSE Linux Enterprise Desktop - Ayuntamiento de Zaragoza
This guide is intended for use by professional network and system administrators during the actual planning, deployment, configuration, ...
Read more >
Palatino® Bold Italic | Fonts.com
Select Font Format; Add to Wish List. Try it; Waterfall; Character Map; OpenType; Technical Details. Desktop Web Font. Try this font now!
Read more >
SUSE Linux Enterprise Desktop 文档
Windows 网络SUSE Linux Enterprise Desktop 支持用Active Directory 作为. 鉴定源。它为您提供了安全而稳定的Linux 操作系统的一切优点,同时还提.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found