jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

table cells alignment

Open funnykriss10 opened this issue 12 years ago • 11 comments

Using jspdf i was able to convert HTML table to pdf and change font-size.Is there a way that I can align few columns of the table in the pdf to right and few aligned to left.Like if a table has 5 columns, I want first and second aligned to left, 3rd one to center and 5th one to right.

funnykriss10 avatar May 29 '14 19:05 funnykriss10

CSS text-align

diegocr avatar May 29 '14 19:05 diegocr

I tried using text-align but it is not changing alignment

funnykriss10 avatar May 29 '14 19:05 funnykriss10

Have you got any sample code for the issue?

MrRio avatar May 29 '14 20:05 MrRio

Whoops! Thought that was supported by #252

http://jsfiddle.net/dGLmr/51/

diegocr avatar May 29 '14 20:05 diegocr

Below is the code I used. I was able to generate the table as pdf but looking for right alignments of the columns of table in pdf.

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title></title>
<script src="Scripts/SaveAsPDF/FileSaver.js"></script>
    <script src="Scripts/SaveAsPDF/jspdf.js"></script>
    <script src="Scripts/SaveAsPDF/jspdf.plugin.cell.js"></script>  
   <script src="Scripts/SaveAsPDF/jspdf.plugin.from_html.js"></script>
    <script src="Scripts/SaveAsPDF/deflate.js"></script>
    <script src="Scripts/SaveAsPDF/addler32cs.js"></script>
    <script src="Scripts/SaveAsPDF/Blob.js"></script>
    <script src="Scripts/SaveAsPDF/jspdf.plugin.standard_fonts_metrics.js"></script>
    <script src="Scripts/SaveAsPDF/jspdf.plugin.split_text_to_size.js"></script>
</head>
 <body>
     <div id="tblSaveAsPdf" class="table-details margin-top-small collapse" >
                    <table class="table table-bordered table-striped table-hover bootstrap-datatable datatable dataTable table-responsive">
                        <thead>
                            <tr role="row"> 
                                <th role="columnheader" class="col-lg-2">Account</th>
                                <th role="columnheader" class="col-lg-3">Description</th>
                                <th role="columnheader" class="col-lg-3">JournalEntry#</th>
                                <th role="columnheader" class="col-lg-2">Debit</th>
                                <th role="columnheader" class="col-lg-2" style="text-align: right" >Credit</th>
                            </tr>
                        </thead>
                         <tfoot>
                            <tr role="row">
                                <td ><span>TOTAL</span> </td>
                               <td></td>  
                                <td></td>                                                        
                                <td >
                                    <span data-bind="text: pp_formattedDebitTotal"></span>
                                </td>
                                <td   style="text-align: right">
                                   <span data-bind="text: pp_formattedCreditTotal"></span>
                                </td>
                            </tr>
                      </tfoot>
                        <tbody  data-bind="foreach: pp_voidCheckGLSummarys">
                            <tr >
                                <td>
                                    <span data-bind="text: pp_account"></span>
                                </td>
                                <td >
                                    <span data-bind="text: pp_accountName"></span>
                                </td>
                                <td>
                                    <span data-bind="text: pp_entry"></span>
                                </td>
                                <td>
                                    <span data-bind="text: pp_amountDebit"></span>
                                </td>
                                <td>
                                    <span data-bind="text: pp_amountCredit"></span>
                                </td>
                            </tr>
                            </table>
                            </div>
                        </tbody>  
      <div>
     <button onclick="demoFromHTML(tblSaveAsPdf)" class="button">SavePDF</button></p>     </div>
</div>

<script>
function demoFromHTML(targetId) {

    var pdf = new jsPDF('p', 'pt', 'letter')
        , source = $('#' + targetId)[0]
        , specialElementHandlers = {          
            '#bypassme': function(element, renderer){            
                return true
            }
        }

        margins = {
            top: 40,
            bottom: 60,
            left: 70,
            width: 522,       
        };

        pdf.fromHTML(
           source
           , margins.left
           , margins.top 
           , {
               'width': margins.width 
               , 'elementHandlers': specialElementHandlers
           },
           function (dispose) {

               pdf.save('Test.pdf');
           },
           margins
       )
    }
</script>

funnykriss10 avatar May 29 '14 20:05 funnykriss10

http://jsfiddle.net/dGLmr/51/ Even in this example when I table in the table has no columns aligned to right though 'text-align:right' is used.

funnykriss10 avatar May 29 '14 20:05 funnykriss10

Yeah, that fiddle was to provide proof that it isn't working since MrRio asked for an example.

diegocr avatar May 29 '14 20:05 diegocr

oh, ok thank you!

funnykriss10 avatar May 29 '14 20:05 funnykriss10

Is there any workaround for table cells alignment to work in jspdf???

AnithaN avatar Oct 20 '14 06:10 AnithaN

Do we have any update on this?

SagarNaliyapara avatar Aug 03 '18 09:08 SagarNaliyapara

We are closing this issue, because we will not support any longer fromHTML and addHTML.

Explaination: We are working on a new html2pdf plugin, which will be based on html2canvas and our context2d plugin. This should lead to more reliable results for your projects. And it will give us the time to focus on the core functionality of pdf-generation because we will not use our energy for writing/supporting/extending 2 html plugins. If you still want to use addHTML or fromHTML you can still use jsPDF 1.4.1.

Best Regards

Uzlopak avatar Sep 08 '18 14:09 Uzlopak