英文:
How to generate a pdf invoice from mySQL table using FPDF for a specfic id
问题
I understand your request. Here's the translated code portion you provided:
<?php
require('fpdf.php');
// A4 width: 219mm
// Default margin: 10mm each side
// Writable horizontal: 219-(10*2)=189mm
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
// Set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',14);
$pdf->Cell(130 ,5,'Lexipress.CO',0,0);
$pdf->Cell(59 ,5,'INVOICE',0,1);//end of line
// Set font to arial, regular, 12pt
$pdf->SetFont('Arial','',12);
$pdf->Cell(130 ,5,'[Street Address]',0,0);
$pdf->Cell(59 ,5,'',0,1);//end of line
$pdf->Cell(130 ,5,'[City, Country, ZIP]',0,0);
$pdf->Cell(25 ,5,'Date',0,0);
$pdf->Cell(34 ,5,'[dd/mm/yyyy]',0,1);//end of line
$pdf->Cell(130 ,5,'Phone [+12345678]',0,0);
$pdf->Cell(25 ,5,'Invoice #',0,0);
$pdf->Cell(34 ,5,'[1234567]',0,1);//end of line
$pdf->Cell(130 ,5,'Fax [+12345678]',0,0);
$pdf->Cell(25 ,5,'Customer ID',0,0);
$pdf->Cell(34 ,5,'[1234567]',0,1);//end of line
// Make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
// Billing address
$pdf->Cell(100 ,5,'Bill to',0,1);//end of line
// Add dummy cell at the beginning of each line for indentation
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Name]',0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Company Name]',0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Address]',0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Phone]',0,1);
// Make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
// Invoice contents
$pdf->SetFont('Arial','B',12);
$pdf->Cell(130 ,5,'Description',1,0);
$pdf->Cell(25 ,5,'Taxable',1,0);
$pdf->Cell(34 ,5,'Amount',1,1);//end of line
$pdf->SetFont('Arial','',12);
// Numbers are right-aligned, so we give 'R' after the new line parameter
$pdf->Cell(130 ,5,'UltraCool Fridge',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'3,250',1,1,'R');//end of line
$pdf->Cell(130 ,5,'Supaclean Dishwasher',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'1,200',1,1,'R');//end of line
$pdf->Cell(130 ,5,'Something Else',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'1,000',1,1,'R');//end of line
// Summary
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Subtotal',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Taxable',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'0',1,1,'R');//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Tax Rate',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'10%',1,1,'R');//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Total Due',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line
$pdf->Output();
?>
I hope this helps you format your invoices as you described. If you have further questions or need assistance with specific parts of your code, feel free to ask.
英文:
I am facing a problem of getting the the data for a specific id. I want the user to get the link to download the data from a button. This is mysqldatatable name (add_courier):
id | order_inv | name | date | quantity of the items | price | payment mode.
Now, I am able to get the list of all the ids but I am not able to get the invoice for a specific id. Here is my code:
<?php
require('fpdf.php');
$con=mysqli_connect('localhost','root','');
mysqli_select_db($con,'lexipressdb');
class PDF extends FPDF {
function Header(){
$this->SetFont('Arial','B',15);
//dummy cell to put logo
//$this->Cell(12,0,'',0,0);
//is equivalent to:
$this->Cell(12);
//put logo
$this->Image('gift.jpg',10,10,10);
$this->Cell(100,10,'Invoice',0,1);
//dummy cell to give line spacing
//$this->Cell(0,5,'',0,1);
//is equivalent to:
$this->Ln(5);
$this->SetFont('Arial','B',11);
$this->SetFillColor(180,180,255);
$this->SetDrawColor(180,180,255);
$this->Cell(40,5,'Order Invoice',1,0,'',true);
$this->Cell(25,5,'Quantity',1,0,'',true);
$this->Cell(65,5,'Weight',1,0,'',true);
$this->Cell(60,5,'Cost',1,1,'',true);
}
function Footer(){
//add table's bottom line
$this->Cell(190,0,'','T',1,'',true);
//Go to 1.5 cm from bottom
$this->SetY(-15);
$this->SetFont('Arial','',8);
//width = 0 means the cell is extended up to the right margin
$this->Cell(0,10,'Page '.$this->PageNo()." / {pages}",0,0,'C');
}
}
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
$pdf = new PDF('P','mm','A4'); //use new class
//define new alias for total page numbers
$pdf->AliasNbPages('{pages}');
$pdf->SetAutoPageBreak(true,15);
$pdf->AddPage();
$pdf->SetFont('Arial','',9);
$pdf->SetDrawColor(180,180,255);
$query=mysqli_query($con,"select * from add_courier");
while($data=mysqli_fetch_array($query)){
$pdf->Cell(40,5,$data['order_inv'],'LR',0);
$pdf->Cell(25,5,$data['r_qnty'],'LR',0);
$pdf->Cell(60,5,$data['r_weight'],'LR',0);
$pdf->Cell(60,5,$data['r_costtotal'],'LR',1);
}
$pdf->Output();
?>
I want to have my format to be like this for each id so that the user can download it from the link:'
<?php
require('fpdf.php');
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
//set font to arial, bold, 14pt
$pdf->SetFont('Arial','B',14);
//Cell(width , height , text , border , end line , [align] )
$pdf->Cell(130 ,5,'Lexipress.CO',0,0);
$pdf->Cell(59 ,5,'INVOICE',0,1);//end of line
//set font to arial, regular, 12pt
$pdf->SetFont('Arial','',12);
$pdf->Cell(130 ,5,'[Street Address]',0,0);
$pdf->Cell(59 ,5,'',0,1);//end of line
$pdf->Cell(130 ,5,'[City, Country, ZIP]',0,0);
$pdf->Cell(25 ,5,'Date',0,0);
$pdf->Cell(34 ,5,'[dd/mm/yyyy]',0,1);//end of line
$pdf->Cell(130 ,5,'Phone [+12345678]',0,0);
$pdf->Cell(25 ,5,'Invoice #',0,0);
$pdf->Cell(34 ,5,'[1234567]',0,1);//end of line
$pdf->Cell(130 ,5,'Fax [+12345678]',0,0);
$pdf->Cell(25 ,5,'Customer ID',0,0);
$pdf->Cell(34 ,5,'[1234567]',0,1);//end of line
//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
//billing address
$pdf->Cell(100 ,5,'Bill to',0,1);//end of line
//add dummy cell at beginning of each line for indentation
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Name]',0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Company Name]',0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Address]',0,1);
$pdf->Cell(10 ,5,'',0,0);
$pdf->Cell(90 ,5,'[Phone]',0,1);
//make a dummy empty cell as a vertical spacer
$pdf->Cell(189 ,10,'',0,1);//end of line
//invoice contents
$pdf->SetFont('Arial','B',12);
$pdf->Cell(130 ,5,'Description',1,0);
$pdf->Cell(25 ,5,'Taxable',1,0);
$pdf->Cell(34 ,5,'Amount',1,1);//end of line
$pdf->SetFont('Arial','',12);
//Numbers are right-aligned so we give 'R' after new line parameter
$pdf->Cell(130 ,5,'UltraCool Fridge',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'3,250',1,1,'R');//end of line
$pdf->Cell(130 ,5,'Supaclean Diswasher',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'1,200',1,1,'R');//end of line
$pdf->Cell(130 ,5,'Something Else',1,0);
$pdf->Cell(25 ,5,'-',1,0);
$pdf->Cell(34 ,5,'1,000',1,1,'R');//end of line
//summary
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Subtotal',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Taxable',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'0',1,1,'R');//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Tax Rate',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'10%',1,1,'R');//end of line
$pdf->Cell(130 ,5,'',0,0);
$pdf->Cell(25 ,5,'Total Due',0,0);
$pdf->Cell(4 ,5,'$',1,0);
$pdf->Cell(30 ,5,'4,450',1,1,'R');//end of line
$pdf->Output();
?>
I don't know how to go about it. Can someone please help? I've also checked the other questions for the same issues but i'm not getting the resloution.'
答案1
得分: 1
Here is the translated code:
$sql = mysqli_query($con, "select * from add_courier");
foreach loop starts here
<button><a href="printme.php&id=<?php echo $row['id']; ?>">Print</a></button>
foreach loop ends here
In your FPDF page, get the ID variable
// Example
require('fpdf.php');
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'lexipressdb');
$id = $_GET['id'];
Alter your query with this on the FPDF page
$query = mysqli_query($con, "select * from add_courier where id='$id'");
英文:
$sql=mysqli_query($con,"select * from add_courier");
foreach loop starts here
<button > <a href="printme.php&id=<?php echo $row['id']?>">Print</a>
</button>
for each loop ends here
in your FPDF page get the ID variable
//example//
require('fpdf.php');
$con=mysqli_connect('localhost','root','');
mysqli_select_db($con,'lexipressdb');
$id=$_GET['id'];
alter your query with this on FPDF page
$query=mysqli_query($con,"select * from add_courier where id='$id'");
答案2
得分: 1
以下是您提供的代码的翻译:
<table>
<thead>
<th style="font-weight:bold;">追踪</th>
<th style="font-weight:bold;">打印发票</th>
</thead>
<tbody>
<?php
$ret = mysqli_query($con, "select * from add_courier");
$cnt = 1;
while ($row = mysqli_fetch_array($ret)) {
?>
<tr>
<td style="font-weight:bold;"><?php echo $row['trackingid']; ?></td>
<td>
<a href="invoice/generate_pdf.php?id=<?php echo $row['trackingid']; ?>">生成</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
require('fpdf.php');
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'lexipressdb');
$id = $_GET['id'];
// 请首先回显并检查id的值
echo $id;
exit;
// 如果在点击生成链接时获取到id值,则注释掉上面的行并享受
class PDF extends FPDF
{
function Header()
{
$this->SetFont('Arial', 'B', 15);
// 用于放置标志的虚拟单元格
// $this->Cell(12, 0, '', 0, 0);
// 等同于:
$this->Cell(12);
// 放置标志
$this->Image('gift.jpg', 10, 10, 10);
$this->Cell(100, 10, '发票', 0, 1);
// 用于给出行间距的虚拟单元格
// $this->Cell(0, 5, '', 0, 1);
// 等同于:
$this->Ln(5);
$this->SetFont('Arial', 'B', 11);
$this->SetFillColor(180, 180, 255);
$this->SetDrawColor(180, 180, 255);
$this->Cell(40, 5, '订单发票', 1, 0, '', true);
$this->Cell(25, 5, '数量', 1, 0, '', true);
$this->Cell(65, 5, '重量', 1, 0, '', true);
$this->Cell(60, 5, '成本', 1, 1, '', true);
}
function Footer()
{
// 添加表的底边线
$this->Cell(190, 0, '', 'T', 1, '', true);
// 转到底部距离底部1.5厘米的位置
$this->SetY(-15);
$this->SetFont('Arial', '', 8);
// 宽度=0表示该单元格向右边距扩展
$this->Cell(0, 10, '页 ' . $this->PageNo() . " / {pages}", 0, 0, 'C');
}
}
// A4宽度:219mm
// 默认边距:每边10mm
// 可写水平:219-(10*2)=189mm
$pdf = new PDF('P', 'mm', 'A4'); // 使用新类
// 定义总页数的新别名
$pdf->AliasNbPages('{pages}');
$pdf->SetAutoPageBreak(true, 15);
$pdf->AddPage();
$pdf->SetFont('Arial', '', 9);
$pdf->SetDrawColor(180, 180, 255);
$query = mysqli_query($con, "select * from add_courier where id='$id'");
while ($data = mysqli_fetch_array($query)) {
$pdf->Cell(40, 5, $data['order_inv'], 'LR', 0);
$pdf->Cell(25, 5, $data['r_qnty'], 'LR', 0);
$pdf->Cell(60, 5, $data['r_weight'], 'LR', 0);
$pdf->Cell(60, 5, $data['r_costtotal'], 'LR', 1);
}
$pdf->Output();
?>
请注意,这些翻译是根据您提供的代码内容生成的,不包括代码中的注释。如果需要更多帮助或有其他问题,请随时提出。
英文:
<table>
<thead>
<th style="font-weight:bold;">Tracking</th>
<th style="font-weight:bold;">Print Invoice</th>
</thead>
<tbody>
<?php
$ret=mysqli_query($con,"select *from add_courier");
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
?>
<tr>
<td style="font-weight:bold;"><?php echo $row['trackingid'];?>
</td>
<td>
<a href="invoice/generate_pdf.php?id=<?php echo $row['trackingid'];
//this is your tracking primary key column name
?>">Generate</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
--------------------------------------------------------------------------------------------------------
generate_pdf.php code
<?php
require('fpdf.php');
$con=mysqli_connect('localhost','root','');
mysqli_select_db($con,'lexipressdb');
$id=$_GET['id'];
// please first echo and check the id value
echo $id; exit;
// if you get the id on clicking generate link then comment the above line and enjoy
class PDF extends FPDF {
function Header(){
$this->SetFont('Arial','B',15);
//dummy cell to put logo
//$this->Cell(12,0,'',0,0);
//is equivalent to:
$this->Cell(12);
//put logo
$this->Image('gift.jpg',10,10,10);
$this->Cell(100,10,'Invoice',0,1);
//dummy cell to give line spacing
//$this->Cell(0,5,'',0,1);
//is equivalent to:
$this->Ln(5);
$this->SetFont('Arial','B',11);
$this->SetFillColor(180,180,255);
$this->SetDrawColor(180,180,255);
$this->Cell(40,5,'Order Invoice',1,0,'',true);
$this->Cell(25,5,'Quantity',1,0,'',true);
$this->Cell(65,5,'Weight',1,0,'',true);
$this->Cell(60,5,'Cost',1,1,'',true);
}
function Footer(){
//add table's bottom line
$this->Cell(190,0,'','T',1,'',true);
//Go to 1.5 cm from bottom
$this->SetY(-15);
$this->SetFont('Arial','',8);
//width = 0 means the cell is extended up to the right margin
$this->Cell(0,10,'Page '.$this->PageNo()." / {pages}",0,0,'C');
}
}
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
$pdf = new PDF('P','mm','A4'); //use new class
//define new alias for total page numbers
$pdf->AliasNbPages('{pages}');
$pdf->SetAutoPageBreak(true,15);
$pdf->AddPage();
$pdf->SetFont('Arial','',9);
$pdf->SetDrawColor(180,180,255);
$query=mysqli_query($con,"select * from add_courier where id='$id'");
while($data=mysqli_fetch_array($query)){
$pdf->Cell(40,5,$data['order_inv'],'LR',0);
$pdf->Cell(25,5,$data['r_qnty'],'LR',0);
$pdf->Cell(60,5,$data['r_weight'],'LR',0);
$pdf->Cell(60,5,$data['r_costtotal'],'LR',1);
}
$pdf->Output();
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论