linkedinfacebooktwittermenuarrow-up

GemBox Support Forum

Saving as PDF in WASM Blazor Project

Hi All,

Has anyone had luck getting GemBox.Spreadsheet to save as PDF in a wasm Blazor project?
This is my basic test method…

            SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

            var workbook = new ExcelFile();
            var worksheet = workbook.Worksheets.Add("Hello World");

            worksheet.Cells[0, 0].Value = "Hello";
            worksheet.Cells[0, 1].Value = "World";

            using (var stream = new MemoryStream())
            {
                workbook.Save(stream, SaveOptions.PdfDefault); //Exception thrown here.
                await SaveAs("test.pdf", stream.ToArray());
            }

Exception is thrown on workbook.save

Exception : ‘The type initializer for ‘’ threw an exception.’
Inner Exception : Could not load file or assembly 'PresentationCore, Version=4.0.0.0

any hints on how to solve this one will be well recieved.

Thankyou :slight_smile:

Hi,

What version of GemBox.Spreadsheet are you using?

It seems like the runtime requires WPF assemblies, but note that GemBox.Spreadsheet’s PDF writer no longer uses WPF starting from GemBox.Spreadsheet 4.5 version 1131.

So, can you try with the current latest version?
If the problem remains, please send us a small Visual Studio project that reproduces your issue so that we can investigate it.

Regards,
Mario

Hi Mario,

thanks for your speedy reply.
I’m using NuGet 'Latest Stable : 49.0.1103"

But from your message, it seems this is in-fact not the latest version.
I’m completely confused with your version numbering.

On this page…

We can see that the latest version is 49.0.310.1103
I’m confused. how do I find 1131 ??

Thanks :slight_smile:

The first number is the major version, so in your case “49” means you’re using GemBox.Spreadsheet 4.9.
The third number is the platform, in your case “310” means you’re using GemBox.Spreadsheet for .NET Core 3.1.
And the last number is the minor version, in your case “1103”.

So in short, you are using the latest version, but for WASM you should use GemBox.Spreadsheet for .NET Standard 2.0 (version “49.0.200.1103”).
Also, you’ll probably need to add its dependencies, you can see those on the “Dependencies” tab on NuGet:

I hope this helps.
If your problem remains, please send us a small project that shows what you currently have and I’ll help you to resolve its problem.

Regards,
Mario

Thanks for your help on this! :slight_smile:
I did try using the version suggested. Infact I think I was using that version anyway, it’s just that the version number (200/300) is not displayed on VS (probably is somewhere, but i cannot find it).
I’m going to take a different approach.
Your library works well on the server side, so we’ll just adjust an stream content.
Once I get couple minutes free, I’ll send you a repo. (standard Blazor WebAssembly template reproduces it)
Thankyou :slight_smile:

Try this:

  1. Create a “Lib” folder inside your project.
  2. Download the latest version of GemBox.Spreadsheet for .NET Standard:
    GemBox.Spreadsheet Bug Fixes
  3. Unzip it and add the “GemBox.Spreadsheet.dll” file into a “Lib” folder.
  4. Add a reference to GemBox.Spreadsheet.dll and its dependencies.
    In other words, edit the “.csproj” file like this:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.7" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.7" PrivateAssets="all" />
    <PackageReference Include="HarfBuzzSharp" Version="2.8.2" />
    <PackageReference Include="Portable.BouncyCastle" Version="1.8.9" />
    <PackageReference Include="SkiaSharp" Version="2.80.3" />
    <PackageReference Include="System.Drawing.Common" Version="5.0.0" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
  </ItemGroup>

    <ItemGroup>
        <Reference Include="../Lib/GemBox.Spreadsheet.dll">
            <HintPath>Lib\GemBox.Spreadsheet.dll</HintPath>
            <EmbedInteropTypes></EmbedInteropTypes>
        </Reference>
    </ItemGroup>

</Project>

Does this work for you?

Last just as an FYI, the problem is that GemBox.Spreadsheet for .NET Core 3.1 has some features that only work on Windows, they have WPF dependency.
And the NuGet resolution ends up using GemBox.Spreadsheet for .NET Core 3.1 instead of GemBox.Spreadsheet for .NET Standard 2.0.

However, note that we do plan to address this in the future. We will probably isolate the Windows-only features into a build that targets “net6.0-windows”, we plan to introduce this next year.

I hope this helps.

Regards,
Mario