2017-07-10 45 views
2

好吧,所以,即時通訊使用angular-cli爲我的測試項目,因爲即時通訊新的Web開發世界即時通訊嘗試新的想法。這一次我無法使它工作。如何在angular-cli上使用外部庫? (pdfmake)

那麼,我試過了嗎?

以及我那種跟着pdfmake自述,然後進入我的index.html我放在這兩條線體標籤中:

<script src='build/pdfmake.min.js'></script> 
<script src='build/vfs_fonts.js'></script> 

好,即時得到這個錯誤:

GET http://localhost:4200/build/pdfmake.min.js GET http://localhost:4200/build/vfs_fonts.js 404 (Not Found)

所以,我把比較遠,進入我的組件我只導入pdfmake,像這樣:

import * as pdfmake from 'pdfmake/build/pdfmake.js'; 

以及它幹什麼S'嗯,沒什麼,我仍然沒有把它叫做我的代碼中的任何地方,現在這樣做:

pdfmake.createPdf(this.docDefinition).download();

docDefinition代表我的pdf內容。

好的,現在呢?此錯誤出現:

ERROR Error: File 'Roboto-Regular.ttf' not found in virtual file system

好的,我可能需要導入vfs_font,在哪裏?好吧,我不知道,我嘗試在腳本標記的angular-cli.json導入,不,不工作,我甚至嘗試導入我的組件,不。

我試過用ng-pdf-make庫找到了ant npm站點,它根本不起作用。

我可能會犯新手錯誤,對不起,如果它的東西容易和愚蠢:P。

@Edit

我想他做同樣的事情,事實上,它使用jQuery的作品在我的項目,但它並不適用於pdfmake工作。

這是我的角cli.json腳本標籤:

"scripts": [ 
     "../node_modules/pdfmake/build/pdfmake.min.js", 
     "../node_modules/pdfmake/build/vfs_fonts.js", 
     "../node_modules/jquery/dist/jquery.js", 
     "../node_modules/tether/dist/js/tether.js", 
     "../node_modules/bootstrap/dist/js/bootstrap.js" 
     ], 

而且是事實,我不能使用pdfmake。createPdf( 「內容」),甚至與vfs_fonts導入:

import * as pdfmake from 'pdfmake/build/pdfmake.min.js'; 
import * as pdfFonts from 'pdfmake/build/vfs_fonts.js'; 
+0

你試過[這個](https://stackoverflow.com/questions/38855891/angular-cli-webpack-how-to-add-or-bundle-external -js文件)? –

+0

你的項目文件結構中的pdfmake.min.js和其他文件在哪裏?如果這是典型的cli項目,你應該把它們放在'dist'文件夾中 –

+0

[Angular Cli Webpack,如何添加或捆綁外部js文件?](https://stackoverflow.com/questions/38855891/angular- cli-webpack-how-to-add-or-bundle-external-js-files) – Surreal

回答

2

首先,你需要通過NPM

npm install pdfmake --save 

它會保存你的包在node_modules

後,您可以訪問安裝您pdfmake pdfmake這樣的

import * as pdfMake from 'pdfmake/build/pdfmake'; 
import * as pdfFonts from 'pdfmake/build/vfs_fonts'; 

before using pdf請初始化這樣

pdfMake.vfs = pdfFonts.pdfMake.vfs; 

例如: -

import { Component } from '@angular/core'; 
import * as pdfMake from 'pdfmake/build/pdfmake'; 
import * as pdfFonts from 'pdfmake/build/vfs_fonts'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    title = 'app works!'; 
constructor(){ 
    //called first time before the ngOnInit() 
    pdfMake.vfs = pdfFonts.pdfMake.vfs; 
     var dd = { content: 'your pdf data' }; 
    pdfMake.createPdf(dd).download(); 
    } 

} 

它應該工作。

你不需要在index.html或腳本中添加任何東西。

+0

Thanx爲答案的人,但我怎麼稱呼它? –

+0

pdfmake.createPdf(「你的數據」); – CharanRoot

+0

仍然收到相同的錯誤._ .:錯誤:在虛擬文件系統中找不到文件'Roboto-Regular.ttf' –

0

pdfmake.min.js位於node_modules中,那麼瀏覽器如何訪問node_modules中的pdfmake.min.js?只有dist文件夾內容可用於外部世界,因此可用於瀏覽器。

安裝NPM模塊@Jonnysai說和配置.angular-cli.json如下

{ 
... 
"scripts": ["node_modules/pdfmake/build/pdfmake.min.js"], 
... 
} 

Roboto-Regular.ttf複製所有其他靜態資產注入src/assets/文件夾中。在運輸過程中,整個assets文件夾將自動複製到dist。然後在你的index.html中,包括他們像assets/oboto-Regular.ttf

+0

我像@Jonnysai一樣安裝說,我在.angualr-cli.json中的腳本就像你的腳本。關於包含資產/ Roboto-Regular.ttf我需要使用樣式標籤或其他,即時消息仍然是新的HTML:P –

+0

然後嘗試通過手動複製到資產文件夾並相應地包含在HTML中。但我不推薦這種方式。但你可以試試。 –