Generate PDF files using an html template and Playwright

  • Gérald Barré
There are many ways to generate PDF files. You can use a PDF library, but generating a PDF manually is not trivial. You can use a report library, but they are often paid solution and not trivial to use for simple needs. In this post, we'll use an html template and Playwright to generate a PDF file. Using a browser to convert an html file to a PDF file is very convenient as it allows to use all html/CSS… [read more]

Enabling Reproducible builds when building NuGet packages

  • Gérald Barré
Reproducible builds are important when building NuGet packages from public sources. Indeed, it gives your consumers confidence in your packages by allowing them to validate the package has actually been built using the public sources. To be able to reproduce a build, you need the source files, the referenced DLLs, the compiler version, and the compiler options (language version, defines, nullables, etc.)… [read more]

Prevent refreshing the UI after an event in Blazor

  • Gérald Barré
If you want to improve the performance of a Blazor application, you may want to reduce the number of time the UI is recomputed. In Blazor, this means reducing the number of time the StateHasChanged method is called. This method needs to be called only when the state has changed and the UI needs to be updated. However, there are multiple ways to implicitly call it. One of them is to handle an event such as… [read more]

Reducing the length of GUIDs in URLs or Json payloads

  • Gérald Barré
GUIDs are commonly used as a key in a database. If you want to create a URL to a specific record, you may want to use the GUID in the URL. Now you have a problem: how to display the GUID in the URL? The easy solution is to use ToString("N"), but this will return the GUID in the format 00000000000000000000000000000000. However, this format is inefficient. You can use ToBase64String to get the GUID in a… [read more]

Configuring JSON options in ASP.NET Core

  • Gérald Barré
In an ASP.NET Core application, you can configure the JSON serializer options used by controllers using the AddJsonOptions method: public void ConfigureServices(IServiceCollection services) { services.AddControllers() .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); } This works well for controllers. .NET 6 introduced minimal APIs for hosting and routing in web… [read more]

Upload files with drag & drop or paste from clipboard in Blazor

  • Gérald Barré
To upload a file you can use the FileUpload control. This control generates a <input type="file"> element and allows you to upload a file. To get a good user experience, you also need to support dropping files from the explorer or pasting images from the clipboard. The final result should look like this: First, you need to create a JavaScript file to register some events to handle drag and drop events and… [read more]

Removing elements after an animation in Blazor

  • Gérald Barré
There are many cases where you want to smoothly remove an element from the UI. A solution is to use an animation to fade out the element before removing it. You can start the animation by adding a class to the element you want to remove. Then, you can use the transitionend or animationend event to actually remove the element. First, you need to register the transitionend event handler, so you can use it… [read more]