What's new

Help Question for bot

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,927
Reaction
472
Points
658
Age
24
Generate a jquey and php code that can generate pdf from json response using datatablepdf and the response is from json
 
Sure! Here's an example of jQuery and PHP code that can generate a PDF from JSON response using DataTables and DataTables PDF plugin:

jQuery Code:
JavaScript:
$(document).ready(function() {
    $('#dataTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'pdf'
        ],
        ajax: {
            url: 'data.php',
            type: 'POST',
            dataType: 'json',
            dataSrc: 'data'
        },
        columns: [
            { data: 'name' },
            { data: 'age' },
            { data: 'country' }
        ]
    });
});

PHP Code (data.php):
PHP:
<?php
// Fetch data from JSON response
$jsonResponse = '{
    "data": [
        {
            "name": "John Doe",
            "age": 25,
            "country": "USA"
        },
        {
            "name": "Jane Smith",
            "age": 30,
            "country": "Canada"
        }
    ]
}';

$data = json_decode($jsonResponse, true)['data'];

// Create PDF using DataTables PDF plugin
require('path/to/datatables-pdf.php');

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);

foreach ($data as $row) {
    $pdf->Cell(40, 10, $row['name'], 1);
    $pdf->Cell(30, 10, $row['age'], 1);
    $pdf->Cell(50, 10, $row['country'], 1);
    $pdf->Ln();
}

$pdf->Output();

Make sure to replace 'path/to/datatables-pdf.php' with the actual path to the DataTables PDF plugin file.

This code assumes that you have both DataTables and DataTables PDF plugin installed and set up properly. You can customize the code further based on your specific requirements.
 
Sure! Here's an example of jQuery and PHP code that can generate a PDF from JSON response using DataTables and DataTables PDF plugin:

jQuery Code:
JavaScript:
$(document).ready(function() {
    $('#dataTable').DataTable({
        dom: 'Bfrtip',
        buttons: [
            'pdf'
        ],
        ajax: {
            url: 'data.php',
            type: 'POST',
            dataType: 'json',
            dataSrc: 'data'
        },
        columns: [
            { data: 'name' },
            { data: 'age' },
            { data: 'country' }
        ]
    });
});

PHP Code (data.php):
PHP:
<?php
// Fetch data from JSON response
$jsonResponse = '{
    "data": [
        {
            "name": "John Doe",
            "age": 25,
            "country": "USA"
        },
        {
            "name": "Jane Smith",
            "age": 30,
            "country": "Canada"
        }
    ]
}';

$data = json_decode($jsonResponse, true)['data'];

// Create PDF using DataTables PDF plugin
require('path/to/datatables-pdf.php');

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);

foreach ($data as $row) {
    $pdf->Cell(40, 10, $row['name'], 1);
    $pdf->Cell(30, 10, $row['age'], 1);
    $pdf->Cell(50, 10, $row['country'], 1);
    $pdf->Ln();
}

$pdf->Output();

Make sure to replace 'path/to/datatables-pdf.php' with the actual path to the DataTables PDF plugin file.

This code assumes that you have both DataTables and DataTables PDF plugin installed and set up properly. You can customize the code further based on your specific requirements.
Can you provide the full source code including the cdn links
 
Can you provide the full source code including the cdn links
Sure! Here's the full source code including the CDN links for DataTables and DataTables PDF plugin:

HTML Code:
HTML:
<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF from JSON response using DataTables</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css">
</head>
<body>
    <table id="dataTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Country</th>
            </tr>
        </thead>
    </table>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.print.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#dataTable').DataTable({
                dom: 'Bfrtip',
                buttons: [
                    'pdf'
                ],
                ajax: {
                    url: 'data.php',
                    type: 'POST',
                    dataType: 'json',
                    dataSrc: 'data'
                },
                columns: [
                    { data: 'name' },
                    { data: 'age' },
                    { data: 'country' }
                ]
            });
        });
    </script>
</body>
</html>

PHP Code (data.php):
PHP:
<?php
// Fetch data from JSON response
$jsonResponse = '{
    "data": [
        {
            "name": "John Doe",
            "age": 25,
            "country": "USA"
        },
        {
            "name": "Jane Smith",
            "age": 30,
            "country": "Canada"
        }
    ]
}';

$data = json_decode($jsonResponse, true)['data'];

// Create PDF using DataTables PDF plugin
require('path/to/datatables-pdf.php');

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);

foreach ($data as $row) {
    $pdf->Cell(40, 10, $row['name'], 1);
    $pdf->Cell(30, 10, $row['age'], 1);
    $pdf->Cell(50, 10, $row['country'], 1);
    $pdf->Ln();
}

$pdf->Output();

Make sure to replace [path/to/datatables-pdf.php] with the actual path to the DataTables PDF plugin file. The CDN links provided in the HTML code will load the necessary CSS and JavaScript files for DataTables and DataTables PDF plugin.
 
Sure! Here's the full source code including the CDN links for DataTables and DataTables PDF plugin:

HTML Code:
HTML:
<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF from JSON response using DataTables</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css">
</head>
<body>
    <table id="dataTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Country</th>
            </tr>
        </thead>
    </table>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.print.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#dataTable').DataTable({
                dom: 'Bfrtip',
                buttons: [
                    'pdf'
                ],
                ajax: {
                    url: 'data.php',
                    type: 'POST',
                    dataType: 'json',
                    dataSrc: 'data'
                },
                columns: [
                    { data: 'name' },
                    { data: 'age' },
                    { data: 'country' }
                ]
            });
        });
    </script>
</body>
</html>

PHP Code (data.php):
PHP:
<?php
// Fetch data from JSON response
$jsonResponse = '{
    "data": [
        {
            "name": "John Doe",
            "age": 25,
            "country": "USA"
        },
        {
            "name": "Jane Smith",
            "age": 30,
            "country": "Canada"
        }
    ]
}';

$data = json_decode($jsonResponse, true)['data'];

// Create PDF using DataTables PDF plugin
require('path/to/datatables-pdf.php');

$pdf = new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);

foreach ($data as $row) {
    $pdf->Cell(40, 10, $row['name'], 1);
    $pdf->Cell(30, 10, $row['age'], 1);
    $pdf->Cell(50, 10, $row['country'], 1);
    $pdf->Ln();
}

$pdf->Output();

Make sure to replace [path/to/datatables-pdf.php] with the actual path to the DataTables PDF plugin file. The CDN links provided in the HTML code will load the necessary CSS and JavaScript files for DataTables and DataTables PDF plugin.
can you provide the datatables-pdf.php source code
 
can you provide the datatables-pdf.php source code
Certainly! However, DataTables does not provide an official DataTables PDF plugin. There are multiple third-party plugins available for generating PDFs from DataTables. One popular option is the "jQuery DataTables Export to PDF" plugin. Here's an example of how you can use this plugin to generate PDFs from JSON response:

HTML Code:
HTML:
<!DOCTYPE html>
<html>
<head>
    <title>Generate PDF from JSON response using DataTables</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.7.1/css/buttons.dataTables.min.css">
</head>
<body>
    <table id="dataTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>Age</th>
                <th>Country</th>
            </tr>
        </thead>
    </table>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.print.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.7.1/js/buttons.pdf.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#dataTable').DataTable({
                dom: 'Bfrtip',
                buttons: [
                    'pdf'
                ],
                ajax: {
                    url: 'data.php',
                    type: 'POST',
                    dataType: 'json',
                    dataSrc: 'data'
                },
                columns: [
                    { data: 'name' },
                    { data: 'age' },
                    { data: 'country' }
                ]
            });
        });
    </script>
</body>
</html>

PHP Code (data.php):
PHP:
<?php
// Fetch data from JSON response
$jsonResponse = '{
    "data": [
        {
            "name": "John Doe",
            "age": 25,
            "country": "USA"
        },
        {
            "name": "Jane Smith",
            "age": 30,
            "country": "Canada"
        }
    ]
}';

$data = json_decode($jsonResponse, true)['data'];

// Prepare data for DataTables format
$result = array(
    'data' => $data
);

// Convert the data to JSON format
echo json_encode($result);
?>

Make sure to include the necessary CDN links for DataTables, DataTables Buttons, and DataTables PDF plugin in the HTML code.
 

Similar threads

Back
Top