简体   繁体   English

PDFTron 库不适用于 Angular 组件库

[英]PDFTron lib not working on Angular component library

I started my first Angular component library where I am adding PDFTron lib so I can use my wrapper lib on all of my projects.我开始了我的第一个 Angular 组件库,我在其中添加了PDFTron库,这样我就可以在我的所有项目中使用我的包装器库。 Direct PDFTron integration working fine with library path but with my Angular component library, it's not taking library.直接PDFTron集成与库路径工作正常,但与我的 Angular 组件库一起使用,它不占用库。 See more details as mentioned below:查看更多详细信息,如下所述:

My Code to load PDFTron Library:我加载 PDFTron 库的代码:

PDFViewerPage (Direct Use of PDFtron Library ): PDFViewerPage(直接使用PDFtron Library ):

WebViewer({
      path: './assets/lib/',
      // initialDoc: '../../../assets/data/files/webviewer-demo-annotated.pdf'
      initialDoc: this.initialDoc
    }, this.viewer.nativeElement).then(instance => {
      console.log(instance);
      this.wvInstance = instance;
    }).catch(error => {
      console.log("PDF error:",error);
 })

My Custom NPM library:我的自定义 NPM 库:

WebViewer({
      path: this.libPath,
      initialDoc: this.initialDoc
    }, this.viewer.nativeElement).then(instance => {
      console.log(instance);
      this.wvInstance = instance;
    }).catch(error => {
      console.log("PDF error:",error);
 })

this one is not working as my library code is in @Node-Modules and my lib folder is in ./assets folder in the project.这个不起作用,因为我的库代码在@Node-Modules中,而我的 lib 文件夹在项目的./assets文件夹中。

Folder Structure of assets:资产的文件夹结构: 在此处输入图像描述

The library can't be accessible in my custom library how can I access PDFTron library in my custom NPM?在我的自定义库中无法访问该库如何访问我的自定义 NPM 中的 PDFTron 库?

Error:错误:

ERROR Error: Uncaught (in promise): TypeError: pdftron_webviewer__WEBPACK_IMPORTED_MODULE_0 _ is not a function TypeError: pdftron_webviewer__WEBPACK_IMPORTED_MODULE_0 _ is not a function at MyFirstNpmProjComponent.ngAfterViewInit (my-first-npm-proj.mjs:36:9) at callHook (core.mjs:2533:22) at callHooks (core.mjs:2502:17) at executeInitAndCheckHooks (core.mjs:2453:9) at refreshView (core.mjs:9555:21) at refreshComponent (core.mjs:10655:13) at refreshChildComponents (core.mjs:9280:9) at refreshView (core.mjs:9534:13) at renderComponentOrTemplate (core.mjs:9598:9) at tickRootContext (core.mjs:10829:9) at resolvePromise (zone.js:1255:1) at resolvePromise (zone.js:1209:1) at zone.js:1321:1 at ZoneDelegate.invokeTask (zone.js:434:1) at Object.onInvokeTask (core.mjs:25463:33) at ZoneDelegate.invokeTask (zone.js:433:1) at Zone.runTask (zone.js:205:1) at drainMicroTaskQueue (zone.js:620:1) ERROR Error: Uncaught (in promise): TypeError: pdftron_webviewer__WEBPACK_IMPORTED_MODULE_0 _ is not a function TypeError: pdftron_webviewer__WEBPACK_IMPORTED_MODULE_0 _ is not a function at MyFirstNpmProjComponent.ngAfterViewInit (my-first-npm-proj.mjs:36:9) at callHook (core.mjs :2533:22) 在 callHooks (core.mjs:2502:17) 在 executeInitAndCheckHooks (core.mjs:2453:9) 在 refreshView (core.mjs:9555:21) 在 refreshComponent (core.mjs:10655:13) 在refreshPromise (zone.js: zone.js: 1255:1) 在 resolvePromise (zone.js:1209:1) 在 zone.js:1321:1 在 ZoneDelegate.invokeTask (zone.js:434:1) 在 Object.onInvokeTask (core.mjs:25463:33) 在ZoneDelegate.invokeTask (zone.js:433:1) 在 Zone.runTask (zone.js:205:1) 在 drainMicroTaskQueue (zone.js:620:1)

I am not sure what path I am missing or any extra setup is required for this?我不确定我缺少什么路径或需要任何额外的设置?

You could edit your assets option in angular.json.您可以在 angular.json 中编辑您的资产选项。 Like喜欢

assets: [
 "src/asset/lib"
]

You can check this for reference: https://angular.io/guide/workspace-config#asset-config .您可以查看以下内容以供参考: https://angular.io/guide/workspace-config#asset-config Hope this one can help希望这个可以帮助

I recently ran into this exact same issue.我最近遇到了同样的问题。 While I'm not 100% sure of the cause, it seems like the webpack dev server is packaging the pdftron library in an unexpected way.虽然我不能 100% 确定原因,但似乎 webpack 开发服务器正在以一种意想不到的方式打包 pdftron 库。

I have been able to solve the issue in my project.我已经能够在我的项目中解决这个问题。 My project is using Angular v13.3 and PDFTron Webviewer v8.5 from NPM.我的项目使用 NPM 的 Angular v13.3 和 PDFTron Webviewer v8.5。

Two changes were made to my project:我的项目做了两个改动:

First: We see from the PDFTron Angular docs that we should add the pdftron script to the project in angular.json .首先:我们从PDFTron Angular 文档中看到,我们应该将 pdftron 脚本添加到angular.json中的项目中。 Don't follow the doc exactly.不要完全按照文档。 We use the path to node_modules .我们使用node_modules的路径。

"scripts": [
  "node_modules/@pdftron/webviewer/webviewer.min.js"
],

Second: We need to use the WebViewer which comes from webviewer.min.js .第二:我们需要使用来自webviewer.min.jsWebViewer This part seems strange and like it should work... but, don't import it at all.这部分看起来很奇怪,应该可以工作......但是,根本不要import它。

Again from PDFTron docs :再次来自PDFTron 文档

declare const WebViewer: any;

With this set, I am able to construct an Angular component in my library and then use it in several applications.有了这个集合,我可以在我的库中构建一个 Angular 组件,然后在多个应用程序中使用它。

Again, I'm not exactly sure what webpack is doing wrong, but this seems to work around the issue.同样,我不确定 webpack 做错了什么,但这似乎可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM