Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webshot does not render local images inside html

I'm using node-webshot and phantomjs-cli to render an html string to a png image. The html contains an image tag. The image is not rendered when it is the src attribute points to a local file and no error is raised. However it does render the image when the src points to a http location. I've tried all of the different file path combinations that I can think of, eg

<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..

but so far no luck. Interestingly it works fine on my colleague's machine which is a Mac but not on mine which is Windows, and I've tried with two different Windows machines with no success.

Here is some simplified code that focuses on the issue:

'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);      
});
*/

The commented out code block is an example of it working with a web link as the image source but I need it to work off of a local path.

Has anyone had this issue before an know how to solve it?

Thanks

like image 314
There is no spoon Avatar asked Jan 15 '18 10:01

There is no spoon


Video Answer


1 Answers

Local files should be accessible via the file: URI Scheme:

<img src="file:///C:/images/mushrooms.png">

Per the specification:

A file URL takes the form:

   file://<host>/<path>
like image 165
BlakeGru Avatar answered Oct 13 '22 00:10

BlakeGru