Tuesday, 2 November 2010

Convert Word documents to PDF in SharePoint Server 2010 using PowerShell

SharePoint Server 2010 Standard and Enterprise editions includes a feature called Word Automation Services. There is an excellent article here explaining it in more detail, but in summary, the feature provides the capability for converting documents from one Microsoft Word format to another (e.g., .doc to .docx), converting Word documents to .pdf or .xps, and execute tasks during the opening of a document – such as updating the Table of Contents or index fields.

Whilst there are probably more applications for converting documents in workflows and event receivers, a batch of documents can also be converted using PowerShell. In this article I shall run through the basic steps involved in converting documents with an example of how to convert Word documents to PDF, plus some information on how you can speed up the conversion process and also remove the original Word documents from SharePoint, once successfully converted.

Before you can start converting documents, you will need to create a Word Automation Service Application instance from the Manage Service Applications page in Central Administration, and start the Word Automation Services service from the Manage services on server page, also in Central Administration. I’m also assuming that you have a site ready with a document library containing the Word documents to be converted, as shown below.

1

Once you have your service application running, you can type the following two lines in PowerShell to connect to the Word Automation Services Proxy and set up the variable for a new conversion job:

$wasp = Get-SPServiceApplicationProxy | where { $_.TypeName -eq "Word Automation Services Proxy" }
$job = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJob($wasp)

Next, we will get the site, folder containing the Word documents to be converted, and folder to store the converted PDF documents (this folder can be the same). Note that in addition to converting documents in folders, the ConversionJob class also contains methods to add individual files or complete document libraries, if preferred:

$web = Get-SPWeb http://portal/team
$inputFolder = $web.GetFolder("Shared Documents/Word Documents")
$outputFolder = $web.GetFolder("Shared Documents/PDF Documents")

We can now set up our conversion job using the properties available in the ConversionJob class. This includes a description for the job, specifying PDF as the save format, and choosing to overwrite any PDF documents with the same name that currently exist in the folder:

$job.UserToken = $web.CurrentUser.UserToken
$job.Name = "Convert Hello Docs to PDF"
$job.Settings.OutputFormat = [Microsoft.Office.Word.Server.Conversions.SaveFormat]::PDF
$job.Settings.OutputSaveBehavior = [Microsoft.Office.Word.Server.Conversions.SaveBehavior]::AlwaysOverwrite

The AddFolder method below includes the input folder (source folder containing the Word documents), output folder (destination folder into which the converted PDF documents will be copied), and a boolean option specifying whether you want to include sub-folders under the input folder. For example, if this is set to $true, then documents will be converted from the input folder and all documents in sub-folders below it:

#Add input and output folders and start conversion
$job.AddFolder($inputFolder, $outputFolder, $false)
$job.Start()

You can view the status of your job by typing the following commands:

$status = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobStatus($wasp.Id, $job.JobId, $null)
$status

An example of a conversion status report is shown below:

2
If you are wondering why the status for your documents are shown as “NotStarted”, it is because they are converted by a timer job, which by default runs every 15 minutes. If you want to start this timer job now instead of waiting, type the following:

$watj = Get-SPTimerJob "Word Automation Service Application"
$watj.RunNow()

Once the timer job has been run, the updated status will look as follows:

3

There is a nice bit of C# code in this article from the Microsoft Word product team blog which waits until all documents have been converted and then deletes the successfully converted Word documents from the input location. I have adapted this for use in PowerShell below. Just tack it on to the end of the timer job commands if you want to include it in a PS1 script file:

[bool]$done = $false
write-host "Converting files - Please wait..."
while(!$done)
{
    Start-Sleep -s 5
    $status = New-Object Microsoft.Office.Word.Server.Conversions.ConversionJobStatus($wasp.Id, $job.JobId, $null)
   
    if ($status.Count -eq ($status.Succeeded + $status.Failed + $status.Canceled))
    {
        $done = $true
       
        #Delete original Word files successfully converted to PDF
        #Remove this code if you want to keep the documents in their original location
        $itemType = [Microsoft.Office.Word.Server.Conversions.ItemTypes]::Succeeded
        $items = $status.GetItems($itemType)
        foreach($item in $items) {
            $file = $web.GetFile($item.InputFile)
            $file.Delete()
        }
    }
}
write-host "Conversion operation complete - Status report:"
$status
$web.Dispose()

Once the job has completed, you should see the converted PDF files in the output location specified in the script:

4

71 comments:

  1. So I've got your code running, many thanks! Question: my word doc has some embedded links to external files (when you open the doc, it asks to get the latest content from the linked files). When I add the $job.Settings.UpdateFields = $True
    I was hoping those links would be refreshed. No such luck. Any ideas?

    ReplyDelete
  2. Thanks, great work!

    Here is another solution to create PDF documents:
    http://www.parago.de/2011/04/how-to-export-sharepoint-task-list-data-to-pdf-using-a-templating-system/

    The solution is dynamically creating a PDF document from a SharePoint list using a template engine to customize the PDF output.

    ReplyDelete
  3. To enhance document conversion (including conversion in PDF) in sharepoint workflows, there is HarePoint Workflow Extensions software
    ( http://www.harepoint.com/Products/HarePointWorkflowExtensions/ ) - about 200 new workflow activities, including free ones.

    ReplyDelete
  4. Great post Phil!

    Have you run this against newer SP2010 CU patch levels? I am able to run the above PowerShell against RTM, but not against SP1 or SP1+Dec CU. Below is a thread where others are seeing the same WCF failure. Any ideas would be greatly appreciated! !

    Best,
    @SPJeff


    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/b3933e94-d73b-4bb2-b6ff-f85ccd11947a/?prof=required

    ReplyDelete
  5. Hi,
    Thanks for your post.
    How to add text in to the Pdf?...

    ReplyDelete
  6. You would have to add text to the Word document before converting it.

    ReplyDelete
  7. Hi,

    Thanks for a good article. Anyone know if it´s possible to get the metadata from the doc to be written into the pdf item?

    ReplyDelete
  8. Very niced post and it was a great help for me.
    keep up good work!

    ReplyDelete
  9. تعد الاخلاص افضل شركة تنظيف ومكافحة حشرات بالطائف فهى تقدم افضل الخدمات وباقل الاسعار لانها تتميز بانها افضل :
    افضل شركة رش مبيدات بالطائف
    افضل شركة مكافحة حشرات بالطائف
    افضل شركة تنظيف خزانات بالطائف
    شركة تنظيف منازل بالطائف
    تسليك مجاري بالطائف
    ______________
    شركة عزل خزانات بالطائف
    _______
    نستخدم افضل المبيدات في عمليات الرش للقضاء النهائي على الحشرات بجميع انواعها ونوفر عدة خدمات تنظيف اخرى فدوما يمكنك الاتصال بنا للحصول على افضل الخدمات وباسعار مناسبة
    شركة رش مبيدات بالرياض
    افضل شركة تنظيف خزانات بالرياض
    شركة عزل اسطح بالرياض
    افضل شركة تنظيف بالخرج
    شركة نقل اثاث بالخرج
    افضل شركة تنظيف مجالس بالطائف
    افضل شركة تنظيف منازل بالطائف
    افضل شركة عزل اسطح بالطائف
    افضل شركة نقل عفش بالطائف

    ReplyDelete
  10. لابد من الاستعانة بشركة متخصصة في مكافحة النمل الابيض للتخلص منه وتلافي اخطاره فالاول تعد من افضل شركات مكافحة الحشرات وخدماتها تغطى جميع مناطق المملكة فالاول افضل شركة مكافحة النمل الابيض بالرياض وافضل شركة مكافحة النمل الابيض بجدة وافضل شركة مكافحة النمل الابيض بمكة وافضل شركة مكافحة النمل الابيض بالخبر وافضل شركة مكافحة النمل الابيض بالقصيم وتقوم ايضا بمكافحة النمل الابيض قبل البناء
    شركة مكافحة النمل الابيض بالمدينة المنورة
    الفبرونيل وهي مادة قوية جدا وفعالة جدا للنمل الابيض وهناك مادة البايفلكس
    بايفلكس Biflex TCشركة مكافحة النمل الابيض بالجبيل
    شركة مكافحة النمل الابيض بالاحساء
    - شركة مكافحة النمل الابيض بابها - شركة مكافحة النمل الابيض بينبع

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. The main reason why people use the typing services is because the services aid in facilitating the daily routine. typing documents

    ReplyDelete
  13. Selamat menunaikan ibadah puasa Salam dari Obat Epilepsi Semoga barokah

    ReplyDelete
  14. needed to thank you for this very good read! pantangan kista

    ReplyDelete
  15. There are a whole range of software that are available to help you convert PDF files into different formats and let us have a look at some of the popular PDF Applications that are available online. Adobe After Effects CS6

    ReplyDelete
  16. thank for information, is the nice post. Informasi yang sangat bermanfaat sekali, terima kasih banyak atas informasinya yah obat iritasi kulit wajah karena kosmetik

    obat asam lambung tinggi untuk ibu hamil

    ReplyDelete
  17. I'll review this blog as An and it demonstrates how much exertion has been put into this.
    eidas compliance

    ReplyDelete
  18. It is great to have visited your website. Thanks for sharing useful information. And also visit my website about health. God willing it will be useful too

    Obat Sakit Dada
    Obat Tumor Jinak diatas Bola Mata
    Obat Penghilang Nyeri Lutut
    Obat Penghilang Nyeri Pada Payudara
    Pengobatan penyakit Meningioma
    Obat Luka Bernanah bekas Caesar

    ReplyDelete
  19. I want to say thanks to you. I have bookmark your site for future updates. https://www.altoconvertpdftoword.com/

    ReplyDelete
  20. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. business document software

    ReplyDelete
  21. También puede visitar una tienda de ladrillo y mortero de Michael Kors o su sitio web y comprar directamente un bolso Michael Kors desde allí. Usar un bolso de Michael Kors les permite a los demás reconocer que el habitante urbano educado toma la moda realmente con seriedad. Los bolsos de hombro son particularmente refinados y elegantes.

    {Bolsas Michael Kors Precios | Bolsos Michael Kors Outlet | Michael Kors Rebajas}

    En vacker konstnärlig skapelse av vävt läder, som ger ett skalskaligt utseende - liknar en snakeskin eller fiskhud, linjer utsidan av påsen. Läderens bältros är små läderringar. Det finns också gyllene accenter på väskan. Slutresultatet är svagt liknar kedjepost.

    {Michael Kors Rea | Michael Kors Väska Rea | Michael Kors Plånbok}

    ReplyDelete
  22. Some essential instruments are the devices that can make PDF records from Microsoft Word archives. https://www.altoconvertwordtopdf.com/blog/word_and_pdf_are_just_one_step_apart

    ReplyDelete
  23. Intriguing post. I Have Been pondering about this issue, so much obliged for posting. Really cool post.It "s truly extremely pleasant and Useful post.Thanks dokument

    ReplyDelete
  24. Luckily, Word 2007 accompanies an implicit utility to make a PDF record! You should simply type up your report and afterward tap on the Office button (in the upper left corner), and snap Save As > PDF or XPS. https://ds11.pdffiller.com/en/features.htm

    ReplyDelete
  25. They can clarify how science frequently propels with the presentation of new advances and how tackling innovative issues regularly results in new logical learning. diebestenvpn

    ReplyDelete
  26. A beautiful purse or handbag from Coach Outlet Online can last for many years, and represent a great overall value.
    There are Michael Kors Bags Outlet in a large number of shopping malls located throughout the country.
    Cheap Michael Kors Bags is a great way to determine which models best suit your needs.
    Official Coach Factory Outlet Online all strive to provide comfort and convenience for their owners and the seams are double-stitched for maximum durability.
    Michael Kors Factory Store, has one of the most popular handbag and accessory lines on the market today.
    Coach Handbags Wholesale says a lady is classy, elegant and sophisticated.
    Coach Store Near Me trends come and go, but a Coach stands the test of time.
    The official Michael Kors Handbags On Sale Outlet regularly posts various handbags being offered for sale.
    Compare your Coach Bags On Sale Outlet to the logo provided on the website to make sure it is similar.
    All Michael Kors Outlet Online Store have serial numbers.

    ReplyDelete
  27. No matter what type of Michael Kors Bags Outlet each individual got, they all had one thing in common. We all know that getting something on sale is like winning a mini lottery, but to find a name brand handbag like Michael Kors Black Friday Sale for instance, discounted is like a slice of heaven on earth. I have personally attacked my husband with hugs and kisses when finding Official Coach Outlet Online at discounted prices.

    Michael Kors Factory Outlet stores can be found in malls all over. MK Outlet and other Coach merchandise can also be found in some boutiques. Inspired by the same material baseball gloves are made from, these Coach Bags On Sale Online are versatile and stylish. Since it's conception, the Michael Kors Handbags Outlet has been produced into a variety of designs, colors, shapes, and styles that have won top pick of millions of women the world over.

    New Michael Kors Bags are the most sought after handbag. Since Coach Outlet Online is one of the most recognized name brands in the world of fashion, you will find A-list celebrities down to small girls wearing them.

    tags:Coach Outlet|Coach Bags Factory Outlet|Coach Purses Outlet

    ReplyDelete