2014-05-13 57 views
0

您好我正在使用JSPDF通過創建單元格來手動生成PDF。如何使JSPDF的單元格中心的文字

我必須指定單元格的高度,但是當文本的大小更多的則是過來拍打細胞。我怎樣才能避免這一點。

enter image description here

這裏是我的代碼:

  var l = { 
        orientation: 'l', 
        unit: 'mm', 
        format: 'a4', 
        compress: true, 
        fontSize: 8, 
        lineHeight: 1, 
        autoSize: false, 
        printHeaders: true 
       }, pdf = new jsPDF(l, '', '', ''), i, j, margins = { 
        top: 30, 
        bottom: 10, 
        left: 10, 
        width: 25 
       }; 

       //initializing the cells 
       pdf.cellInitialize(); 

       var lines = pdf.splitTextToSize(' This is large icon to f', 12); 
       pdf.cell(margins.left, margins.top, 14, 8, lines, 0); 

       pdf.save('Te.pdf'); 

現在我可以增加細胞的高度,但文本仍然在圈細胞。

下面是它的樣子。

enter image description here

誰能幫我。我使用JSPDF https://github.com/MrRio/jsPDF

回答

1

你必須使用JSPDF這個?

你可以很容易地實現你pdfmake(http://pdfmake.org)所需要的。

的理念是有點不同,但 - pdfmake是自動計算佈局的聲明庫。

基本表的定義是這樣的:

var docDefinition = { 
    content: [ 
    { 
     table: { 
     // header rows 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' ] 
     ] 
     } 
    } 
    ] 
}; 

還有用於COL /行跨度,牢不可破行支持(萬一有沒有離開當前頁面上有足夠的空間),自動調整大小,固定尺寸和星形大小的柱子。

你可以檢查在pdfmake playground

+0

我甚至看過pdfmake,但它沒有提供任何文檔,也沒有說明它會支持IE8 + – Moiz

+0

目前大多數選項都在[入門]中描述(http://pdfmake.org/#/gettingstarted) ... IE8 - 我將不得不考慮;) – bartekp

+0

謝謝:) IE8是最大的問題。 – Moiz

0

我來自同一個問題所遭受的表的例子。

但如果你看看jspdf.plugin.cell.js,它的實際繪製它包裝在文本的矩形。換句話說,細胞本身是一個矩形。

裏面的 jsPDFAPI.cell()功能, this.text(txt, x + 3, y + h - 3);, 此行實際上控制了文本將出現在每個單元的一側。

你可以這樣做: this.text(txt, x + 3, y + h/2 - 3);,那麼文本將居中在一個單元格中。

希望這會有所幫助。

+0

感謝您的幫助。通過對JSPDF的基礎庫進行一些更改,我早已解決了這個問題。 – Moiz

相關問題