MuhimbiConvertTo-PDF PowerShell Module

Muhimbi PDF Converter comes with a comprehensive, but friendly, web services interface that can be accessed from any modern web services based development environment including JavaPHPRuby, SAP, SharePoint, .NET (C#, VB) etc.

As we all know, PowerShell is very similar to other .NET environments and although we can use PowerShell’s built in web services facilities using the New-WebServiceProxy commands, It is much easier to for us to use the pre-generated proxy DLL that ships alongside our software. In the folder where the Conversion Service has been deployed to (See the Muhimbi Document Converter/Open Installation Folder shortcut in the Windows start menu) you will find the Muhimbi.DocumentConverter.WebService.Client.dll and Muhimbi.DocumentConverter.WebService.Data.dll. This DLL contains up to date proxies for the entire web service.

In this blog we will recreate the Muhimbi NET code to PowerShell Module(Cmdlet) which will assist you to Convert a document to PDF.

Prerequites:

  1. Muhimbi should be installed and configured on the Server. You can download the Trial at: http://www.muhimbi.com/Products/PDF-Converter-for-SharePoint/Free-Trial.aspx
  2. The person should have basic understanding of the PowerShell.

Simple PowerShell example to Convert a file to PDF

The following sample shows the minimum steps required to convert a document to PDF format. In our example we will use PowerShell Objects on any environment that can invoke web services should be able to access the required functionality. Note that the WSDL can be found at http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl.

This example does not explicitly set ConversionSettings.Format. As a result the file is converted to the default PDF format. It is possible to convert files to other file formats as well by setting this property to a value of the Output Format enumeration. For details see this blog post. http://blog.muhimbi.com/2012/02/convert-document-types-using-pdf.html

You can find more C# samples for Converting, Watermarking and Merging documents at: https://support.muhimbi.com/hc/en-us/articles/228089807-Where-can-I-find-sample-code

You can also find the PDF Converter Services User & Developer Guide contains details  about how to program against the product’s Web Services based interface from .NET at https://support.muhimbi.com/hc/en-us/articles/228089927-Where-can-I-find-details-about-the-PDF-Converter-s-object-model

  1. Download the file MuhimbiConvertto-PDF.
  2. Open PowerShell as Administrator Navigate to Folder that contains the Download location for the file and Import the module.

1

3 Run the Command

PS C:\Users\clavin.fernandes\Downloads\MuhimbiConvertto-PDF> MuhimbiConvertTo-PDF -FilePath C:\Users\clavin.fernandes\Desktop\Test.DOCX

2

4. The Converted File will be in same source document directory. i.e. with reference to my example it will be on my desktop.


Function MuhimbiConvertTo-PDF

{

[CmdletBinding()]

param

(

#Enter the Full Path of the file

[parameter(Mandatory=$true,helpmessage ='Give me a path in format C:\Users\clavin.fernandes\Documents\somefile.docx')][String]$FilePath

#[parameter(Mandatory)][String]$MuhimbiServiceURL

)

Add-Type -Path ".\Muhimbi.DocumentConverter.WebService.Client.dll"

Add-Type -path ".\Muhimbi.DocumentConverter.WebService.Data.dll"

try

{

# ** The URL where the Web Service is located. Amend host name if needed.

[URI]$ServiceURL = "<a href="http://localhost:41734/Muhimbi.DocumentConverter.WebService/">http://localhost:41734/Muhimbi.DocumentConverter.WebService/</a>"

#** The source file path and read it into a byte array.

$sourceFile = [IO.File]::ReadAllBytes($FilePath)

#** Set the absolute minimum open options

$Openoption = New-Object "Muhimbi.DocumentConverter.WebService.Data.OpenOptions"

$Openoption.OriginalFileName = [IO.Path]::GetFileName($FilePath)

$Openoption.FileExtension= [IO.Path]::GetExtension($FilePath)

# ** Set the absolute minimum conversion settings

$ConveterSetting = New-Object "Muhimbi.DocumentConverter.WebService.Data.ConversionSettings"

$ConveterSetting.Fidelity = [Muhimbi.DocumentConverter.WebService.Data.ConversionFidelities]::Full

$ConveterSetting.Quality = [Muhimbi.DocumentConverter.WebService.Data.ConversionQuality]::OptimizeForPrint

#** Use standard Windows Security.

$binding = New-Object System.ServiceModel.BasicHttpBinding

$binding.Security.Mode = [System.ServiceModel.BasicHttpSecurityMode]::TransportCredentialOnly

$binding.Security.Transport.ClientCredentialType = [System.ServiceModel.HttpClientCredentialType]::Windows

#** Increase the Timeout to deal with (very) long running requests.

$binding.SendTimeout = [System.TimeSpan]::FromMinutes(30)

$binding.ReceiveTimeout = [System.TimeSpan]::FromMinutes(30)

#** Set the maximum document size to 40MB

$binding.MaxReceivedMessageSize = 50*1024*1024

$binding.ReaderQuotas.MaxArrayLength = 50 * 1024 * 1024

$binding.ReaderQuotas.MaxStringContentLength = 50 * 1024 * 1024

#** Specify an identity (any identity) in order to get it past .net3.5 sp1

$EPI = [System.ServiceModel.EndpointIdentity]::CreateUpnIdentity("unknown")

$EPA = New-object -TypeName System.ServiceModel.EndpointAddress -ArgumentList $ServiceURL, $EPI

#** Open the service and configure the bindings

$client = New-Object -TypeName Muhimbi.DocumentConverter.WebService.Client.DocumentConverterService.DocumentConverterServiceClient -ArgumentList $binding, $EPA

$client.Open()

# ** Carry out the conversion

$Converted = $client.Convert($sourceFile, $Openoption , $ConveterSetting)

#** Write the converted file back to the file system with a PDF extension.

$DirectoryName= [System.IO.Path]::GetDirectoryName($FilePath)

$FileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($FilePath)

$Conversionextenation = $ConveterSetting.Format

$destinationpath = "$DirectoryName`\$FileNameWithoutExtension`.$Conversionextenation"

[System.IO.File]::WriteAllBytes($destinationpath,$Converted)

}

catch

{

$ErrorMessage = $_.Exception.message

Write-Host $ErrorMessage -ForegroundColor Red -BackgroundColor White

}

finally

{

#** Close the Client

$client.Close()

}

}

6. Now if  you need more information about the Syntax, Parameters, Examples etcYou can always use the help file for the Cmdlet.  In the PowerShell Prompt type  get-help MuhimbiConvertTo-PDF -full to see details.

3

Happy Converting 🙂 using MuhimbiConvertTo-PDF PowerShell cmdlet…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s