Phantom JS + Docker:从HTML转换时不尊重html字体家族 [英] Phantom JS + Docker: html font-family is not respected when converting from HTML

查看:84
本文介绍了Phantom JS + Docker:从HTML转换时不尊重html字体家族的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

dockerfile:

dockerfile:

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

package.json

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);
});

邮递员(从html生成jpeg):

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

启动"node app.js"后,在调用POST时观察"Comic Sans"字体是否正常工作

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

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

Then run Docker, and observe default font being used:

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

我正在Windows 10中运行它

I am running this in Windows 10

有什么想法吗?

推荐答案

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

That font is not in the node:latest image. You need to install MS font family first. I wasn't able to put together working Dockerfile, but these commands should work

  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
  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字体进行构建.

The second command throws error, which causes failing docker image build. Well if you find a way to ignore this error, you should be able to include the commands in the Dockerfile a and build it with MS fonts.

这篇关于Phantom JS + Docker:从HTML转换时不尊重html字体家族的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆