Introduction
We create many lists in Sharepoint. So it would be better if there is a simple way to convert those lists into PDF format. By doing this, we can save storage space too.
What is wkhtmltopdf.exe??
wkhtmltopdf is a command line tool to render HTML into PDF. These run entirely "headless" and do not require a display or display service.
You can download this from http://wkhtmltopdf.org. Make sure that you are downloading the right version. This trick is for Windows.
Using the Code
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$web = (New-Object Microsoft.SharePoint.SPSite("Site URL")).OpenWeb()
$lists = $web.Lists["List Name"]
$count=0
foreach($item in $lists.Items)
{
$pagename= $item.File.Name -replace 'aspx','pdf'
$pageurl= $web.url +"/"+ $item.File.url
$param= '--username <username> --password <password> '+ '
"' +$pageurl + '" ' + 'F:\FoldeName\'+$pagename
start-Process -FilePath 'F:\wkhtmltopdf.exe' -ArgumentList $param
$count=$count+1
$pageurl+' Pagenumber:'+$count
[System.Threading.Thread]::Sleep(10000)
}
$count
Open Notepad with the above lines of code and save with .PS1 extension (let me consider as filename.PS1). In the given code, wkhtmltopdf.exe is downloaded and saved in F Folder. The output will be stored in F drive inside FolderName folder.
I introduced a $count
variable. This is a simple way to ensure the total number of PDF created and later you can manually check to ensure all the items are converted to PDF format or not.
I have also introduced sufficient amount of Sleep to ensure the optimal performance.
How to Run the Sharepoint Management Shell Scripts ??
Open Sharepoint Management Shell and navigate to the folder where you have saved the Notepad file ( filename.PS1 ). Enter the following command:
.\filename.PS1
Now the conversion process starts.
Enjoy it.