javascript - webshot 不会在 html 中渲染本地镜像

标签 javascript html node.js phantomjs

我正在使用 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/

相关文章:

丹麦语字符串的 JavaScript UTF-8 编码问题

html - ionic 4, Angular : problems showing OK and Cancel buttons with <ion-datetime>, < ionic 选择>

node.js - Express无法加载索引

javascript - Kendo grid 根据列数据进行列对齐

javascript - Highcharts - HTML 工具提示和数据标签呈现问题

javascript - 在日期选择器 JavaScript 中显示下拉菜单指示的月份

Javascript 新对象字段未保存

node.js - Ng 服务返回 "Object prototype may only be an Object or null: undefined TypeError: Object prototype may only be an Object or null: undefined"

javascript - 我正在为 javascript 和 zoom 而苦苦挣扎

html - 下拉菜单全部左对齐