Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the 'webpage' Phantom module in node.js
    text
    copied!<p>I am trying to wrap a PhantomJS script in a node.js process. The phantom script grabs a url from the arguments provided on the command line and outputs a pdf (much similar to the rasterize.js example included with the pahntom install).</p> <p>The phantom script I have works fine, it's just my employer wants a node script if possible. No problem, I can use the <a href="https://github.com/alexscheelmeyer/">node-phantom</a> node module to wrap it.</p> <p>But now I've hit a stumbling block, my phantom script has:</p> <pre><code>var page = require('webpage').create(); </code></pre> <p>So, node.js is trying to find a module called 'webpage', the 'webpage' module is built into the phantom install so node can't find it. As far as I can tell, there is no npm module called 'webpage'.</p> <p>'webpage' is used like this:</p> <pre><code>page.open(address, function (status) { if (status !== 'success') { // --- Error opening the webpage --- console.log('Unable to load the address!'); } else { // --- Keep Looping Until Render Completes --- window.setTimeout(function () { page.render(output); phantom.exit(); }, 200); } }); </code></pre> <p>where address is the url specified on the command line and output is another argument, the name and type of the file.</p> <p>Can anyone help me out? This is quite an abstract one so I'm not expecting much if I'm honest, worth a try though.</p> <p>Thanks.</p> <p>EDIT - Approx 2hrs later</p> <p>I now have this which throws out a PDF:</p> <pre><code>var phanty = require('node-phantom'); var system = require('system'); phanty.create(function(err,phantom) { //var page = require('webpage').create(); var address; var output; var size; if (system.args.length &lt; 4 || system.args.length &gt; 6) { // --- Bad Input --- console.log('Wrong usage, you need to specify the BLAH BLAH BLAH'); phantom.exit(1); } else { phantom.createPage(function(err,page){ // --- Set Variables, Web Address, Output --- address = system.args[2]; output = system.args[3]; page.viewportSize = { width: 600, height: 600 }; // --- Set Variables, Web Address --- if (system.args.length &gt; 4 &amp;&amp; system.args[3].substr(-4) === ".pdf") { // --- PDF Specific --- size = system.args[4].split('*'); page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' } : { format: system.args[4], orientation: 'portrait', margin: '1cm' }; } // --- Zoom Factor (Should Never Be Set) --- if (system.args.length &gt; 5) { page.zoomFactor = system.args[5]; } else { page.zoomFactor = 1; } //---------------------------------------------------- page.open(address ,function(err,status){ if (status !== 'success') { // --- Error opening the webpage --- console.log('Unable to load the address!'); } else { // --- Keep Looping Until Render Completes --- process.nextTick(function () { page.render(output); phantom.exit(); }, 200); } }); }); } }); </code></pre> <p>But! It's not the right size! The page object created using the phantom 'webpage' create() function looks like this before it's passed the URL:</p> <p><img src="https://i.stack.imgur.com/LpSQB.png" alt="phantom returned page"></p> <p>Whereas mine in my node script, looks like this:</p> <p><img src="https://i.stack.imgur.com/jRFIJ.png" alt="my page"></p> <p>Is it possible to hard code the properties to achieve A4 formatting? What properties am I missing?</p> <p>I'm so close!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload