Last active
March 18, 2016 12:05
-
-
Save AdamNaj/e24930984da535359490 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Extracts the compressed file to to the specified directory. | |
.PARAMETER Item | |
Specifies the item to be exported as PDF/JPG/PNG. | |
.EXAMPLE | |
The following eports the Home page by piping an item into the cmdlet. | |
PS C:\> Get-Item "master:\content\home" | Export-Pdf | |
.EXAMPLE | |
The following eports the Home page by specifying its path. | |
PS C:\> Export-Pdf "master:\content\home" | |
.INPUT | |
You can pipe a string that contains a path to an existing archive file. | |
- System.String | |
.OUTPUT | |
- System.IO.FileInfo or System.IO.DirectoryInfo | |
.NOTES | |
Adam Najmanowicz | |
.LINK | |
http://phantomjs.org/screen-capture.html | |
#> | |
function Export-Pdf{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="Item")] | |
[ValidateNotNullOrEmpty()] | |
[Sitecore.Data.Items.Item]$Item, | |
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName="Path")] | |
[ValidateNotNullOrEmpty()] | |
[string]$Path, | |
[Parameter(Mandatory=$false, Position=1,ParameterSetName="Path")] | |
[string]$Language, | |
[Parameter(Mandatory=$false, Position=2)] | |
[string]$fileFormat = "pdf", | |
[Parameter(Mandatory=$false, Position=3)] | |
[int]$width = 960, | |
[Parameter(Mandatory=$false, Position=4)] | |
[int]$height = 480, | |
[Parameter(Mandatory=$false, Position=5)] | |
[string]$format = "A4", | |
[Parameter(Mandatory=$false, Position=6)] | |
$orientation = "portrait", | |
[Parameter(Mandatory=$false, Position=7)] | |
$zoomFactor = 100, | |
[Parameter(Mandatory=$false, Position=8)] | |
[Switch]$Interactive = $false | |
) | |
if(-not [string]::IsNullOrEmpty($Path)){ | |
if($Language -eq $null){ | |
$Item = Get-Item $Path | |
} else { | |
$Item = Get-Item $Path -Language $Language | |
} | |
} | |
$Interactive = $Interactive -and $host.PrivateData.Interactive | |
if (-not [Sitecore.Pipelines.HasPresentation.HasPresentationPipeline]::Run($Item)) { | |
if($interactive){ | |
Show-Alert "The item does not have presentation." | |
} else { | |
Write-Error "The item does not have presentation." | |
} | |
exit -1; | |
} | |
if($Interactive){ | |
$configuration = @{ | |
Parameters = @( | |
@{Name="fileFormat"; Title="<br/>Output format"; Options=@{"PDF"="pdf"; "JPG"="jpg"; "PNG"="png"}; Tab="Format"}; | |
@{Name="format"; Title="Page format"; Options=@{"A4"="A4"; "Letter"="Letter"; "Legal"="Legal"}; Columns="6"; Tab="PDF"}; | |
@{Name="zoomFactor"; Title="Zoom %"; Columns="6"; Tab="Format"}; | |
@{Name="maininfo"; Title="<br/><br/>The published version of the page will be exported. <br/>Make sure the page is published prior to exporting."; Tooltip="<br/>Page needs to be published"; Editor="info"; Tab="Format"}; | |
@{Name="orientation"; Title="Page orientation"; Options=@{"Portrait"="portrait";"Landscape"="landscape"}; Columns="6"; Tab="PDF"}; | |
@{Name="margin"; Title="Margin (in inches)"; Value="0.5"; Columns="6"; Tab="PDF"}; | |
@{Name="width"; Title="Width"; Columns="6"; Tab="Image"}; | |
@{Name="height"; Title="Height"; Columns="6"; Tab="Image"}; | |
) | |
Title = "Export Page" | |
Description = "Configure generator" | |
Width = 480 | |
Height = 480 | |
ShowHints = $false | |
} | |
$dialogStatus = Read-Variable @configuration | |
if ($dialogStatus -eq "cancel") { | |
exit -2; | |
} | |
} | |
$timeStamp = Get-Date -format "yyyy MMM d hh-mm" | |
$pdfPath = [Sitecore.IO.TempFolder]::GetFilename("$($Item.Name)-$($Item.Language.Name) - $timeStamp.$fileFormat") | |
$pdfPath = [Sitecore.IO.FileUtil]::MapPath($pdfPath); | |
Write-Verbose $pdfPath | |
$urlString = New-Object Sitecore.Text.UrlString @($SitecoreAuthority) | |
[Void]$urlString.Add("sc_mode", "normal"); | |
[Void]$urlString.Add("sc_itemid", $Item.ID); | |
[Void]$urlString.Add("sc_lang", $Item.Language); | |
$url = $urlString.ToString() | |
$js = @" | |
var system = require('system'); | |
var page = require('webpage').create(); | |
page.paperSize = { | |
format: '$format', | |
orientation: '$orientation', | |
margin: '$($margin)in' | |
}; | |
fileFormat = '$fileFormat'; | |
if(fileFormat === 'pdf'){ | |
page.viewportSize = { width: 1920, height: 1080 }; | |
} | |
if(fileFormat !== 'pdf'){ | |
page.viewportSize = { width: $width, height: $height }; | |
if('$height' === ''){ | |
page.clipRect = { top: 0, left: 0, width: $width, height: 0 }; | |
} else { | |
page.clipRect = { top: 0, left: 0, width: $width, height: $height }; | |
} | |
} | |
phantom.outputEncoding = "utf8"; | |
page.zoomFactor = $zoomFactor/100; | |
console.log('PhantomJS'); | |
console.log('URL: $url'); | |
console.log('Generated File: ' + system.args[1]); | |
console.log('Paper format: $format' ); | |
console.log('Orientation: $orientation'); | |
console.log('Width: $width'); | |
console.log('Height: $height'); | |
page.onResourceReceived = function(resource) { | |
if (resource.status === 500) { | |
console.log('Server returned HTTP status 500'); | |
phantom.exit(-1); | |
} | |
} | |
page.open('$url', function(status) { | |
window.setTimeout(function() { | |
if (status == 'fail') { | |
console.log('$url failed to open'); | |
phantom.exit(-1); | |
} | |
page.render(system.args[1]); | |
phantom.exit(); | |
}, 500); | |
}); | |
"@; | |
$jsPath = [System.IO.Path]::GetTempFileName(); | |
$jsPath = [Sitecore.IO.TempFolder]::GetFilename("$($Item.Name)-$jsPath-PDFGen.js") | |
$jsPath = [Sitecore.IO.FileUtil]::MapPath($jsPath); | |
$js | Set-Content -Path $jsPath | |
$phantomJs = New-Object System.Diagnostics.Process | |
$phantomJs.StartInfo.WorkingDirectory = [Sitecore.IO.FileUtil]::MapPath("/"); | |
$phantomJs.StartInfo.FileName = [Sitecore.ContentTesting.Configuration.Settings]::PhantomJsExecutablePath; | |
$phantomJs.StartInfo.Arguments = "$jsPath `"$pdfPath`"" | |
$phantomJs.StartInfo.CreateNoWindow = $true | |
$phantomJs.StartInfo.UseShellExecute = $false | |
$phantomJs.StartInfo.RedirectStandardOutput = $true | |
$phantomJs.StartInfo.RedirectStandardError = $true | |
Register-ObjectEvent -InputObject $phantomJs -EventName 'OutputDataReceived' -Action { | |
Write-Verbose "$($EventArgs.data)" -Verbose:$VerbosePreference | |
} | Out-Null | |
Register-ObjectEvent -InputObject $phantomJs -EventName 'ErrorDataReceived' -Action { | |
Write-Host "$($EventArgs.data)" -Verbose:$VerbosePreference | |
} | Out-Null | |
[Void]$phantomJs.Start() | |
$phantomJs.BeginOutputReadLine() | |
$phantomJs.BeginErrorReadLine() | |
[Void]$phantomJs.WaitForExit() | |
if($Interactive){ | |
Download-File -FullName $pdfPath -Width 450 | Out-Null | |
Remove-Item "$pdfPath" -Force -ErrorAction SilentlyContinue | |
} | |
Remove-Item "$jsPath" -Force -ErrorAction SilentlyContinue | |
return $pdfPath | |
} | |
Export-Pdf (Get-Item .) -Verbose #-Interactive | |
Close-Window |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment