Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phantom JS + Docker: html font-family is not respected when converting from HTML

When I run my phantomjs app in docker, in Node, it works fine (converting HTML to Jpeg).
However, when I publish it to a docker container, the font names are no longer being respected.

This app converts HTML into jpeg, pdf or other media, using html-convert npm, which is a wrapper for phantomjs

dockerfile:

FROM node:latest
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node app.js
EXPOSE 8081

package.json

{
  "name": "htmlconverter",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "body-parser": "^1.18.2",
    "ent": "^2.2.0",
    "express": "^4.16.3",
    "generator-azuresfcontainer": "^1.0.0",
    "html-convert": "^2.1.7",
    "html-entities": "^1.2.1",
    "memorystream": "^0.3.1",
    "phantomjs": "*",
    "phantomjs-prebuilt": "*",
    "picture-tube": "^1.0.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": ""
}

app.js

var http = require("http");
var express = require('express');
var htmlConvert = require('html-convert');
var url = require('url');
var querystring = require('querystring');
var Readable = require('stream').Readable;
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/', function (req, res) {
    var html = unescape(req.body.html);
    var format = req.body.format;
    var orientation = req.body.orientation;
    var convert = htmlConvert({
        format: format,
        orientation: orientation
    });
    var s = new Readable();
    s._read = function noop() { };
    s.push(html);
    s.push(null);
    var result = s.pipe(convert());
    result.pipe(res);
});

var server = app.listen(8081, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log("Example app listening at http://%s:%s", host, port);
});

Postman (generate jpeg from html):

http://127.0.0.1:8081/

{
    "html": "<!DOCTYPE html><html><head><style>body {background-color: powderblue; font-family: 'Comic Sans MS';}h1   {color: blue;}p    {color: red;}</style></head><body><h1>This is a heading</h1><p>This is a paragraph.</p></body></html>",
    "format":"jpeg",
    "orientation":"Landscape"
}

Observe the "Comic Sans" font working when calling POST, after launching "node app.js"

Then run Docker, and observe default font being used:

docker build -t htmlconverter .
docker run -p 8081:8081 htmlconverter 

I am running this in Windows 10

Any ideas?

like image 714
LastTribunal Avatar asked Oct 16 '22 17:10

LastTribunal


1 Answers

Check the below link for solution. link

Please add the scripts to the Dockerfile:
RUN apk add fontconfig ttf-dejavu
RUN apk add --no-cache curl &&
cd /tmp && curl -Ls https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz | tar xz &&
cp -R lib lib64 / &&
cp -R usr/lib/x86_64-linux-gnu /usr/lib &&
cp -R usr/share /usr/share &&
cp -R etc/fonts /etc &&
apk del curl

I did it and it works well. (node:12.20-alpine3.12)
like image 172
MAHENDRA. A.R Avatar answered Nov 03 '22 03:11

MAHENDRA. A.R