Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint / SharePoint2010

HTML to PDF Conversion using Sharepoint Management Shell 2010

0.00/5 (No votes)
12 Mar 2014CPOL1 min read 10.1K  
HTML to PDF Conversion using Sharepoint Management Shell 2010

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)