facebooktwittermenuarrow-up

GemBox Support Forum

Create PDF in Blazor WebAssembly App

I may be pushing the library beyond its current limits, but I hope not. Equally, this could be something I’ve missed. My ultimate goal is to create a WASM in Blazor that will store the data client-side, which is then used to generate a client-side PDF based on a Word template. (think offline order taking app)

The following code is run off of a button onClick in “Index.razor”:

public void onClick()
{
    // If using Professional version, put your serial key below.
    ComponentInfo.SetLicense("FREE-LIMITED-KEY");

    DocumentModel document = new DocumentModel();

    Section section = new Section(document);
    document.Sections.Add(section);

    Paragraph paragraph = new Paragraph(document);
    section.Blocks.Add(paragraph);

    Run run = new Run(document, "Hello World!");
    paragraph.Inlines.Add(run);

    using (var stream = new MemoryStream())
    {
        document.Save(stream, SaveOptions.PdfDefault);
        FileUtil.SaveAs(js, "HelloWorld.pdf", stream.ToArray());
    }
}

The execution fails at the document.Save() line with

Execution

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: The type initializer for ‘D’ threw an exception.
System.TypeInitializationException: The type initializer for ‘D’ threw an exception. —> System.NullReferenceException: Object reference not set to an instance of an object.
at ​. () <0x386bbe0 + 0x00102> in :0
at D…ctor () <0x3840a58 + 0x00114> in :0
at D…cctor () <0x383f518 + 0x0000c> in :0
— End of inner exception stack trace —
at ​. () <0x38389b0 + 0x00032> in :0
at . ( ​ ) <0x38387b8 + 0x00010> in :0
at . ( ​ ) <0x3838588 + 0x00024> in :0
at ​. () <0x38381c8 + 0x00002> in :0
at ​. ( ​ ) <0x38303f8 + 0x00046> in :0
at ​. ( ​ , System.Int32 , ​ , System.Boolean , ​& ) <0x382fa80 + 0x0000a> in :0
at ​. ( ​ , System.Int32 , ​& ) <0x3825290 + 0x000a8> in :0
at B. ( ​ , System.Int32 ) <0x3824d20 + 0x0001c> in :0
at B. (System.String , System.Boolean , System.Boolean , System.Int32 ) <0x3824660 + 0x00020> in :0
at . (GemBox.Document.CharacterFormat , , [] , B , System.Int32 ) <0x3804710 + 0x0014c> in :0
at . (GemBox.Document.CharacterFormat , , [] , B ) <0x3801238 + 0x00014> in :0
at . ( ) <0x38010e0 + 0x0003c> in :0
at . () <0x3775370 + 0x00190> in :0
at GemBox.Document.DocumentModel. (GemBox.Document.PaginatorOptions , ​ ) <0x3746908 + 0x000b2> in :0
at GemBox.Document.PdfSaveOptions.67tg5rkvl9zwp8v7723mndgzuc3b4vj8 (GemBox.Document.DocumentModel , System.IO.Stream , System.String ) <0x372a030 + 0x00030> in :0
at GemBox.Document.DocumentModel.Save (System.IO.Stream stream, GemBox.Document.SaveOptions options) <0x3729c00 + 0x0004c> in :0
at WebApp.Pages.Index.onClick () [0x0005b] in C:\Source\BlazorTestApp\WebApp\Pages\Index.razor:32
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T] (System.MulticastDelegate delegate, T arg) <0x35ca030 + 0x0006e> in :0
at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync (System.Object arg) <0x35c9f70 + 0x0000a> in :0
at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync (Microsoft.AspNetCore.Components.EventCallbackWorkItem callback, System.Object arg) <0x35c9ed0 + 0x0000a> in :0
at Microsoft.AspNetCore.Components.EventCallback.InvokeAsync (System.Object arg) <0x35cda18 + 0x00040> in :0
at Microsoft.AspNetCore.Components.RenderTree.Renderer.DispatchEventAsync (System.UInt64 eventHandlerId, Microsoft.AspNetCore.Components.RenderTree.EventFieldInfo fieldInfo, System.EventArgs eventArgs) <0x35ccf90 + 0x000a8> in :0

GemBox.Document v33.0.1000 and its dependencies are installed from NuGet. The only discrepancy I can see is that the project is .NET Standard 2.1, not 2.0… if that makes a difference?

Many thanks in advance for your help

Hi,

Please try again with the latest version from our BugFixes page or NuGet.

Also, note that you’ll need to include fonts in your application so that PDF files can be generated.
For instance, you can add them as embedded fonts, see FontsBaseResourceLocation property remarks.
For the example that you provided, you’ll need to include “Calibri” because that is a default font.

Last, GemBox.Document’s PDF writer currently uses System.Drawing for processing images.
But I’m afraid that this doesn’t work on Blazor WASM which is why GemBox.Document cannot create PDF files with images.

Nevertheless, note that we do have plans for investigating the possibility of removing/replacing this dependency in the current road-map.
At the moment, I don’t have any release date to share, but if I had to guess I would say that we’ll probably start working on this before the end of this year.

Regards,
Mario

Mario,

Thanks for your help. I now have the demo code working, however the final application will have the need to render images. It’s all proof of concept at the moment, so hopefully your team will have solved the System.Drawing issue by the time this goes ahead.

Many thanks