MuhimbiConvertOnlineTo-PDF PowerShell Module

A few week ago, I had written a Blog “MuhimbiConvertTo-PDF PowerShell Module” for the On-Premise Muhimbi Converter.

In addition to the On-Premise Converter, Muhimbi also offers a cloud based solution “Muhimbi PDF Converter Service Online” it is a REST based service that makes it easy to add document conversion, watermarking, merge etc.

In this Blog, we will use the Muhimbi Conversion Service Online REST API using PowerShell “Invoke-WebRequest” built in PowerShell Cmdlet.

Article Related to Muhimbi  PDF Converter Services Online:

For details on getting started with the PDF Converter Services Online, see: https://support.muhimbi.com/hc/en-us/articles/115002816048-Getting-started-with-the-PDF-Converter-Services-Online

You can also find client libraries and samples at: https://github.com/Muhimbi/PDF-Converter-Services-Online


Prerequisites:

1. You need to have a Muhimbi PDF Converter Service Online subscription. Or just a sign-up for a trial Subscription.

2. The person should have basic understanding of the PowerShell.


Steps to Import the MuhimbiConvertOnlineTo-PDF Module(Cmdlet) and Execute it:

1. Download the file MuhimbiConvertOnlineTo-PDF.

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

1.1

 

3. Run the Command PS C:\Users\ClavinFernandes\Desktop\MuhimbiOnline-ConverttoPDF>

MuhimbiConvertOnlineTo-PDF -apikey 0000000-0000-0000-0000-00000000 -sourceFilePath C:\Users\ClavinFernandes\Desktop\sample.docx -targetFilePath C:\Users\ClavinFernandes\Desktop\sample.pdf

1.2

Note: The Converted File will be in target directory you have specified in the PowerShell Parameter [-targetFilePath]. With reference to my example it will be on my desktop.


# This Function converts Memory Stream to String, which we can use for catching Expection
function ConvertFrom-StreamToString
    {
    param(
    [parameter(Mandatory)][System.IO.MemoryStream]$inputStream
    )
    $reader = New-Object System.IO.StreamReader($inputStream);
    $inputStream.Position = 0;
    return $reader.ReadToEnd()
    } 

function MuhimbiConvertOnlineTo-PDF
    {
    [CmdletBinding()]
    param(
        [parameter(Mandatory=$true,helpmessage ='!!!! ENTER YOUR API KEY HERE !!!!')][String]$apikey,
        [parameter(Mandatory=$true,helpmessage ='Give me a path in format C:\Users\clavin.fernandes\Documents\somefile.docx')][String]$sourceFilePath,
        [parameter(Mandatory=$true,helpmessage ='Give me a path in format with extension .pdf C:\Users\clavin.fernandes\Documents\somefile.pdf')][String]$targetFilePath
    )
       try
            {
            #Converting File to Base64 bit
            $fileContentEncoded = [System.Convert]::ToBase64String([IO.File]::ReadAllBytes($sourceFilePath))
            #You REST-API call starts here
            $URL = "https://api.muhimbi.com/api/v1/operations/convert"
            $content = "application/json"
            $hdrs = @{}
            $hdrs.Add("API_KEY","$apikey") 

       $body =  ConvertTo-Json @{
            "use_async_pattern" = "false";
            "fail_on_error" = "true";
            # The format to convert the file
            "output_format" = "PDF";
            #file name with the correct extension
            "source_file_name"= [IO.Path]::GetFileName($sourceFilePath);
            #The file content to convert
            "source_file_content" = "$fileContentEncoded"
            } 

       $Content = Invoke-WebRequest -Method Post -Uri $URL -Headers $hdrs -Body $body -ContentType $content -TimeoutSec 3600 -UseBasicParsing 

       #Getting the processed file
       $response = $Content.Content | ConvertFrom-Json
       $base64string = $response.processed_file_content
       #Converting the file from base64 and writing the file back to your target location
       [IO.File]::WriteAllBytes($targetFilePath, [Convert]::FromBase64String($base64string))
       }
       #Read the response and print the Error details
       catch
       {
           $DecryptedOutput = ConvertFrom-StreamToString -inputStream $Error[0].Exception.Response.GetResponseStream()
           $response = $DecryptedOutput | ConvertFrom-Json
           $response.result_details
       }
}

 

4. 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 MuhimbiConvertOnlineTo-PDF -full to see details.

1.3

Happy Converting 🙂 using MuhimbiConvertOnlineTo-PDF PowerShell cmdlet

Advertisements

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