Jump to content

Multiple pdf


Aswin
 Share

Go to solution Solved by Barand,

Recommended Posts

13 minutes ago, Aswin said:

Guys will you help me

Yes

 

13 minutes ago, Aswin said:

by 

Creating multiple pdf using single command 

using tcpdf in php

No.

If you want to pay someone to do it for you, post in the "job Offerings" forum. Otherwise, post the code you have already and tell us where you are having probems so we can help you with your coding.

Edited by Barand
  • Thanks 1
Link to comment
Share on other sites

<?php
function fetch_data(){

    $conn=mysqli_connect("localhost","root","","pdf");
    $output='';
    $sql="SELECT * FROM tables";
    $result=mysqli_query($conn,$sql);
    $sno=0;
    while($row=mysqli_fetch_array($result)){
        $output.=
        '<tr>
        <td>'  .$row["product_id"].'</td>
        <td>'  .$row["product_name"].'</td>
        </tr>';

    }
    return $output;
}
// if(isset($_POST["createpdf"])){
    require_once('tcpdf_min/tcpdf.php');
    $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false);
    $obj_pdf->SetCreator(PDF_CREATOR);
    $obj_pdf-> SetTitle("Product PDF");
    $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING);
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN));
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA));
    $obj_pdf->SetDefaultMonospacedFont('helvetica');
    $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT);
    $obj_pdf->SetPrintHeader(false);
    $obj_pdf->SetPrintFooter(false);
    $obj_pdf->SetAutoPageBreak(TRUE,10);
    $obj_pdf -> SetFont('helvetica', '',12);
    $obj_pdf->AddPage();
    $content='';
    $content .='<h4 align="center">Generate Pdf</h4><br>
    <table border="1" cellspacing="0" cellpadding="3">
    <tr>
    <th>Id</th>
    <th>Name</th>
    </tr>';
    $content .= fetch_data();
    $content .= '</table>';
    $obj_pdf->writeHTML($content);
    $obj_pdf->Output('Sample Pdf','I');
//} 
?>
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
    <title>Pdf Generation</title>
    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <br>
    <!-- <h4 align="center">Pdf Output</h4> -->
    <table>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <?php
        echo fetch_data();
        ?>
    </table>
    <form method="post">
        <input type="submit" name="createpdf" value="Generate Pdf">
    </form>
</body>
</html>

 

This is the code which am using here i have to create pdf for each product will u help me

Link to comment
Share on other sites

  • Solution

It doesn't let you create a PDF once output has already been sent, therefore creating multiple pdfs in a single script doesn't appear to be an option.

As an alternative I would suggest

  • give users the option to select each product and create their pdfs separately, or
  • create one pdf but put each product on a separate page

Also, I prefer to put my pdf code in a separate file and create the pdf via a "create PDF" link and give the user the option of viewing or downloading the PDF.

This version gives both the above options

                                 image.png.f81a98b018beb577e1ca7222c995c365.png

aswin_1.php

<?php 
                //                                         //
                //  CREATE YOUR OWN MYSQL CONNECTION HERE  //
                //                                         //

function fetch_data($conn){

    $output='';
    $sql="SELECT * FROM product";
    $result=mysqli_query($conn,$sql);
    $sno=0;
    while($row=mysqli_fetch_array($result)){
        $output.=
        '<tr>
        <td><a href="aswin_2.php?product=' . $row["product_id"] . '">' . $row["product_id"] . '</a></td>
        <td>'  .$row["product_name"].'</td>
        </tr>';

    }
    return $output;
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Pdf Generation</title>
    <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <br>
    <table style='width:400px; margin: 50px auto;'>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <?php
        echo fetch_data($conn);
        ?>
    </table>
    <div style='width: 400px; margin: 20px auto;'>
    <a href="aswin_2.php?product=all" download>All products</a>
</body>
</html> 

 

aswin_2.php (creates the pdf)

<?php 
                //                                         //
                //  CREATE YOUR OWN MYSQL CONNECTION HERE  //
                //                                         //
                
require_once '../tcpdf/tcpdf.php'; 

if (!isset($_GET['product'])) {
    exit;
}

if ($_GET['product']=='all') {
    $stmt = $conn->query("SELECT product_id
                                , product_name
                          FROM product
                          ORDER BY product_id    
                          ");
                      
    $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false);
    $obj_pdf->SetCreator(PDF_CREATOR);
    $obj_pdf-> SetTitle("Product PDF");
    $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING);
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN));
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA));
    $obj_pdf->SetDefaultMonospacedFont('helvetica');
    $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT);
    $obj_pdf->SetPrintHeader(false);
    $obj_pdf->SetPrintFooter(false);
    $obj_pdf->SetAutoPageBreak(TRUE,10);
    $obj_pdf -> SetFont('helvetica', '',12);
    
    foreach ($stmt as $row) {
        $obj_pdf->AddPage();
        $content='';
        $content .='<h4 align="center">Generate Pdf</h4><br>
        <table border="1" cellspacing="0" cellpadding="3">
        <tr>
        <th>Id</th>
        <th>Name</th>
        </tr>';
        $content .= "<tr>
                        <td>{$row['product_id']}</td>
                        <td>{$row['product_name']}</td>
                    </tr>
                    ";
        $content .= '</table>';
        $obj_pdf->writeHTML($content);
        
    }
    $obj_pdf->Output('All Products','D');
}
else {
    $stmt = $conn->prepare("SELECT product_id
                                 , product_name
                            FROM product
                            WHERE product_id = ?     
                            ");
    $stmt->bind_param('s', $_GET['product']);
    $stmt->execute();
    $stmt->bind_result($product_id, $product_name);
    $stmt->fetch();

    $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false);
    $obj_pdf->SetCreator(PDF_CREATOR);
    $obj_pdf-> SetTitle("Product PDF");
    $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING);
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN));
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA));
    $obj_pdf->SetDefaultMonospacedFont('helvetica');
    $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
    $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT);
    $obj_pdf->SetPrintHeader(false);
    $obj_pdf->SetPrintFooter(false);
    $obj_pdf->SetAutoPageBreak(TRUE,10);
    $obj_pdf -> SetFont('helvetica', '',12);
    $obj_pdf->AddPage();
    $content='';
    $content .='<h4 align="center">Generate Pdf</h4><br>
    <table border="1" cellspacing="0" cellpadding="3">
    <tr>
    <th>Id</th>
    <th>Name</th>
    </tr>';
    $content .= "<tr>
                    <td>$product_id</td>
                    <td>$product_name</td>
                </tr>
                ";
    $content .= '</table>';
    $obj_pdf->writeHTML($content);
    $obj_pdf->Output("Product $product_id",'D');
}
?>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.