Click here to Skip to main content
15,885,216 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2010
Tip/Trick

HTML to PDF Conversion using Sharepoint Management Shell 2010

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Mar 2014CPOL1 min read 9.9K   1   1
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)


Written By
Software Developer (Junior)
India India
A Software Developer. Curiosity defines me. I find extreme happiness in learning new Technologies. I'm here to give something back to the community and the web from which I learned so much.Working hard to create my own identity in the world of Information and Technology.

Comments and Discussions

 
-- No messages could be retrieved (timeout) --