Print PDF’s on Azure Using an API and RazorLight (Update 1)

The API mentioned in my first tutorial got taken down and so I had to do some updates.  First, choose another 3rd party API.  I currently am trying out  HTML 2 PDF Rocket and modified my code a bit:

[HttpGet]
public async Task<FileStreamResult> PrintAsync(int id)
{
    InvoiceVM invoiceVM = new InvoiceVM();
    invoiceVM = invoiceRepository.Get(id);

    var engine = new RazorLightEngineBuilder()
      .UseFilesystemProject(_hostingEnvironment.WebRootPath + "\\pdf\\")
      .UseMemoryCachingProvider()
      .Build();

    var view = await engine.CompileRenderAsync("PDF.cshtml", invoiceVM);
    string apiKey = "xxxxxxxxxxxxxxxxxxxx";

    using (var client = new WebClient())
    {
        // Build the conversion options
        NameValueCollection options = new NameValueCollection();
        options.Add("apikey", apiKey);
        options.Add("value", view);
        options.Add("MarginLeft", "10");
        options.Add("MarginRight", "10");
        options.Add("MarginTop", "10");
        options.Add("MarginBottom", "10");
        options.Add("PageSize", "Letter");

        MemoryStream ms = new MemoryStream(client.UploadValues("http://api.html2pdfrocket.com/pdf", options));

        return new FileStreamResult(ms, "application/pdf");
    }
}

This is based directly off the HTML 2 Rocket documentation.  For the rest of the code see the first tutorial.

 

Continue Reading

Print PDF’s on Azure Using an API and RazorLight

The Problem

I recently had an issue with printing a report to PDF using Microsoft Reporting Service and a RDLC file, etc. Something similar to this. Unfortunately, it worked great in development, but refused to work once deployed into Azure. No matter what I did, I could not duck the GDI errors I kept getting, and apparently this continues through a line of various PDF exporting extensions, all of which rely on GDI for export. Turns out, I’m not alone in facing this problem and so, I decided to find a solution.

The Solution

My general idea was to use something to render my PDF view, send that view as one long html string to a free PDF microservice and get the PDF in return.

Continue Reading

Using Async, Await, Plus a Slight Delay

Today’s problem dealt with how we view our invoices online.  We use an app on the iSeries that creates a PDF and delivers it to a set destination.  That destination, in our case is a regular windows server, the files landing in a small site:   pdf.mycompany.com.

My initial approach was simple, use the PHP API I have sitting on the iSeries to make a call to the program – passing it the parameters for that specific invoice, await response (which gave me the new created filename) and then redirect to that URL.  The method looks something like this:

[HttpGet]
public async Task<ActionResult> GetInvoiceAsync(int invoice)
{
    GetInvoice getInvoice = new GetInvoice();
    var client = new HttpClient();

    string fileName = await getInvoice.LoadPDF(invoice);

    string url = "http://pdf.mycompany.com/";
    url += fileName + ".pdf";

    return Redirect(url);
}

 

This worked great… 90% of the time, but the other 10% of the time, I clicked too quickly on an invoice and got forwarded to a 404.

Continue Reading