PhantomJS create page from string

后端 未结 5 1394
忘掉有多难
忘掉有多难 2020-12-08 04:21

Is it possible to create a page from a string?

example:

html = \'blah blah blah\'

page.open(html,         


        
相关标签:
5条回答
  • 2020-12-08 04:50

    It's very simple, take a look at the colorwheel.js example.

    var page = require('webpage').create();
    page.content = '<html><body><p>Hello world</p></body></html>';
    

    That's all! Then you can manipulate the page, e.g. render it as an image.

    0 讨论(0)
  • 2020-12-08 04:54

    Looking at the phantomjs API, page.open requires a URL as the first argument, not an HTML string. This is why the what you tried does not work.

    However, one way that you might be able to achieve the effect of creating a page from a string is to host an empty "skeleton page," somewhere with a URL (could be localhost), and then include Javascript (using includeJs) into the empty page. The Javascript that you include into the blank page can use document.write("<p>blah blah blah</p>") to dynamically add content to the webpage.

    I've ever done this, but AFAIK this should work.

    Sample skeleton page:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head></head>
    <body></body>
    </html>
    
    0 讨论(0)
  • 2020-12-08 04:54

    To do this you need to set the page content to your string.

    phantom.create(function (ph) {
      ph.createPage(function (page) {
          page.set('viewportSize', {width:1440,height:900})
    
          //like this
          page.set('content', html);
    
          page.render(path_to_pdf, function() { 
            //now pdf is written to disk.
            ph.exit();
          });
      });
    });
    

    you need to use page.set() to set the html content.

    as per https://github.com/sgentle/phantomjs-node#functionality-details

    Properties can't be get/set directly.
    Instead use page.get('version', callback) or page.set('viewportSize', {width:640,height:480}), etc.

    Nested objects can be accessed by including dots in keys, such as page.set('settings.loadImages', false)

    0 讨论(0)
  • 2020-12-08 04:54

    I got the following to work in PhantomJS version 2.0.0. Whereas before, I was using page.open() to open a page from the filesystem and set a callback:

    page.open("bench.html", pageLoadCallback);
    

    Now, I accomplish the same thing from a string variable with the HTML page. The page.setContent() method requires a URL as the second argument, and this uses fs.absolute() to construct a file:// URL.

    page.onLoadFinished = pageLoadCallback;
    page.setContent(bench_str, "file://" + fs.absolute(".") + "/bench.html");
    
    0 讨论(0)
  • 2020-12-08 05:16

    Just wanted to mention I recently had a similar need and discovered that I could pass file:// style references as an URL param, so I dumped my HTML string into a local file then passed the full path to my capture script (django_screamshot) which basically uses casperjs and phantomjs + a capture.js script.

    Anyway it just works and its reasonably fast..

    0 讨论(0)
提交回复
热议问题