Phantom-node module unable to load external resour

2019-05-11 05:09发布

问题:

i'm working on a nodejs server which renders posted html to pdf,png or jpg. ( https://github.com/svenhornberg/pagetox (server.js) if you want to try it)

It is working really good, renders complex sites but only to that point that i want do load a simple image. For example i am sending following code to my server:

<!doctype html>
<html>
<head>
  <title>logo</title>
</head>
<body>
  <img alt="logo" src="http://upload.wikimedia.org/wikipedia/commons/d/de/Wikipedia_Logo_1.0.png">
</body>
</html>

The Code should be okay. But the rendered response image does not contain the logo image. As said in the phantomjs documentation (http://phantomjs.org/api/webpage/property/settings.html) localToRemoteUrlAccessEnabled is set to false, so set it to true like said in the documentation of phantom-node (https://github.com/sgentle/phantomjs-node/):

  page.set('settings.localToRemoteUrlAccessEnabled', true);
  page.set('settings.loadImages', true);

to prove it is set to true, i called a getter which returns true

  page.get('settings.localToRemoteUrlAccessEnabled', function(data) { console.log(data);});

i'm using node v0.10.26 with phantomjs-node (0.6.1), phantomjs(1.9.7) on linux

Now the question, does anybody see a mistake ? Or can give me a hint what i am doing wrong. Or any other possibilities why phantomjs/phtanom-node isnt loading any external images

I am using the render method to generate the png, jpg or pdf file. I am calling the method inside the evaluate function as a callback, so the site should be loaded completely. (the full code is at https://github.com/svenhornberg/pagetox/blob/master/server.js)

Edit1: I'm not behind a proxy, which maybe blocks the requests.

回答1:

looks like i need to set a timeout and wait that all external resources are loaded.

page.set('content') dosen't work with dynamic content in phantomjs

i got it working, but a timeout is not what i want. If i find any better way i will post it.



回答2:

What you want to set is

page.set('settings.webSecurityEnabled', false);

Which enables cross domain requests. The other option localToRemoteUrlAccessEnabled does not affect your case because you don't use file: resources but you rather overwrite about:blank.

Other issues might be SSL/TLS related. See my answer here for more help.