Friday, February 13, 2015

What you should know about Word Automation Services in SharePoint to convert word to pdf

In multiple customer projects we had the requirement to convert word documents to pdf. There are really many ways to achieve this by using third party tools, OpenXML SDK and pdf rendering software or any other kind. SharePoint offers a so called Word Automation Service which can also transform your word document into pdf file. But this feature is only available at SharePoint Server Standard or higher. So 3rd party tools are great if you only use SharePoint Foundation.
In this post i assume that you have SharePoint Server Standard and i will write about the possibilities you have with the Word Automation Services (WAS). Let’s see what the input and output could be.

Source files types:

  • Open XML File Format documents (.docx, .docm, .dotx, .dotm).
  • Word 97-2003 documents (.doc, .dot).
  • Rich Text Format files (.rtf).
  • Single File Web Pages (.mht, .mhtml).
  • Word 2003 XML Documents (.xml).
  • Word XML Document (.xml)

Destination file types:

The supported destination document formats includes all of the supported source document formats, and the following.
  • Portable Document Format (.pdf)
  • Open XML Paper Specification (.xps)
In order to use it, you have to configure the word automation services as service application in SharePoint and it should be started. You can do it in the central administration in manage service applications. If you want to know the details of how it works i can recommend you to read this post. Here are also listed the limitations of WAS.
The big bang is, that there is no UI. What does it mean? Well, if you think that there is a button or context menu at each document or a feature which has to be activated now, you’re wrong. It is now possible to use it, but how?There are several options which i will list:
  • Developing your own custom action to start a conversion or use solutions from codeplex Solution 1 | Solution 2 | Solution 3
  • Developing your own custom activity to use it in a SharePoint Designer Workflow or in Visual Studio Workflow
  • Developing a custom Timer Job which starts the conversion
  • Developing an event receiver which starts the conversion
  • Using Powershell Script and put it into a windows task if it has to be converted at special times
As you can see, you have to develop or script something. I post my Powershell Script here. It starts the conversion. BUT it will not work in sandboxed mode.That’s why i also have a second script which will disable the sandbox mode for the WAS. It is also important if you develop it in code. The solution has to be a farm solution not sandboxed!
01Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
02# Input parameters for the script
04$date = Get-Date -format yyyy_MM
06# Get the Word Automation Service Proxy
07$wasp = Get-SPServiceApplicationProxy | where { $_.TypeName -eq "Word Automation Services Proxy" }
08#Create the Conversion job
09$conversionJob = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob($wasp)
10# Get the web url
11$web = Get-SPWeb "http://server/sites/TenneT/Ohu"
12# Set the credentials to use when running the conversion job.
13$conversionJob.UserToken = $web.CurrentUser.UserToken
14# Conversion Job Name
15$conversionJob.Name = "Convert docx to PDF"
16$conversionJob.Settings.OutputFormat = [Microsoft.Office.Word.Server.Conversions.SaveFormat]::PDF
17$conversionJob.AddFile($wordFile,$pdfFile)
18# Start the conversion job
19$conversionJob.Start()
Disable the sandbox mode:
1$sp = Get-SPServiceApplication | where {$_.TypeName.Equals("Word Automation Services")}
2$sp.DisableSandbox = $true
3$sp.Update()
Afterwords make an iisreset.
You should know that the conversion is an asynchronous process. So remember this if you develop a user interface and remember it also if you start the conversion and refresh the page! It may take up to 15min. cause the timer job “Word Automation Services Timer Job” runs every 15min. If you start it manually it will start the conversion.
Okay, what are the main things from this post?
  • Converting documents to pdf
  • NO UI – you have to code it
  • NO UI – you cannot start it easily by clicking a button
  • Many possibilities
  • NOT in SP Foundation
Happy Converting!
The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Source: http://www.ilikesharepoint.de/2014/04/what-you-should-know-about-word-automation-services-in-sharepoint-to-convert-word-to-pdf/

No comments:

Post a Comment