Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Valen558 opened this issue Jun 4, 2018 · 7 comments

Comments

@Valen558
Copy link

Valen558 commented Jun 4, 2018


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)
@Valen558 Valen558 changed the title run example code with node 8.11.2 get "Error: ENOENT: no such file or directory, open 'fonts/PalatinoBold.ttf" running example code with node 8.11.2 get "Error: ENOENT: no such file or directory, open 'fonts/PalatinoBold.ttf" Jun 4, 2018
@darkliahos
Copy link

It might be worth base 64 encoding the font fil and loading it that way as a work around maybe?

@Aman40
Copy link

Aman40 commented Aug 5, 2018

Same issue.

@dillonstreator
Copy link

dillonstreator commented 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!

@manoj10296
Copy link

@dillonstreator thanks man it worked

@paulomoisessilva
Copy link

paulomoisessilva commented Sep 18, 2020

@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?

@nelsonBlack
Copy link

/../../

remove the above trailing slashes and dots in path

@devsflk
Copy link

devsflk commented Feb 17, 2022

This solve my issue put your image like this

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants