Phantom JS + Docker:从 HTML 转换时不考虑 html font-family

Las*_*nal 5 node.js phantomjs docker

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

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

码头档案:

FROM node:latest
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node app.js
EXPOSE 8081
Run Code Online (Sandbox Code Playgroud)

包.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": ""
}
Run Code Online (Sandbox Code Playgroud)

应用程序.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);
});
Run Code Online (Sandbox Code Playgroud)

邮递员(从 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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

docker build -t htmlconverter .
docker run -p 8081:8081 htmlconverter 
Run Code Online (Sandbox Code Playgroud)

我在 Windows 10 中运行它

有任何想法吗?

小智 0

该字体不在node:latest图像中。您需要先安装 MS 字体系列。我无法将工作 Dockerfile 放在一起,但这些命令应该可以工作

  1. wget http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.6_all.deb
  2. dpkg -i ttf-mscorefonts-installer_3.6_all.deb
  3. apt-get install -f -y

第二个命令抛出错误,导致 docker 镜像构建失败。如果您找到一种方法来忽略此错误,您应该能够将命令包含在 Dockerfile a 中并使用 MS 字体构建它。