node.js - 幻影 JS + Docker : html font-family is not respected when converting from HTML

标签 node.js docker phantomjs

当我在 Node 中的 docker 中运行我的 phantomjs 应用程序时,它工作正常(将 HTML 转换为 Jpeg)。
但是,当我将它发布到 docker 容器时,字体名称不再受到尊重。

此应用程序使用 html-convert npm 将 HTML 转换为 jpeg、pdf 或其他媒体,它是 phantomjs 的包装器

docker 文件:

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

包.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": ""
}

应用程序.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(从 html 生成 jpeg):

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"
}

在启动“node app.js”后,观察调用 POST 时“Comic Sans”字体的工作情况

然后运行 ​​Docker,观察使用的默认字体:

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

我在 Windows 10 中运行这个

有什么想法吗?

最佳答案

检查以下链接以获取解决方案。 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)

关于node.js - 幻影 JS + Docker : html font-family is not respected when converting from HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647872/

相关文章:

google-app-engine - 在 Google App Engine 中使用 Docker 组合

docker - 重新启动后 Direct-LVM 停止工作

javascript - 如何从CasperJS操作PhantomJS的 "page"对象?

javascript - 如何使用phantomjs截取整个页面

javascript - 在 RxJS 6 中取消订阅?

javascript - Socket.IO 客户端在回来时接收离线消息

docker - 当用户 namespace 与SELinux一起使用时,Docker运行会引发错误

node.js - 如何循环遍历链接并为 PhantomJS 中的每个站点执行 jQuery javascript

javascript - 如何在我的 VPS 上启动 Nodejs RESTful 服务?

ruby-on-rails - 在 Node.JS 中创建管理面板的最佳方法