如何使用pdfmake制作目录?

Pau*_*Kok 5 javascript pdfmake

是否可以使用pdfmake制作目录(TOC)?该库将为我生成PDF,但是我不知道某个对象将在哪一页上呈现。当然,这取决于页面大小,方向等。某些内容将流到下一页。我看不到如何预先计算章节的结尾。

考虑以下文档定义:

var dd = {
    footer: function(currentPage, pageCount) { return currentPage.toString() + ' of ' + pageCount; },
    content: [
        'Table of contents\n\n',
        'Chapter 1 ....... ?',
        'Chapter 2 ....... ?',
        'Chapter 3 ....... ?',
        'Chapter 4 ....... ?',
        {
            text: '',
            pageBreak: 'after'
        },
        {
            text: 'Chapter 1: is on page 2',
            pageBreak: 'after'
        },
        {
            stack: [
                'Chapter 2: is on page 3\n',
                'A LOT OF ROWS 2\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n',
                'A LOT OF ROWS 2\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n',
                'A LOT OF ROWS 2\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n',
                'A LOT OF ROWS 2\nWill go to the next page if this line contains a lot of text. Will go to the next page if this line contains a lot of text. Will go to the next page if this line contains a lot of text.'
                ],
            pageBreak: 'after'
        },
        {
            text: 'chapter 3: is on page 5',
            pageBreak: 'after'
        },
        {
            text: 'chapter 4: is on page 6'
        },
  ]
}
Run Code Online (Sandbox Code Playgroud)

要测试的最简单的方法就是将此dd对象粘贴到操场上:http : //pdfmake.org/playground.html

关于如何创建目录的任何想法?

Jey*_*ork 6

虽然直到最近才支持此功能,但您现在可以这样做

var docDefinition = {
  content: [
    {
      toc: {
        // id: 'mainToc'  // optional
        title: {text: 'INDEX', style: 'header'}
      }
    },
    {
      text: 'This is a header',
      style: 'header',
      tocItem: true, // or tocItem: 'mainToc' if is used id in toc
      // or tocItem: ['mainToc', 'subToc'] for multiple tocs
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

来源:https : //github.com/bpampuch/pdfmake#table-of-contents


请注意,这在最近的 0.1.28 版本中还没有。但我想它将包含在下一个中,同时您可以轻松地从源代码构建:

git clone https://github.com/bpampuch/pdfmake.git
cd pdfmake
npm install # or: yarn
git submodule update --init  libs/FileSaver.js
npm run build # or: yarn run build
Run Code Online (Sandbox Code Playgroud)

来源:https : //github.com/bpampuch/pdfmake#building-from-sources


我可以确认上面的 ToC 示例在这种情况下是有效的,只要您至少有一个tocItem. 由于空的 ToC 表,零tocItems 和 atoc当前将抛出异常。