Phantom JS + Docker:从 HTML 转换时不考虑 html 字体系列
Phantom JS + Docker: html font-family is not respected when converting from HTML
当我在 docker 中 运行 我的 phantomjs 应用程序时,它在 Node 中工作正常(将 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
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(从 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
中完成此操作
有什么想法吗?
该字体不在 node:latest
图像中。您需要先安装 MS 字体系列。我无法将工作 Dockerfile 放在一起,但这些命令应该可以工作
wget http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.6_all.deb
dpkg -i ttf-mscorefonts-installer_3.6_all.deb
apt-get install -f -y
第二条命令抛出错误,导致 docker 映像构建失败。好吧,如果您找到忽略此错误的方法,您应该能够将命令包含在 Dockerfile a 中并使用 MS 字体构建它。
这就是我如何让我的应用程序与 Puppeteer 一起工作。
注意:我切换到 Chrome Headless,因为 PhantomJS 是一个带有 shady Lic 协议的恐龙,没有像样的支持
FROM node:latest
# See https://crbug.com/795759
RUN apt-get update && apt-get install -yq libgconf-2-4
# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
RUN apt-get update && apt-get install -y wget --no-install-recommends \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /src/*.deb
RUN echo "deb http://httpredir.debian.org/debian jessie main contrib" > /etc/apt/sources.list \
&& echo "deb http://security.debian.org/ jessie/updates main contrib" >> /etc/apt/sources.list \
&& echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \
&& apt-get update \
&& apt-get install -y ttf-mscorefonts-installer \
&& apt-get clean \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["node", "app.js" ]
EXPOSE 8081
查看下面的link解决方案。
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)
当我在 docker 中 运行 我的 phantomjs 应用程序时,它在 Node 中工作正常(将 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
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(从 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
中完成此操作有什么想法吗?
该字体不在 node:latest
图像中。您需要先安装 MS 字体系列。我无法将工作 Dockerfile 放在一起,但这些命令应该可以工作
wget http://ftp.de.debian.org/debian/pool/contrib/m/msttcorefonts/ttf-mscorefonts-installer_3.6_all.deb
dpkg -i ttf-mscorefonts-installer_3.6_all.deb
apt-get install -f -y
第二条命令抛出错误,导致 docker 映像构建失败。好吧,如果您找到忽略此错误的方法,您应该能够将命令包含在 Dockerfile a 中并使用 MS 字体构建它。
这就是我如何让我的应用程序与 Puppeteer 一起工作。
注意:我切换到 Chrome Headless,因为 PhantomJS 是一个带有 shady Lic 协议的恐龙,没有像样的支持
FROM node:latest
# See https://crbug.com/795759
RUN apt-get update && apt-get install -yq libgconf-2-4
# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
RUN apt-get update && apt-get install -y wget --no-install-recommends \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst ttf-freefont \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /src/*.deb
RUN echo "deb http://httpredir.debian.org/debian jessie main contrib" > /etc/apt/sources.list \
&& echo "deb http://security.debian.org/ jessie/updates main contrib" >> /etc/apt/sources.list \
&& echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \
&& apt-get update \
&& apt-get install -y ttf-mscorefonts-installer \
&& apt-get clean \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD ["node", "app.js" ]
EXPOSE 8081
查看下面的link解决方案。 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)