1

I am working with a requirement for one of the modules in the project(.Net Core) which comprises multiple PDF operations, like converting from PDF to HTML, then editing the HTML in a web view editor, finally converting it into a PDF.

I used JavaScript code provided to use a web-based PDF editor (PDFTRON). I tried manual integration with the help of provided official documentation PDFTron MANUAL INTEGRATION GUIDE. I am able to successfully use the library when I run through node js, but when I am using it in Dotnet core it's failing to load simple_wasm/MemGrow.js.mem file which is a subpart of memgrow.js, please find below screenshot for error occurred.

404 error enter image description here

Folder configured for web viewer enter image description here

Code is as follows:

Index.cshtml

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>
    Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.
    <label for="file_upload">Choose A file</label>
    <input type="file" id="file_upload" name="file_upload" accept=".pdf">
</p>
</div>
<div id='viewer' style='width: 1024px; height: 600px;'> </div>
<script src="~/WebViewer/lib/webviewer.min.js"> </script>
<script> 
const input = document.getElementById('file_upload');
  WebViewer({
    path: 'WebViewer/lib', // path to the PDFTron 'lib' folder on your server
    licenseKey: 'Insert commercial license key here after purchase',    
    initialDoc: 'files/sample.pdf',  // You can also use documents on your server
  }, document.getElementById('viewer'))
    .then(instance => {
        const docViewer = instance.docViewer;
        const annotManager = instance.annotManager;
        // const Annotations = instance.Annotations;
        input.addEventListener('change', () => {
            debugger;
            // Get the file from the input
            const file = input.files[0];
            instance.loadDocument(file, { filename: file.name });
        });

        docViewer.on('documentLoaded', () => {
            // call methods relating to the loaded document
        });
    });

Working fine when running through node js server, screenshot as follows, Run with node.js

Looking for assistance to provide solution in .net core.

  • I suspect the mime types might not be set for IIS and that's why it's returning a 404. Try adding the mime types for the extensions listed here https://www.pdftron.com/documentation/web/faq/mime-types/#pdf-and-office – mparizeau Jul 30 '20 at 20:21
  • @mparizeau i configured all the MIME Types in IIS which were provided in the documentation link, still the problem persists. Next i created new web solution and configured webviewer in wwwroot folder again, but didn't work out, i dont know what i am missing out still. – Shashank S Chandel Aug 02 '20 at 17:25

1 Answers1

2

I was able to reproduce and resolve this issue:

webviewer running in aspdotnetcore

In the project's root directory, Startup.cs needs to be modified to add the correct MIME Types, as @mparizeau suggested.

// Microsoft.AspNetCore.StaticFiles needs to be added to use the FileExtensionContentTypeProvider type
// https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.staticfiles.fileextensioncontenttypeprovider?view=aspnetcore-3.1
using Microsoft.AspNetCore.StaticFiles;
// ...

namespace aspnetcoreapp
{
    public class Startup
    {
      // ...
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var provider = new FileExtensionContentTypeProvider();
            provider.Mappings[".res"] = "application/octet-stream";
            provider.Mappings[".mem"] = "application/octet-stream";
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles(new StaticFileOptions(){
                ContentTypeProvider = provider
            });

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

CorreyL
  • 118
  • 1
  • 9