Phantom.js – use custom fonts in HTML to PDF convert

Very short, but I think, may help a lot of people… Last time I spent a lot of time on the big issue with Phantom.js in Node app. Very common task: application should generate PDF files from input HTML code. Nothing difficult, yes? Yep, and it worked fine on my Node app… But one day, client said, that we should change font in document. Simple, we can just use font-face in input HTML styles, right? In theory, yes. Phantom.js and Node packages to use this library to convert HTML to PDF can use custom fonts and also other external assets like images.

With very popular html-pdf package we can use external, full link to assets, use absolute path to our local directory with files (using file:/// prefix) or specify base path in rendering options, and then use relative (to this base path) paths in our styles. And… it works fine. It also worked fine with custom fonts on my project. But only on local machine where I use MacOS. After deploying app to Debin, Ubuntu Server or CentOS server, PDFs are broken – with no selected text, but all pages are “images”, with many broken characters and very big results file (it isn’t strange if we have images, instead of text in PDF). Custom fonts have been used correctly, but PDF was broken, why?

It’s library and Phantom.js issue, but solution is very, very simple. We must install our custom fonts locally on production machine and do not attach them in CSS – just use as existing font-family! It’s because Phantom is external script and it can use system-wide fonts. Where should we place our files? There are directories. If you have issues with custom fonts and non-selectable text, just copy fonts to:

/usr/share/fonts
/usr/local/share/fonts/

Nconf – simple, great solution for Node.js app configuration

When I started work with Node.js and Express, I was looking for something to store my app settings. Something like env files in many PHP frameworks – central place for configuration, not stored in repository, but only for example when we run app in development or production environment. We can write custom code for that, or use some modules from npm, also directly for env files. Nuxt package also has dedicated env file module. These modules are great, but there is something much, much better – nconf.

Continue reading “Nconf – simple, great solution for Node.js app configuration”

Run Node.js app on Ubuntu Server using pm2

I work with Node.js and some other JavaScript technologies in last months. One important think is – how run ready application on production server? It wasn’t easy question, because earlier I used only development mode in IDE (with automatic rebuild and generation in background). After some research I found pm2 – great package to run Node.js application in background. With that manager we can start, stop or restart many applications. Let’s see how we can easily run Node.js app.

Continue reading “Run Node.js app on Ubuntu Server using pm2”

Vue.js – push data from nested childs to parent

Last time I work with Node and Vue.js. It’s very nice and allows to simplify many, many things. And it’s a lot better – for me – than React and its code mess… One of very nice features from Vue is components. We can separate code into small modules and then import and use them in another as blocks. We can also send some data to them, but direct data binding is only in one way – from parents to childs. According to official documentation, we should use events to notify parents about changes in childs and then make changes. But what about nested childs?

Continue reading “Vue.js – push data from nested childs to parent”

Express.js and Vue.js – secure forms using CSRF token

Yes it’s something new on this blog – not only PHP, but also Node.js and Vue.js because I work on new project and use these technologies. Many, many things are completly new for me, but some of them are not. Good example is forms security: we must prevent attackers to make CSRF attacks and use tokens. Simple to say and now, in PHP world, very simple in usage – most of frameworks, most of template systems already have build-in solutions. With Express.js and Vue.js we can use available node modules, but we must still remember about some things. This post is about how use CSRF in that connection.

Continue reading “Express.js and Vue.js – secure forms using CSRF token”