2016-04-12 55 views
3

是否可以使用pdfmake創建內容表(TOC)?該庫將爲我生成PDF,但我不知道某個對象將在哪個頁面上呈現。當然,這取決於頁面大小,方向等。一些內容將流向下一頁。我無法預見如何提前計算一章的最終結果。如何使用pdfmake製作目錄?

考慮這個文檔定義:

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任何想法?

回答

2

雖然這是不支持,直到最近,你現在可以做

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


我可以確認的是,上述的ToC的例子就是在這種情況下,只要功能因爲您至少有一個tocItem。零tocItemtoc目前將由於空的ToC表而引發異常。