Using tuespechkin with an MVC project on Azure

I won't be able to get Pechkin or Tupechkin to work on my azure site.

Whenever I try to access the site, it just hangs with no error message (even with customErrors disabled). Are there any further settings I am missing? Everything works fine in place.

For a 64-bit application, I follow these steps:

  • Create a new Blank MVC app with Azure, make sure the host is selected in the cloud
  • Change your app to 64 bit

  • Login to azure portal and update your app to basic hosting and change to 64-bit

  • Install the WTTW.Win.Whtmltox.Win64 and WTPechkin nuget packages.
  • Add a singleton class to return IConverter

    public class TuesPechkinConverter
    {
    private static IConverter converter;
    
    public static IConverter Converter
    {
        get
        {
            if (converter == null)
            {
                converter =
            new ThreadSafeConverter(
                new PdfToolset(
                    new Win64EmbeddedDeployment(
                        new TempFolderDeployment())));
            }
    
            return converter;
        }
    }
    }
    
          

  • Add a main controller with the following code in the Index action:

    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" }    
        }
        };
    
        byte[] pdfBuf = TuesPechkinConverter.Converter.Convert(document);
        return File(pdfBuf, "application/pdf", "DownloadName.pdf");
    
          

+3


source to share


2 answers


As far as I know, you cannot get it to work in a web app . However, you can do this: you need to create a cloud service and add a worker role to it. VPERIKIN will be installed in this working role.

The workflow will be as follows: from your cloud web application, you will access a worker role (this thing is possible by configuring the worker role to host Asp.NET Web API 2). The worker role would configure the converter with WtterPekin and create a PDF file. We will finalize the PDF in response to the web api and send it back. Now let's do this ...

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

Your cloud service and your work role are created. The next thing to do is configure the worker role so that it can host ASP.NET Web API 2 . This configuration is pretty simple following this tutorial.

Once you have configured the Desktop Role to host the web api, you will have to install the wpww.whtmltox.Win64 and TuesdayPechkin nuget packages.

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

:

public class PdfController : ApiController
{
}

      

Add an action to our controller that will return an object HttpResponseMessage

.

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

      



Here we will configure two objects ObjectSettings

and GlobalSettings

that will be applied to the object HtmlToPdfDocument

. You now have two options.

You can generate a pdf from html text (you may have sent the html of your page in the request) or directly from the 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" }
    }
};

      

The good thing is, when using the page url, you can use an object ObjectSettings

to publish the parameters:

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

      

Also, from the WPechkin doc, the converter should be thread safe and should be stored somewhere static or as a singleton instance:

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

      

Finally, you finalize the pdf in the response content, set the response content type to the app / pdf, and add the content header and its:

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;

      

+2


source


I am afraid the answer to this question is not possible for wkhtmltopdf to work on Azure.

See this thread .

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

wkhtmltopdf uses the Window GDI API, which does not currently work on Azure Websites.

The use of Wtpechkin is supported



  • It supports .NET 2.0+, 32 and 64 bit processes and applications hosted in IIS.
  • Azure Websites does not currently support using wkhtmltopdf.

Bypass

I ended up creating an Azure Cloud Service running wkhtmltopdf.exe. I am posting html to the service and receiving byte [] in return.

+1


source







All Articles