【问题标题】:How to make a table of contents using pdfmake?如何使用 pdfmake 制作目录?
【发布时间】:2016-04-12 15:41:14
【问题描述】:

是否可以使用 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 对象粘贴到 Playground 中:http://pdfmake.org/playground.html

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

【问题讨论】:

    标签: javascript pdfmake


    【解决方案1】:

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

    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 示例在这种情况下是有效的。由于 ToC 表为空,零 tocItems 和 toc 目前将引发异常。

    【讨论】:

      猜你喜欢
      • 2019-12-10
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      • 2015-10-23
      • 1970-01-01
      • 2012-11-05
      相关资源
      最近更新 更多