PhantomJS create page from string

后端 未结 5 1392
忘掉有多难
忘掉有多难 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: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)

提交回复
热议问题