代码之家  ›  专栏  ›  技术社区  ›  Softhinker.com

角度2:如何使用pdfmake库

  •  13
  • Softhinker.com  · 技术社区  · 7 年前

    pdfmake

    "scripts": [
        "../node_modules/pdfmake/build/pdfmake.js",
        "../node_modules/pdfmake/build/vfs_fonts.js"
      ]
    

    然后在app.component中。ts,我是这样使用的:

    import * as pdfMake from 'pdfmake';
    
    @Component({
    selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent implements OnInit {
       pdf: any;
       downloadPdf() {
        let item = {firstName: 'Peter', lastName: 'Parker'};
        this.pdf = pdfMake;
        this.pdf.createPdf(buildPdf(item)).open();
      }
    }
    
    function buildPdf(value) {
       var pdfContent = value;
       var docDefinition = {
         content: [{
        text: 'My name is: ' + pdfContent.firstName  + ' ' + pdfContent.lastName + '.'
         }]
       }
       console.log(pdfContent);
       return docDefinition;
    }
    

    加载应用程序时,我在浏览器控制台中遇到以下错误:

    Uncaught TypeError: fs.readFileSync is not a function
    at Object.<anonymous> (linebreaker.js:15)
    at Object.<anonymous> (linebreaker.js:161)
    at Object.../../../../linebreak/src/linebreaker.js (linebreaker.js:161)
    at __webpack_require__ (bootstrap b937441…:54)
    at Object.../../../../pdfmake/src/textTools.js (textTools.js:4)
    at __webpack_require__ (bootstrap b937441…:54)
    at Object.../../../../pdfmake/src/docMeasure.js (docMeasure.js:4)
    at __webpack_require__ (bootstrap b937441…:54)
    at Object.../../../../pdfmake/src/layoutBuilder.js (layoutBuilder.js:7)
    at __webpack_require__ (bootstrap b937441…:54)
    

    复制pdfmake。js和vfs_字体。js到assets文件夹,然后将其添加到index.html:

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

    import * as pdfMake from 'pdfmake';
    

    并将其添加到app.component.ts:

    declare var pdfMake: any;
    

    "../node_modules/pdfmake/build/pdfmake.js",
    "../node_modules/pdfmake/build/vfs_fonts.js"
    

    这是可行的,但仍然是一个解决办法。

    4 回复  |  直到 7 年前
        1
  •  27
  •   Softhinker.com    7 年前

    第一

    npm install pdfmake --save
    

    其次,在typings.d.ts中添加以下内容:

    declare module 'pdfmake/build/pdfmake.js';
    declare module 'pdfmake/build/vfs_fonts.js';
    

    import * as pdfMake from 'pdfmake/build/pdfmake.js';
    import * as pdfFonts from 'pdfmake/build/vfs_fonts.js';
    pdfMake.vfs = pdfFonts.pdfMake.vfs;
    

    最后,使用pdfMake。xxx()和往常一样。

        2
  •  13
  •   benny_boe    7 年前

    .angular-cli.json 在TS文件中添加导入。看看 Global scripts

    一旦通过脚本数组导入库,您应该

    (没有输入 pdfmake 因此,在卸载配置文件时,您需要声明它们。)

    所以如果你想把它添加到你的TS文件中…替换你的 import * as pdfMake from 'pdfmake'; (它是服务器端版本!)使用客户端版本( 'pdfmake/build/pdfmake' 'pdfmake/build/vfs_fonts' )否则你也会出错。

    当您的导入看起来像这样时,它应该可以工作:

    import pdfMake from 'pdfmake/build/pdfmake';
    import pdfFonts from 'pdfmake/build/vfs_fonts';
    pdfMake.vfs = pdfFonts.pdfMake.vfs;
    
        3
  •  3
  •   Shift 'n Tab    6 年前

    Stories Global Scripts 如果你仔细遵循说明。 如果 图书馆没有打字机。

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

    关于src/typings.d.ts文件

    添加 declare var pdfMake: any; 下面的行。

    pdfMake 全局变量现在可用。

    ngOnInit() { console.log(pdfMake); }
    

    输出

    {
        createdPdf: f(t),
        vfs: {
            Roboto-Italic.ttf: "some long encoded string...",
            Roboto-Medium.ttf: "some long encoded string...",
            Roboto-MediumItalic.ttf: "some long encoded string...",
            Roboto-Regular.ttf: "some long encoded string...",
        }
    }
    

    这意味着它可以随时使用。

        4
  •  1
  •   Ayush Bhadauria    5 年前
    npm i pdfmake;
    
    import * as pdfMake from 'pdfmake/build/pdfmake';
    import * as pdfFonts from 'pdfmake/build/vfs_fonts';
    
    pdfMake.vfs = pdfFonts.pdfMake.vfs;
      var dd = {
        content: [
          {
            layout: 'lightHorizontalLines', // optional
            table: {
              // headers are automatically repeated if the table spans over multiple pages
              // you can declare how many rows should be treated as headers
              headerRows: 1,
              widths: [ '*', 'auto', 100, '*' ],
    
              body: [
                [ 'First', 'Second', 'Third', 'The last one' ],
                [ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ],
                [ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ]
              ]
            }
          }
        ]
      };
    pdfMake.createPdf(dd).download();