大家好,这可能是 stackoverflow 上最常见的问题,我有一个 html 格式的发票模板,比如这个演示页面。我想生成发票 pdf 文件,当用户单击按钮时。
谁能建议我完成这项任务的最佳选择?我已经尝试过 jsPDF 和 html2canvas,但它没有按预期工作。因此,如果有任何更好的方法可以做到这一点,请告诉我。
谢谢你。
如果您只想像浏览器一样呈现页面,您可以尝试Puppeteer,这是一种控制 Chrome 无头浏览器实例的方法。它实际上是(自述文件中的示例):
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();
})();
当然,如果该页面是非公开的,它会变得更狡猾,但你可以通过一些技巧来解决这个问题。
如果您真正想要的只是按原样打印页面,您可以尝试对 print 进行媒体查询。这通常是打印页面的最佳方式。
尽管如此,如果你真的想打印一张看起来不错的发票,你应该在服务器端而不是在客户端。尝试生成看起来像 html 的发票似乎是避免以“正确”方式创建 pdf 的手动工作的借口;)
I would suggest using tcpdf which worked out great for me, here is an example where i used it:
$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>
or alternative if you dont want to use 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>
If you want i can prepare a download link of the fully working code as well