├── .gitattributes ├── Convert-WebPage.ps1 ├── Invoke-PhantomJS.ps1 ├── PowerShellPhantomJS.psm1 ├── README.md ├── TryConvertWebPage-TestPDF.ps1 ├── TryConvertWebPage-TestPNG.ps1 ├── TryJSAsFile-Test.ps1 ├── TryJSAsString-Test.ps1 ├── capture.js ├── images ├── Thumbs.db └── TryConvertWebpageTest.gif ├── phantomjs.exe └── test.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /Convert-WebPage.ps1: -------------------------------------------------------------------------------- 1 | function Convert-WebPage { 2 | param( 3 | $Url, 4 | $OutFile, 5 | [Switch]$Show 6 | ) 7 | 8 | & $PhantomJSExe .\capture.js $Url $OutFile 9 | 10 | if($Show) { Invoke-Item $OutFile } 11 | } -------------------------------------------------------------------------------- /Invoke-PhantomJS.ps1: -------------------------------------------------------------------------------- 1 | function Invoke-PhantomJS 2 | { 3 | param( 4 | $JSFileName, 5 | $JavaScript, 6 | [Switch]$ConvertFromJson 7 | ) 8 | 9 | $DeleteFile = $false 10 | 11 | if($JavaScript) 12 | { 13 | $JSFileName = [System.IO.Path]::GetTempFileName() 14 | $JavaScript | Set-Content -Encoding Ascii $JSFileName 15 | $DeleteFile = $true 16 | } 17 | 18 | $result = & $PhantomJSExe $JSFileName 19 | 20 | if($DeleteFile) { del $JSFileName } 21 | 22 | if($ConvertFromJson) { return $result | ConvertFrom-Json } 23 | 24 | return $result 25 | } 26 | 27 | -------------------------------------------------------------------------------- /PowerShellPhantomJS.psm1: -------------------------------------------------------------------------------- 1 | $PhantomJSExe = Join-Path $PSScriptRoot phantomjs.exe 2 | 3 | . (Join-Path $PSScriptRoot Invoke-PhantomJS.ps1) 4 | . (Join-Path $PSScriptRoot Convert-WebPage.ps1) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | PowerShell and PhantomJS 2 | - 3 | PowerShell module for PhantomJS, a headless WebKit scriptable with a JavaScript API. 4 | 5 | Convert-WebPage 6 | - 7 | This PowerShell function captures a webpage as a screen shot, it takes a *Url* and an *OutFile*. Name your file with the *OutFile* parameter, supplying one of these extensions **PNG, GIF, PDF, JPG** and the page will be converted to that type. 8 | 9 | ![](https://raw.githubusercontent.com/dfinke/PowerShellPhantomJS/master/images/TryConvertWebpageTest.gif) 10 | -------------------------------------------------------------------------------- /TryConvertWebPage-TestPDF.ps1: -------------------------------------------------------------------------------- 1 | Import-Module .\PowerShellPhantomJS.psm1 -force 2 | 3 | Convert-WebPage -Url http://www.instagram.com -OutFile test.pdf -Show -------------------------------------------------------------------------------- /TryConvertWebPage-TestPNG.ps1: -------------------------------------------------------------------------------- 1 | Import-Module .\PowerShellPhantomJS.psm1 -force 2 | 3 | Convert-WebPage -Url http://www.instagram.com -OutFile test.png -Show -------------------------------------------------------------------------------- /TryJSAsFile-Test.ps1: -------------------------------------------------------------------------------- 1 | Import-Module .\PowerShellPhantomJS.psm1 -force 2 | 3 | Invoke-PhantomJS .\test.js -ConvertFromJson -------------------------------------------------------------------------------- /TryJSAsString-Test.ps1: -------------------------------------------------------------------------------- 1 | Import-Module .\PowerShellPhantomJS.psm1 -force 2 | 3 | Invoke-PhantomJS -ConvertFromJson -JavaScript @" 4 | var n = JSON.stringify(window.navigator); 5 | console.log(n); 6 | phantom.exit(); 7 | "@ -------------------------------------------------------------------------------- /capture.js: -------------------------------------------------------------------------------- 1 | var page = require('webpage').create(); 2 | var system = require('system'); 3 | var address = system.args[1]; 4 | var output = system.args[2]; 5 | 6 | page.open(address, function () { 7 | page.render(output); 8 | phantom.exit(); 9 | }); -------------------------------------------------------------------------------- /images/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfinke/PowerShellPhantomJS/2a3f8a71a45024275a3f16291a09e6c771bea3cf/images/Thumbs.db -------------------------------------------------------------------------------- /images/TryConvertWebpageTest.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfinke/PowerShellPhantomJS/2a3f8a71a45024275a3f16291a09e6c771bea3cf/images/TryConvertWebpageTest.gif -------------------------------------------------------------------------------- /phantomjs.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfinke/PowerShellPhantomJS/2a3f8a71a45024275a3f16291a09e6c771bea3cf/phantomjs.exe -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var n = JSON.stringify(window.navigator); 2 | console.log(n); 3 | phantom.exit(); 4 | --------------------------------------------------------------------------------