woggles
woggles

Reputation: 7444

Using tuespechkin with MVC project in Azure

I can't manage to get pechkin or tuespechkin to work on my azure site.

Whenever I try to access the site it just hangs with no error message (even with customErrors off). Is there any further setup I'm missing? Everything works perfectly locally.

For a 64 bit app I'm completing the following steps:

Upvotes: 3

Views: 2775

Answers (2)

GicaGG
GicaGG

Reputation: 56

As far as i know, you can't make it work in a web app. However, there is a way you can do it: you have to create a cloud service and add a worker role to it. TuesPechkin will be installed in this worker role.

The workflow would be the following: from your cloud web app, you would access the worker role(this thing is possible by configuring the worker role to host Asp.NET Web API 2). The worker role would configure a converter using TuesPechkin and would generate the PDF. We would wrap the pdf in the web api response and send it back. Now, let's do it...

To add a cloud service (suppose you have Azure SDK installed), go to Visual Studio -> right click your solution -> Add new project -> select Cloud node -> Azure Cloud Service -> after you click OK select Worker Role and click OK.

Your cloud service and your worker role are created. Next thing to do is to configure your Worker Role so it can host ASP.NET Web API 2. This configuration is pretty straightforward, by following this tutorial.

After you have configured your Worker Role to host a web api, you will have to install the TuesPechkin.Wkhtmltox.Win64 and TuesPechkin nuget packages.

Your configuration should now be ready. Now create a controller, in which we will generate the PDF: add a new class in your Worker Role which will extend ApiController:

public class PdfController : ApiController
{
}

Add an action to our controller, which will return an HttpResponseMessage object.

[HttpPost]
public HttpResponseMessage GeneratePDF(PdfViewModel viewModel)
{
}

Here we will configure two ObjectSettings and GlobalSettings objects which will be applied to an HtmlToPdfDocument object. You now have two options.

You can generate the pdf from html text(maybe you sent the html of your page in the request) or directly by page url.

var document = new HtmlToPdfDocument
{
    GlobalSettings =
    {
        ProduceOutline = true,
        DocumentTitle = "Pretty Websites",
        PaperSize = PaperKind.A4, // Implicit conversion to PechkinPaperSize
        Margins =
        {
            All = 1.375,
            Unit = Unit.Centimeters
        }
    },
    Objects = {
        new ObjectSettings { HtmlText = "<h1>Pretty Websites</h1><p>This might take a bit to convert!</p>" },
        new ObjectSettings { PageUrl = "www.google.com" }
    }
};

A nice thing to now is that when using page url, you can use the ObjectSettings object to post parameters:

var obj = new ObjectSettings();
obj.LoadSettings.PostItems.Add
    (
        new PostItem() 
        {
            Name = "paramName",
            Value = paramValue
        }
    );

Also, from TuesPechkin documentation the converter should be thread safe and should be kept somewhere static, or as a singleton instance:

IConverter converter =
    new ThreadSafeConverter(
         new RemotingToolset<PdfToolset>(
              new Win64EmbeddedDeployment(
                   new TempFolderDeployment())));

Finally you wrap the pdf in the response content, set the response content type to application/pdf and add the content-disposition header and that's it:

byte[] result = converter.Convert(document);
MemoryStream ms = new MemoryStream(result);

response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(ms);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
response.Content.Headers.Add("content-disposition", "attachment;filename=myFile.pdf");
return response;

Upvotes: 2

Nic
Nic

Reputation: 12855

I'm afraid the answer is that it's not possible to get wkhtmltopdf working on Azure.

See this thread.

I am assuming you mean running wkhtmltopdf on Windows Azure Websites.

wkhtmltopdf uses Window's GDI APIs which currently don't work on Azure Websites.

Tuespechkin supported usage

  • It supports .NET 2.0+, 32 and 64-bit processes, and IIS-hosted applications.
  • Azure Websites does not currently support the use of wkhtmltopdf.

Workaround

I ended up creating a Azure Cloud Service, that runs wkhtmltopdf.exe. I send the html to the service, and get a byte[] in return.

Upvotes: 1

Related Questions