How to print or convert to PDF from datatables column images

How to print or convert to PDF from datatables column images

amitmyeamitmye Posts: 2Questions: 1Answers: 0

I want to export a datatables in format like

-------------------------------------------------
sr | id  | name |  phone     |  photo
-------------------------------------------------
1  | s12 | John | 9874563210 | Image 
-------------------------------------------------

HTML code:

<table id="example23" class="display nowrap" cellspacing="0" width="100%">
<thead>
 <tr>
  <th>Sr. No</th>
  <th>id</th>
  <th>Name</th>
  <th>Phone Number</th>
  <th>Photo</th>
 </tr>
</thead>
<tbody>
<?php 
$sr=0;
$sql = "SELECT * FROM `user` ORDER BY `name ASC ";
$result = mysqli_query ($connect, $SQL);
if (mysqli_num_rows ($result) > 0) {
while ($fieldinfo=mysqli_fetch_object ($result)
{
 $sid=$fieldinfo->sid;
 $name=$fieldinfo->name;
 $phone=$fieldinfo->phone;
 $photo = $fieldinfo->photo;
 $sr=$sr+1;
?>
 <tr>
  <td><?PHP echo $sr?></td>
  <td><?PHP echo $sid?></td>
  <td><?PHP echo $name?></td>
  <td><?PHP echo $phone?></td>
  <td><img src="upload/photo/<?PHP echo $photo;?>" onError="this.onerror=null;this.src='images/placeholder.png';" style="height: 80px;"></td>
 </tr>
<?PHP }?>
</tbody>
</table>

If photo is not available then onError="this.onerror=null; will display placeholder.

My JavaScript code is:

    <script>
        $('#example23').DataTable({
            dom: 'Bfrtip',
            buttons: [
                {
                  extend: 'copy',
                  title: 'Result for copy'
                },  
                {
                  extend: 'excel',
                  title: 'Result for excel'
                }, 
                {
                  extend: 'pdf',
                  title: 'Result for pdf'
                }, 
                {
                  extend: 'print',
                  title: 'Result for Print'
                }
            ],
            "pageLength": 50,
            "oLanguage": {
              "sSearch": "Quick Search / Sorting: "
            }
        });

        </script>

When I export my table, the photo column remains empty. But images are displaying in datatables. I want to display all images in my exported file (PDF).

Answers

  • colincolin Posts: 15,174Questions: 1Answers: 2,589

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • amitmyeamitmye Posts: 2Questions: 1Answers: 0

    Hi, This is my fiddle https://jsfiddle.net/pratapamit/hztfdrs1/13/. Please help. Thanks

  • colincolin Posts: 15,174Questions: 1Answers: 2,589

    Thanks for the fiddle. The problem is that the image isn't in the table, jsut a HTML reference, so you would need to get that image. There are a few threads on this which should help, like this and this.

    Colin

This discussion has been closed.