Click here to Skip to main content
Click here to Skip to main content

HTML to PDF Conversion using Sharepoint Management Shell 2010

, 12 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Ajith K Gatty
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.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionMessage Automatically Removed Pinmemberpittypan29-Apr-14 20:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 12 Mar 2014
Article Copyright 2014 by Ajith K Gatty
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid