2012-08-11 15 views
22

मैं व्यक्तिगत HTML तत्वों को Phantom.JS का उपयोग करके पीएनजी में प्रस्तुत करना चाहता हूं। यदि संभव है तो किस प्रकार, क्या कोई जानता है? साथ ही, मैं एक पृष्ठ प्रस्तुत करने के लिए Phantom.js का उपयोग कैसे करूं जो उपयोगकर्ता पहले से देख रहा है?फ़ैंटॉमजेएस के साथ किसी पृष्ठ का हिस्सा कैसे प्रस्तुत करें?

उत्तर

50

केवल एक पृष्ठ का हिस्सा प्रस्तुत करने के लिए आपको पृष्ठ के लिए clipRect विशेषता सेट करने और फिर इसे प्रस्तुत करने की आवश्यकता है।

var clipRect = document.querySelector(selector).getBoundingClientRect(); 
page.clipRect = { 
    top: clipRect.top, 
    left: clipRect.left, 
    width: clipRect.width, 
    height: clipRect.height 
}; 
page.render('capture.png'); 

मुझे आपके प्रश्न के दूसरे भाग को समझ में नहीं आता है। Phantom.js हेडलेस अर्थ है कि कोई वास्तविक प्रदर्शन नहीं है जिसे उपयोगकर्ता देख रहा है।

+0

मैं समझता हूँ कि phantom.js नेतृत्वहीन है। विशेष रूप से मुझे जो चाहिए वह ब्राउजर व्यूपोर्ट में दिखाई देने वाली वेबपृष्ठ को कैप्चर करने की क्षमता है। क्या आप ऐसा करने के तरीके के बारे में जानते हैं? –

+0

ठीक है, मैं समझता हूं कि आपका क्या मतलब है। मैं निश्चित रूप से नहीं जानता कि फैंटॉमजेएस इस तरह से काम करता है या नहीं। मुझे नहीं पता कि फैंटॉम के पास व्यूपोर्ट या उसके जैसा कुछ भी है। – jasonlfunk

+1

मैं देखता हूं। मैं कुछ प्रकार के ढांचे की तलाश में हूं जो क्रोम एक्सटेंशन एपीआई के अंदर या कुछ फ़ंक्शन करता है जो मुझे ऐसा करने की अनुमति देता है। यदि आपके पास कोई पॉइंटर्स है, तो मैं इसकी सराहना करता हूं। –

-5

मैं एक ही जरूरत थी, मैं इस कोशिश की और यह मेरे लिए ठीक काम किया:

मत भूलना यूआरएल

var page = require('webpage').create(); 
page.open('YourPageURL', function (status) { 

if (status !== 'success') { 
    console.log('Network Problem'); 
} else { 
    var p = page.evaluate(function() { 
     return document.getElementById('yourDivID').innerHTML 
    }); 
    console.log(p); 
} 
phantom.exit(); 
}); 
+2

वोट दिया गया क्योंकि यह वास्तव में प्रश्न में बताए गए अनुसार "व्यक्तिगत HTML तत्वों को पीएनजी में" स्क्रीनशॉट सहेजता नहीं है। यह केवल कुछ तत्वों के आंतरिक HTML को कंसोल करने के लिए प्रिंट करता है। –

5

आप CasperJS उपयोग कर सकते हैं में http://www, एक अन्य परियोजना के आधार पर PhantomJS।

casper.start('http://www.weather.com/', function() { 
    this.captureSelector('weather.png', '#wx-main'); 
}); 

casper.run(); 
21

कार्य उदाहरण।

var page = require('webpage').create(); 

page.open('http://google.com', function() { 
    // being the actual size of the headless browser 
    page.viewportSize = { width: 1440, height: 900 }; 

    var clipRect = page.evaluate(function(){ 
    return document.querySelector('#hplogo').getBoundingClientRect(); 
    }); 

    page.clipRect = { 
    top: clipRect.top, 
    left: clipRect.left, 
    width: clipRect.width, 
    height: clipRect.height 
    }; 

    page.render('google.png'); 
    phantom.exit(); 
}); 
0

नीचे दिया गया समाधान मेरे लिए काम करता है।

var clipRect = document.querySelector(selector).getBoundingClientRect(); 
    page.clipRect = { 
       top: clipRect.top, 
       left: clipRect.left, 
       width: clipRect.width, 
       height: clipRect.height 
     }; 
    page.render('capture.png'); 

लेकिन मुझे लगता है कि यह केवल तभी काम करता है जब हम एक छवि को पीडीएफ नहीं दे रहे हैं। पीडीएफ के लिए इस wokaround करने के लिए, इस लिंक मदद मिल सकती है इस

page.evaluate(function (element){ 
    $(element).appendTo('body'); 
    $('body > :not(' + element + ')').hide(); 
    }, element);  
    }); 

window.setTimeout(function(){ 
    page.render("page.pdf"); 
    },1000); 

कोशिश: How to hide all elements except one using jquery?

https://github.com/ariya/phantomjs/issues/10465

संबंधित मुद्दे