【问题标题】:Libraries not loading for PDFTron using Dotnet core and javascript, issue with library simpe_wasm未使用 Dotnet 核心和 javascript 为 PDFTron 加载库,库 simpe_wasm 存在问题
【发布时间】:2020-07-29 19:22:16
【问题描述】:

我正在处理项目中的一个模块(.Net Core)的需求,该模块包含多个 PDF 操作,例如从 PDF 转换为 HTML,然后在 Web 视图编辑器中编辑 HTML,最后将其转换为PDF。

我使用提供的 JavaScript 代码来使用基于 Web 的 PDF 编辑器 (PDFTRON)。我在提供的官方文档PDFTron MANUAL INTEGRATION GUIDE 的帮助下尝试了手动集成。当我通过节点 js 运行时,我能够成功使用该库,但是当我在 Dotnet 核心中使用它时,它无法加载作为 memgrow.js 子部分的 simple_wasm/MemGrow.js.mem 文件,请在下面找到截图发生错误。

404 错误

为 Web 查看器配置的文件夹

代码如下:

索引.cshtml

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>
    Learn about <a href="https://docs.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
        });
    });

通过node js服务器运行时工作正常,截图如下,

寻求帮助以在 .net 核心中提供解决方案。

【问题讨论】:

  • 我怀疑可能没有为 IIS 设置 mime 类型,这就是它返回 404 的原因。尝试为此处列出的扩展添加 mime 类型pdftron.com/documentation/web/faq/mime-types/#pdf-and-office
  • @mparizeau 我在 IIS 中配置了文档链接中提供的所有 MIME 类型,但问题仍然存在。接下来我创建了新的网络解决方案并再次在 wwwroot 文件夹中配置了 webviewer,但没有成功,我不知道我还错过了什么。

标签: javascript .net pdf .net-core pdftron


【解决方案1】:

我能够重现并解决此问题:

在项目的根目录下,需要修改Startup.cs,添加正确的MIME Types,如@mparizeau suggested

// Microsoft.AspNetCore.StaticFiles needs to be added to use the FileExtensionContentTypeProvider type
// https://docs.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();
            });
        }
    }
}

【讨论】:

  • 我修改了你提供的代码,现在可以正常工作了。我很感谢你的支持。干杯:)
猜你喜欢
  • 2019-09-25
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-15
  • 2020-11-03
相关资源
最近更新 更多