7

是否可以使用 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'
        },
  ]
}

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

关于如何创建 TOC 的任何想法?

4

1 回答 1

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
    }
  ]
}

来源: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

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


我可以确认,只要您至少有一个tocItem. 由于 ToC 表为空, zero tocItems 和 atoc当前会抛出异常。

于 2017-05-06T15:03:42.223 回答