https://github.com/sinclairzx81/phantom-network-service
run phantomjs as a network service
https://github.com/sinclairzx81/phantom-network-service
Last synced: about 1 year ago
JSON representation
run phantomjs as a network service
- Host: GitHub
- URL: https://github.com/sinclairzx81/phantom-network-service
- Owner: sinclairzx81
- License: mit
- Created: 2014-01-02T12:33:58.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-01-04T07:55:51.000Z (over 12 years ago)
- Last Synced: 2024-10-19T11:12:05.047Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 517 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
## phantom.net
For nodejs developers wanting to run [phantomjs](http://phantomjs.org/) as a network service. Includes both server and client library.
#### server
start a phantom.net service on port 5000.
```javascript
var phantom = require('phantom.net')
var server = new phantom.Server(5000)
```
#### client
connect to service and render web page as pdf.
```javascript
var phantom = require('phantom.net')
var client = new phantom.Client('http://localhost:5000')
var parameter = {url: 'http://google.com', mime: 'application/pdf'}
client.render(parameter, function(readstream) {
// do something with the stream.
})
```
### install
npm install phantom.net
note: phantomjs needs to be installed on the server machine, and set up as a PATH environment variable.
### contents
* [overview](#overview)
* [render](#render)
* [waiting](#waiting)
* [example](#example)
* [windows performance](#windows_performance)
phantom.net was written specifically for developers looking to expose phantomjs as a network service. The library allows developers to
quickly host phantomjs as a http accessible endpoint, and pass it urls and content to render. phantom.net will respond with readable streams.
Useful for writing results to disk, or back out as http response.
```javascript
var phantom = require('phantom.net')
var client = new phantom.Client('http://localhost:5000')
var parameter = {url: 'http://google.com', mime: 'application/pdf'}
client.render(parameter, function(readstream) {
var writestream = require('fs').createWriteStream('output.pdf')
readstream.pipe(writestream)
})
```
The client render() method accepts a single parameter which is passed to phantomjs for rendering. Below is the parameter definition. When passing
this parameter, either url or content must be set. The mime is required, and can be either 'application/pdf', image/jpg', 'image/png' or 'image/gif'
note: for more details on the following properties, see [here](https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#properties-list).
```typescript
interface Parameter {
content? : string
url? : string
mime : string
wait? : number
viewportSize? : {
width : number
height : number
}
paperSize? : {
width? : number
height? : number
border? : string
format? : string
orientation?: string
}
zoomFactor? : number
clipRect? : {
top : number
left : number
width : number
height: number
}
}
```
### waiting
By default, phantom.net will wait 200ms for a page to render. A client can modify this value to increase the allowed for the page
to load. The following will wait 2 seconds.
```javascript
var phantom = require('phantom.net')
var client = new phantom.Client('http://localhost:5000')
client.render({url: 'http://google.com', mime: 'application/pdf', wait: 2000}, function(readstream) {
})
```
By default, the phantom.net server will put limits on waiting (the default is 4 seconds), however you can override the maximum
wait as follows...
```javascript
var phantom = require('phantom.net')
var server = new phantom.Server(5000, {maximum_wait: 10000})
```
### example
The following example demonstrates setting up both a phantom.net server (on port 5001) and phantom.net client within the same process.
We also create a basic nodejs http server (on port 5000) to output the stream returned from phantom.net.
```javascript
var phantom = require('phantom.net')
var server = new phantom.Server(5001)
var client = new phantom.Client("http://localhost:5001")
require('http').createServer(function(req, res) {
var parameter = {url: 'http://google.com',
mime: 'application/pdf',
viewportSize: { width: 1600, height: 1200 } }
client.render(parameter, function(errors, stream) {
res.writeHead(200, {'Content-Type' : parameter.mime})
stream.pipe(res)
})
}).listen(5000)
console.log('server listening on port 5000')
If running the server on a windows machine, rendering may take a considerable amount of time. If this is a issue,
you can speed things up unchecking 'automatically detect settings' in internet explorers LAN settings, as follows...
* open up internet explorer.
* options > internet options > connections (tab).
* uncheck 'automatically detect settings'.
* click ok.