Converting SharePoint documents to PDF by PowerShell, for free!

This is an article from my old blog site, blog.konffaaja.com originally published December 1, 2019.

There is a lot of commercial PDF converters available for SharePoint that can automatically convert SharePoint documents to PDF format, but there is also an free and fairly simple way to do that. The key is using Word Automation service, PowerPoint Automation service and PowerShell. You can use i.e. Windows task manager, if you want the conversion to be made automatically.

Before jumping into the PowerShell part please make sure that you have Word Conversion Service and PowerPoint Automation services configured and running. You can create Word Automation service in Cental Admin GUI under Manage Service Applications, but PowerPoint Automation service must be created by PowerShell:

New-SPPowerPointConversionServiceApplication -Name "PowerPoint Automation Service Application" -ApplicationPool "Common Services Application Pool"
New-SPPowerPointConversionServiceApplicationProxy -Name "PowerPoint Automation Service Application Proxy" -ServiceApplication "PowerPoint Automation Service Application" -AddtoDefaultGroup

After that make sure that both services are running on at least one server in the farm. Now you are ready for the actual PS scripts. Word documents can be converted by using the script below. The script will create a new job into Word Automation service and then run the timer job to create the conversions. In the beginning you have to reference Microsoft.Office.Word.Server.dll -file, please check that the dll exists in the same location in your environment.

Add-PSSnapin Microsoft.SharePoint.PowerShell
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Office.Word.Server\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.Office.Word.Server.dll"

# Get the web url
$web = Get-SPWeb "http://intranet/"

$sourceLibrary = $web.GetList("Shared Documents")
$destinationLibrary = $web.GetList("DocumentConversions")

# Get the Word Automation Service Proxy
$wasp = Get-SPServiceApplicationProxy | where { $_.TypeName -eq "Word Automation Services Proxy" }
#Create the Conversion job

$conversionJob = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob($wasp)

# Set the credentials to use when running the conversion job.
$conversionJob.UserToken = $web.CurrentUser.UserToken
# Conversion Job Name
$conversionJob.Name = "Convert docx to PDF"
$conversionJob.Settings.OutputFormat = [Microsoft.Office.Word.Server.Conversions.SaveFormat]::PDF
$conversionJob.AddLibrary($sourceLibrary, $destinationLibrary)

# Start the conversion job
$conversionJob.Start()

$timerJob = Get-SPTimerJob | Where { $_.DisplayName -eq "Word Automation Services Timer Job" }
$timerJob.RunNow()

For PowerPoint documents you can use following script. The script queries all ppt and pptx files from the document library and converts all documents to pdf. Note that PowerPoint Automation service works a bit differently than Word Automation service. In the beginning you have to reference Microsoft.Office.Server.PowerPoint.dll -file, please check that the dll exists in the same location in your environment

Add-PSSnapin Microsoft.SharePoint.PowerShell
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.Office.Server.PowerPoint\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.Office.Server.PowerPoint.dll"

$web = Get-SPWeb "http://intranet/"
$sourceLibrary = $web.GetList("Shared Documents")
$destinationLibrary = $web.GetList("DocumentConversions")

$query = New-Object Microsoft.SharePoint.SPQuery
$query.Folder = $sourceLibrary.RootFolder;
$query.ViewXml = "<View Scope='Recursive'>
 <Query>
 <Where>
 <Or>
 <Contains>
 <FieldRef Name='File_x0020_Type'/>
 <Value Type='Text'>ppt</Value>
 </Contains>
 <Contains>
 <FieldRef Name='File_x0020_Type'/>
 <Value Type='Text'>pptx</Value>
 </Contains>
 </Or>
 </Where>
 </Query>
</View>";
 
$items = $sourceLibrary.GetItems($query);

foreach($item in $items)
{

 $destinationFileName = [System.IO.Path]::GetFileNameWithoutExtension($item.Name)+".pdf";

 $settings = New-Object Microsoft.Office.Server.PowerPoint.Conversion.FixedFormatSettings;
 $settings.BitmapUnembeddableFonts = $true;
 $settings.FrameSlides = $true;
 $settings.IncludeDocumentProperties = $true;
 $settings.IncludeDocumentStructureTags = $true;
 $settings.IncludeHiddenSlides = $true;
 $settings.OptimizeForMinimumSize = $false;
 $settings.UsePdfA = $false;
 $settings.UseVerticalOrder = $true;

 $memStream = New-Object System.IO.MemoryStream

 $request = New-Object Microsoft.Office.Server.PowerPoint.Conversion.PdfRequest($item.File.OpenBinaryStream(), ".pptx", $settings, $memStream);
 $result = $request.BeginConvert((Get-SPServiceContext -Site $sourceLibrary.ParentWeb.Site), $null, $null);
 $request.EndConvert($result);

 $newFile = $destinationLibrary.RootFolder.Files.Add($destinationFileName, $memStream, $true);

}

This script is made for on-prem SharePoint, I haven’t tested if it could be converted for SharePoint Online. So here you are, your free SharePoint PDF converter.

Leave a Comment