我正在使用 node-webshot 和 phantomjs-cli 将 html 字符串呈现为 png 图像。 html 包含图像标签。当 src 属性指向本地文件且未引发错误时,不会呈现图像。但是,当 src 指向 http 位置时,它会渲染图像。我已经尝试了所有我能想到的不同文件路径组合,例如
<img src=images/mushrooms.png"/>
<img src=images//mushrooms.png"/>
<img src=c:\images\mushrooms.png"/>
<img src=c:\\images\\mushrooms.png"/>
<img src=file://c:\\images\\mushrooms.png"/>
<img src=file://images//mushrooms.png"/>
etc..
但到目前为止还没有运气。有趣的是,它在我同事的 Mac 机器上运行良好,但在我的 Windows 机器上却不行,我已经尝试过两台不同的 Windows 机器,但没有成功。
下面是一些针对该问题的简化代码:
'use strict'
const webshot = require('webshot');
console.log('generating');
webshot('<html><head></head><body><img src="c:\\images\\mushrooms.png"/></body></html>', 'output.png', {siteType: 'html'}, function(err) {
console.log(err);
});
/*
webshot('<html><head></head><body><img src="https://s10.postimg.org/pr6zy8249/mushrooms.png"/></body></html>', 'output.png', {siteType: 'html'}, function(err) {
console.log(err);
});
*/
注释掉的代码块是它使用 Web 链接作为图像源的示例,但我需要它在本地路径之外工作。
有没有人在知道如何解决之前遇到过这个问题?
谢谢
最佳答案
本地文件应该可以通过 file:
访问URI 方案:
<img src="file:///C:/images/mushrooms.png">
根据 specification :
A file URL takes the form:
file://<host>/<path>
关于javascript - webshot 不会在 html 中渲染本地镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260909/