Как преобразовать таблицу html в pdf?

#javascript #html #xhtml #jscript

#javascript #HTML #xhtml #jscript

Вопрос:

Я хочу сгенерировать эту таблицу в PDF-файл. но он показывает только один столбец.

В строке ( <tr> ) я использовал 2 столбца ( <td> </td> <td> </td> ).

Но он не показывает второй столбец.

Это таблица.

 <table class="table">  
    <tr>
        <th colspan="2"> <strong> Student Details</strong><th></th> </th> 

    </tr>

   <tr>
       <td style="width: 100px;">
           <h:outputText value="Student Name"/></td>
        <td>   <h:outputText value="#{collect.s_name}" />
       </td></tr>

</table>
  

Это скрипт

 <script>
// <![CDATA[    
function generate() {


    var doc = new jsPDF('p', 'px');

    var res = doc.autoTableHtmlToJson(document.getElementById("example2"));
     var res1 = doc.autoTableHtmlToJson(document.getElementById("example3"));

        for (var j = 0; j < 6; j  ) {  
        doc.autoTable(res.columns, res.data ,{
          tableWidth: '800',
          headerStyles: {rowHeight: 12, fontSize: 9,valign: 'middle' },
          bodyStyles: {rowHeight: 10, fontSize: 8, valign: 'middle'},
          startY:20,
         theme: 'grid',overflow: 'linebreak'
        });

        }
    doc.save("fee-report_#{collect.dept_name}.pdf");
};
  

Комментарии:

1. Формулировка вашего заголовка в виде круглого, четкого вопроса может очень помочь получить полезные ответы.

Ответ №1:

 function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');

pdf.cellInitialize();
pdf.setFontSize(10);
$.each( $('#customers tr'), function (i, row){
    $.each( $(row).find("td, th"), function(j, cell){
        var txt = $(cell).text().trim() || " ";
        var width = (j==4) ? 40 : 70; //make 4th column smaller
        pdf.cell(10, 50, width, 30, txt, i);
    });
});

pdf.save('sample-file.pdf');
}
  

Также проверьте здесь скрипку