【发布时间】:2021-05-26 02:10:58
【问题描述】:
当我在 docker 和 Node 中运行我的 phantomjs 应用程序时,它运行良好(将 HTML 转换为 Jpeg)。
但是,当我将它发布到 docker 容器时,字体名称不再受到尊重。
这个应用程序将 HTML 转换为 jpeg、pdf 或其他媒体,使用 html-convert npm,它是 phantomjs 的包装器
码头文件:
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);
});
邮递员(从 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 中运行它
有什么想法吗?
【问题讨论】:
-
您能根据您的应用程序的功能解释“字体名称不再受到尊重”吗?您是否遇到控制台错误?
-
@brandon-barnett 没有控制台。我只是无法让任何“字体系列”属性起作用。生成pdf或jpeg时只使用默认字体
-
@brandon-barnett,我添加了更多细节
-
你可以参考另一个 docker 镜像的 GitHub issue,也许这会有所帮助 github.com/astefanutti/decktape/issues/79 和 github.com/GoogleChrome/puppeteer/issues/2230 StackOverflow 上的类似问题 stackoverflow.com/questions/48694808/…
-
@HemantSingh,谢谢,这似乎是一个无法真正解决的问题。我切换到 Chrome/Puppeteer,我的 Comic Sans 看起来仍然与 Arial 相似..与 Phantom 的问题基本相同