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

.Net Core – Drop Down (Select) Won’t Populate

I had a recent bug that took me much too long to solve and the root of the cause was me. I got a bit too delete happy and deleted a crucial file that allows me to use Razor Tag Helpers to bring a List into a View as a Drop Down (Select).

The Error

Here’s how the error presented itself:

  • SelectList won’t populate
  • Select or DropDown is empty
  • value = Microsoft.AspNetCore.Mvc.Rendering.SelectList or
  • System.Linq.OrderedEnumerable2 [Microsoft.AspNetCore.Mvc.Rendering.SelectListItem, System.String]
Continue Reading

I’m a Front-End Cheater

I am a full stack developer. I develop in Visual Studio, code in C# with .NET Core, deploy to Azure and I must admit, I’m a front-end cheater.

Comeon, the .NET Core Web App demo itself loads with Bootstrap and JQuery preinstalled. We’re all cheaters at some point, but I’m trying to diversify my cheating and eliminate my dependencies.

Here’s my latest strategy.

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

PayTrace: 400 Bad Request For Declined Payment

TLDR:  post IS successful.  PayTrace, by design, returns a 400 error, which sets off exceptions in httpresponse.  Solution: catch the exception and then continue deserializing your response.

I coded a few weeks ago a .NET post to the PayTrace API which helps me demo and test payment by credit card using client side encryption.  The process more or less went like this:

  • Create demo account as a merchant on Paytrace
  • Download PEM key
  • On submit of form with credit card information, an imported PayTraceJS library encrypts the card number and csc code
  • Use the demo account’s username and password to submit a request for a token
  • Submit transaction (which includes encrypted info as well as other required fields) using token and await response

A successful http response returns a status code of 200.  I read it via stream, deserialize it using json into my CardResponse object (both successful and failure responses have the same design).  Everything went great until I began testing rejected cards.

Continue Reading

Azure Blob’s Ghost Folders?

This week I had to address a upload image to blob application that I had built in my development environment, was working fine, but needed to be configured to work in production.  For the application overall, I used Azure Samples for Upload Image to Storage (built in .NET Core).  In it, the configuration in appsettings.json looks like this:

"AzureStorageConfig": {
    "AccountName": "",
    "AccountKey": "",
    "ImageContainer": "images",
    "ThumbnailContainer": "thumbnails"
}

 

Account Name account name and AccountKey are easily found in Azure Portal, for container I used Azure Storage Explorer just so I could get a full look at the container and its blobs.  The problem was, in my development environment I was uploading DIRECTLY to container.  In the example above, I was uploading to the “images” container.  In my Production environment, though, my ImagesContainer had two folders:  images/small and images/large.  I tried to change the “ImagesContainer”:”images” to “ImagesContainer”:”images/small”, “ImagesContainer”:”images\small” and no go.  Requested URI not found.

Continue Reading