可能的选项来创建pdf文件使用html元素来生成发票在php和codeigniter

xvw2m8pv  于 2022-12-07  发布在  PHP
关注(0)|答案(2)|浏览(95)

大家好,这可能是最常见的问题问stackoverflow,我有一个发票模板在html像这样的demo page。我想生成一个发票的pdf文件,当用户点击按钮。
有人能给我建议做这个任务的最佳选择吗?我试过jsPDF和html2canvas,但是它没有像预期的那样工作。所以如果有任何更好的方法来做这个,请让我知道。
谢谢您的支持。

xbp102n0

xbp102n01#

如果你只是想像浏览器一样呈现页面,你可以尝试Puppeteer,一种控制Chrome的无头浏览器示例的方法,或者任何基于WKHTMLPDF的解决方案(比如Snappy)。

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});

  await browser.close();
})();

当然,如果页面是非公开的,它会变得更狡猾,但是你可以通过一些技巧来解决这个问题。
如果你真正想要的只是打印页面,你可以尝试media query for print。这通常是打印页面的最好方法。
不过,如果你真的想打印一张漂亮的发票,你应该在服务器端打印,而不是在客户端。试图生成一张看起来像html的发票似乎只是一个借口,以避免以“正确”的方式创建一个pdf的手工工作;)

kgsdhlau

kgsdhlau2#

我建议使用tcpdf,它对我来说效果很好,下面是我使用它的一个例子:

$query = "SELECT * FROM login WHERE type_login='customer'";
    $result=mysqli_query($connect, $query);
    while($row = mysqli_fetch_array($result))
    {
        $output .='
            <tr>
                <td>'.$row["id_login"].'</td>
                <td>'.$row["name_login"].'</td>
                <td>'.$row["user_email"].'</td>
            </tr>
        ';
    }

    return $output;
}

if(isset($_POST["create_pdf"]))
{
    require_once("tcpdf/tcpdf.php");
    $obj_pdf = new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true,"UTF-8",false);
    $obj_pdf->SetCreator(PDF_CREATOR);
    $obj_pdf->SetTitle("Customer List");
    $obj_pdf->SetHeaderData("","", PDF_HEADER_TITLE, PDF_HEADER_STRING);
    $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN,"",PDF_FONT_SIZE_MAIN));
    $obj_pdf->SetFooterFont(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.='
        <h3 align="center"> Customer List </h3>
        <table border="1" cellspacing="0" cellpadding="5">
            <tr>
                <th width=5%>customer ID</th>
                <th width=10%>Customer Full name</th>
                <th width=15%>Customer Email</th>
            </tr>
    ';

    $content .= fetch_data();
    $content .= '</table>';
    $obj_pdf->writeHTML($content);
    $obj_pdf->Output("sample.pdf","I");


}
?>

<html>
    <head>
        <title>Customer List</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    </head>
    <body>
        <br><br />
        <div class="container" style="width:700px;">
            <h3 align="center"> Customer List </h3>
            <br />
            <div class="table-responsive">
                <table class="table table-bordered" border="1" cellpadding="4">
                    <tr>
                        <td width="5%"><strong>customer ID</strong></td>
                        <td width="10%"><strong>Customer Full name</strong></td>
                        <td width="15%"><strong>Customer Email</strong></td>
                    </tr>
                    <?php
                    echo fetch_data();
                    ?>
                </table>

如果不想使用tcpdf,请使用以下选项:

if (isset($_POST['txtNameSearch'])){
    $search = $_POST['txtNameSearch'];
$connection = mysqli_connect('localhost', 'root', '', 'bookstore');
    $query = "SELECT * FROM tblproduct right join order_details on tblproduct.prod_id=order_details.prod_id WHERE prod_no = $search ";
    $result=mysqli_query($connection, $query);
    while($row = mysqli_fetch_array($result))
    {
        $output .='
            <tr>
                <td>'.$row["prod_id"].'</td>
                <td>'.$row["prod_name"].'</td>
                <td>'.$row["order_id"].'</td>
                <td>'.$row["quantity"].'</td>
            </tr>
        ';
    }

    return $output;
}
}
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>HTML to PDF</title>
</head>
<body>
            <form method="POST" action="index.php">
            <input type="text" name="txtNameSearch" />
            <input class="src_btn" type="submit" name="btnSearch" value="Search" />
            </form>
    <!-- 
    content of this area will be the content of your PDF file 
    -->
    <div id="HTMLtoPDF">

                <table class="table table-bordered" border="1" cellpadding="4">
                <tr>
                        <td width="25%"><strong>prod_id</strong></td>
                        <td width="25%"><strong>prod_name</strong></td>
                        <td width="25%"><strong>order_id</strong></td>
                        <td width="25%"><strong>quantity</strong></td>
</tr>

    <?php       
    echo fetch_data();

    ?>  

    </div>

    <!-- here we call the function that makes PDF -->
    <a href="#" onclick="HTMLtoPDF()">Download PDF</a>
            <a href="/DEVProject/admin/admin_addnew_user.php"> Back to Admin Panel </a>

如果你想我可以准备一个下载链接的完全工作的代码以及

相关问题