Subscribe to News feed

Using Windows Azure to convert documents to PDF format – Part 1

Posted at: 20:15 on 28 February 2011 by Muhimbi

Windows Azure LogoToday’s article is a guest post by John Dent from BlueHub Solutions, who have been using our popular PDF Conversion Services to convert documents in MS-Office, AutoCAD, HTML, Image and many other formats to PDF from a Windows Azure based solution.

Over the next few weeks we’ll publish a number of posts related to using our software in combination with the Azure platform, so make sure you subscribe to our RSS feed or follow us on Twitter.

The following posts are part of this series:

  1. Using Windows Azure in combination with an internet facing web service. (This post)
  2. Using Windows Azure in combination with a web service hosted inside an organisation’s private network.
  3. Using Windows Azure in combination with a 3rd party web service hosted inside a custom Azure VM. (Coming soon)

If you have any questions then please contact us, we are always happy to talk to our customers. For now I’ll hand you over to John.


This post details how I created a very basic Windows Azure web application which colleagues in my company could use to convert documents to PDF format quickly and easily. This application is built around the Muhimbi PDF Conversion web service.

I work for BlueHub Solutions, a small software development consultancy in England, and I’m noticing that most of our new solutions these days seem to relate to rich Silverlight front-ends talking to hosted web applications via WCF using SQL Server as the back-end, and could thus loosely be considered cloud applications. I’m certainly a big fan of the theory of cloud computing, but had never really taken the time to look into Windows Azure in depth. Now that I have I’m very impressed, and we’ll certainly be aiming to use it in anger for our next client project.

As a starting point I installed the Muhimbi PDF Conversion Services on an Internet facing server, such that our Azure web application will provide a basic user interface onto this service. In subsequent blog posts I will show how these services can be hosted on your internal network rather than the Internet, or hosted within a VPC hosted in Azure.

If you are following along you will need a Windows Azure account, you can create one for free, or if you have an MSDN subscription you can also access associated Azure benefits from the same link.


Component and tools installation

Before we begin we need to install the Windows Azure SDK, the Windows Azure Tools for Microsoft Visual Studio 2010 and the Muhimbi PDF Converter Services.

Download and install the SDK (you just need the SDK and not the other supporting files). After installing the SDK install the Windows Azure Tools for Microsoft Visual Studio 2010. Unfortunately the Azure tools are no longer updated for Visual Studio 2008, but an optional copy of VS2010 is included in the Azure Tools download.

Install the PDF Converter Services on an internet facing server as described in Chapter 2 of the product’s Administration Guide. As you will be requesting the WSDL remotely, edit Muhimbi.DocumentConverter.Service.exe.config, change the baseAddress to the public URL the service will be available from and restart the Muhimbi Service.

Note that I initially didn’t install the copy of SQLExpress that comes with Visual Studio 2010, which resulted in all kind of interesting messages including “failed to initialize windows azure storage emulator”. After installing SQLExpress this message went away. If you really don’t want to install SQLExpress locally then you can skip the local test run and upload the package straight to Azure after the setting up Azure storage step.


Project Creation

Having installed the SDK and tools, open a new instance of Visual Studio 2010 and click the New Project command. You should see a new section called Cloud.


So far so good – not too many options here, so select Windows Azure Project. I called mine WAP1.

On the next screen we’ll select the type of application we are going to create, for this example I am going to create a simple C# ASP.Net web application as shown below (though of course you may prefer a different language).


This interface is a little awkward, you need to click the right-hand arrow to move the ASP.NET Web Role across, and then you need to click the pencil icon to rename the role (in my case I named it TestWebsite).

Once created the base solution should look something like this:



Note that depending on the version of the .Net Framework selected some of these items may not be present in your solution. A number of these files are unnecessary, so let’s clean up a little and delete the following (if present):

  • About.aspx,
  • Default.aspx
  • Site.master
  • Style folder
  • Account folder

The resulting solution should look a little like this



Creating the web application interface

I’m going to write a simple web form that allows a user to upload a document, convert it to PDF and then return the generated PDF file to the browser.

To do this, I created a new web form called Default.aspx (without a master page). I chose this name because, like IIS, Azure will automatically load this file in the absence of a more specific URL. Of course there was already a Default.aspx in the project before, which I deleted as I prefer to start from scratch.

This page simply needs a header, a file upload control, and a Convert button. Here’s the resultant ASP.Net mark-up, which you can paste straight into Default.aspx.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWebsite.Default" %>
<html xmlns="">
<head id="Head1" runat="server">
    <title>HTML to PDF Converter</title>
<body style="background-color:#03899C; font-family:Calibri;">
    <form id="form1" runat="server">
    <h1>File to PDF Converter</h1>
    <td><asp:Label ID="Label1" Text="File : " runat="server" /></td>
    <td><asp:FileUpload ID="FileUploadControl" runat="server" /> </td>    </tr><tr>
    <td><asp:Button ID="Button1" runat="server" Text="Convert" onclick="Convert_Click" /></td>


Coding the web application logic

The next step is to add a web reference to the Muhimbi PDF converter web service. In my case I am adding it as a Web Reference by right clicking on References and then selecting Add Web Reference... As the Muhimbi PDF Conversion service is a WCF based service you should also be able to add it as a Service Reference, but I haven’t tested that particular scenario. (Sample code for using Service References is available here)



I then typed the URL of the Internet facing web service (e.g. http://YourName:41734/Muhimbi.DocumentConverter.WebService/) into the top box, and waited for it to download the WSDL information. I entered a web reference name of DCS and clicked the Add Reference button.



Now it’s time to add the code to our project, which will:

  • Set up a link to the Azure file storage
  • Turn the file into binary byte data for transport
  • Open a connection to the Muhimbi web service
  • Set the minimum file options for the conversion process
  • Call the service method
  • Turn the returned data back into a file
  • Redirect the user to the PDF document.

If you wish you can amend the code and skip the step that writes the PDF to Azure storage and instead return the PDF directly to the user.


To achieve this we’ll need to edit Default.aspx.cs :

First we need to add the following references to the top of the file :

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;
using TestWebsite.DCS;  // (Providing you use the same names as we did)

Then add the following method to the class :

protected void Convert_Click(object sender, EventArgs e)
     if (FileUploadControl.HasFile)
         StorageCredentialsAccountAndKey account = new StorageCredentialsAccountAndKey("AccountName", "PrimaryAccessKey");
         CloudStorageAccount storageAccount = new CloudStorageAccount(account, false);
         CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
         CloudBlobContainer files = cloudBlobClient.GetContainerReference("files");
         files.BeginCreateIfNotExist(null, null);
         BlobContainerPermissions perm = new BlobContainerPermissions();
         perm.PublicAccess = BlobContainerPublicAccessType.Container;
         string filename = Path.GetFileName(FileUploadControl.FileName);
         // ** Open the connection to the web service
         DCS.DocumentConverterService service = new DCS.DocumentConverterService();
         // ** Get the file data
         byte[] sourceFile = FileUploadControl.FileBytes;
         //** Set the absolute minimum open options
         DCS.OpenOptions openOptions = new DCS.OpenOptions();
         openOptions.OriginalFileName = Path.GetFileName(filename);
         openOptions.FileExtension = Path.GetExtension(filename);
         // ** Set the absolute minimum conversion settings.
         DCS.ConversionSettings conversionSettings = new DCS.ConversionSettings();
         conversionSettings.Fidelity = DCS.ConversionFidelities.Full;
         conversionSettings.FidelitySpecified = true;
         conversionSettings.Quality = DCS.ConversionQuality.OptimizeForPrint;
         conversionSettings.QualitySpecified = true;
         conversionSettings.Format = DCS.OutputFormat.PDF;
         conversionSettings.FormatSpecified = true;
         conversionSettings.PDFProfile = PDFProfile.PDF_1_5;
         conversionSettings.PDFProfileSpecified = true;
         // ** Carry out the conversion.
         byte[] convFile = null;
         convFile = service.Convert(sourceFile, openOptions, conversionSettings);
         string newFileName = Path.GetFileNameWithoutExtension(filename) + "." + conversionSettings.Format;

         // ** Save the PDF to Azure storage
         CloudBlob saveloc = files.GetBlobReference("/temp/" + newFileName);
         // ** Redirect them to the pdf.


At this stage, if we run the ASP.Net web project then we should get the following output from Default.aspx. (Note that you may need to set Default.aspx as the start-up page).



Azure Storage

Before we can test the project we need to configure a storage account for the Azure platform, so we have a place to store the PDF files - this is where we will hold our files after they have been converted.

To do this login to your Windows Azure account and click on New Storage Account. This shows the following form:


We need to get our Storage Account Name and our Primary Access key, these are shown on the right hand side of the screen after clicking Create. You then enter these in default.aspx in place of the placeholders on the following line of Default.aspx.cs :

 StorageCredentialsAccountAndKey account = new StorageCredentialsAccountAndKey("AccountName", "PrimaryAccessKey");




We can now test the application. To do this, we re-launch the application and browse to a file that we want to convert. On clicking the Convert button we should be redirected to a PDF copy of that file.


Uploading to Azure

To upload to Azure is relatively simple – we first right click on the Azure project (WAP1) in our solution and click Publish. The following form is displayed:



After this we select Create Service Package Only and click OK. This creates a small package which we can upload to Azure. To do this, we log in to our Azure account and select New Hosted Service.

We’re then prompted to fill out a short wizard to upload your service package. Specifically:

  • We need to enter a name for the service for our reference
  • We need to enter a unique URL that you want to the service to be accessed on (only used in production, not staging).
  • We need to select where in the world you want it to be hosted
  • We select stage or production environments (I selected stage)
  • We need to make sure Start after successful deployment is checked
  • We need to enter a name for this deployment

After doing this we just need to select the package and configuration file from the Debug folder of our Azure project (where it has been built) by browsing locally.


Then we click OK to start uploading and deploying the project – this can take some time, so it could be a good time to get a drink, tidy your desktop, clean out your Inbox, de-worm the dog etc. You may get a warning message during deployment about the fact that you are only using a single instance. For this test it is safe to ignore this warning.

Once this has completed you can highlight the deployment object in the grid and on the right hand side of the screen you will be able to get a link to test the application now hosted on the Windows Azure platform and start converting those files!


Next Steps

I hope to follow up this blog with another showing how to configure Azure Connect so that the Muhimbi web service could be hosted on your LAN rather than the Internet. I also want to explore the new VPC functionality in Windows Azure, which in theory allows you to run full virtual server environments in the cloud. Unfortunately, as these are both Beta features, I’m waiting on approval from Microsoft to be able to access them, a curious process where apparently you are effectively guaranteed to be approved but such approval takes weeks or months to be issued… (Update: It only took 2 days when Muhimbi applied for the beta)


About Me

If you have any comments then please post them below, or feel free to email me at Alternatively you can contact Muhimbi’s support team.

John Dent


Labels: , , , , , ,


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

Need support from experts?

Access our Forum

Download Free Trials