如何使用FPDF从MySQL表中生成特定ID的PDF发票。

huangapple go评论68阅读模式
英文:

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:

&lt;?php
require(&#39;fpdf.php&#39;);
$con=mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
mysqli_select_db($con,&#39;lexipressdb&#39;);
class PDF extends FPDF {
function Header(){
$this-&gt;SetFont(&#39;Arial&#39;,&#39;B&#39;,15);
//dummy cell to put logo
//$this-&gt;Cell(12,0,&#39;&#39;,0,0);
//is equivalent to:
$this-&gt;Cell(12);
//put logo
$this-&gt;Image(&#39;gift.jpg&#39;,10,10,10);
$this-&gt;Cell(100,10,&#39;Invoice&#39;,0,1);
//dummy cell to give line spacing
//$this-&gt;Cell(0,5,&#39;&#39;,0,1);
//is equivalent to:
$this-&gt;Ln(5);
$this-&gt;SetFont(&#39;Arial&#39;,&#39;B&#39;,11);
$this-&gt;SetFillColor(180,180,255);
$this-&gt;SetDrawColor(180,180,255);
$this-&gt;Cell(40,5,&#39;Order Invoice&#39;,1,0,&#39;&#39;,true);
$this-&gt;Cell(25,5,&#39;Quantity&#39;,1,0,&#39;&#39;,true);
$this-&gt;Cell(65,5,&#39;Weight&#39;,1,0,&#39;&#39;,true);
$this-&gt;Cell(60,5,&#39;Cost&#39;,1,1,&#39;&#39;,true);
}
function Footer(){
//add table&#39;s bottom line
$this-&gt;Cell(190,0,&#39;&#39;,&#39;T&#39;,1,&#39;&#39;,true);
//Go to 1.5 cm from bottom
$this-&gt;SetY(-15);
$this-&gt;SetFont(&#39;Arial&#39;,&#39;&#39;,8);
//width = 0 means the cell is extended up to the right margin
$this-&gt;Cell(0,10,&#39;Page &#39;.$this-&gt;PageNo().&quot; / {pages}&quot;,0,0,&#39;C&#39;);
}
}
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
$pdf = new PDF(&#39;P&#39;,&#39;mm&#39;,&#39;A4&#39;); //use new class
//define new alias for total page numbers
$pdf-&gt;AliasNbPages(&#39;{pages}&#39;);
$pdf-&gt;SetAutoPageBreak(true,15);
$pdf-&gt;AddPage();
$pdf-&gt;SetFont(&#39;Arial&#39;,&#39;&#39;,9);
$pdf-&gt;SetDrawColor(180,180,255);
$query=mysqli_query($con,&quot;select * from add_courier&quot;);
while($data=mysqli_fetch_array($query)){
$pdf-&gt;Cell(40,5,$data[&#39;order_inv&#39;],&#39;LR&#39;,0);
$pdf-&gt;Cell(25,5,$data[&#39;r_qnty&#39;],&#39;LR&#39;,0);
$pdf-&gt;Cell(60,5,$data[&#39;r_weight&#39;],&#39;LR&#39;,0);
$pdf-&gt;Cell(60,5,$data[&#39;r_costtotal&#39;],&#39;LR&#39;,1);
}
$pdf-&gt;Output();
?&gt;

I want to have my format to be like this for each id so that the user can download it from the link:'

&lt;?php
require(&#39;fpdf.php&#39;);
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
$pdf = new FPDF(&#39;P&#39;,&#39;mm&#39;,&#39;A4&#39;);
$pdf-&gt;AddPage();
//set font to arial, bold, 14pt
$pdf-&gt;SetFont(&#39;Arial&#39;,&#39;B&#39;,14);
//Cell(width , height , text , border , end line , [align] )
$pdf-&gt;Cell(130	,5,&#39;Lexipress.CO&#39;,0,0);
$pdf-&gt;Cell(59	,5,&#39;INVOICE&#39;,0,1);//end of line
//set font to arial, regular, 12pt
$pdf-&gt;SetFont(&#39;Arial&#39;,&#39;&#39;,12);
$pdf-&gt;Cell(130	,5,&#39;[Street Address]&#39;,0,0);
$pdf-&gt;Cell(59	,5,&#39;&#39;,0,1);//end of line
$pdf-&gt;Cell(130	,5,&#39;[City, Country, ZIP]&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Date&#39;,0,0);
$pdf-&gt;Cell(34	,5,&#39;[dd/mm/yyyy]&#39;,0,1);//end of line
$pdf-&gt;Cell(130	,5,&#39;Phone [+12345678]&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Invoice #&#39;,0,0);
$pdf-&gt;Cell(34	,5,&#39;[1234567]&#39;,0,1);//end of line
$pdf-&gt;Cell(130	,5,&#39;Fax [+12345678]&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Customer ID&#39;,0,0);
$pdf-&gt;Cell(34	,5,&#39;[1234567]&#39;,0,1);//end of line
//make a dummy empty cell as a vertical spacer
$pdf-&gt;Cell(189	,10,&#39;&#39;,0,1);//end of line
//billing address
$pdf-&gt;Cell(100	,5,&#39;Bill to&#39;,0,1);//end of line
//add dummy cell at beginning of each line for indentation
$pdf-&gt;Cell(10	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(90	,5,&#39;[Name]&#39;,0,1);
$pdf-&gt;Cell(10	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(90	,5,&#39;[Company Name]&#39;,0,1);
$pdf-&gt;Cell(10	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(90	,5,&#39;[Address]&#39;,0,1);
$pdf-&gt;Cell(10	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(90	,5,&#39;[Phone]&#39;,0,1);
//make a dummy empty cell as a vertical spacer
$pdf-&gt;Cell(189	,10,&#39;&#39;,0,1);//end of line
//invoice contents
$pdf-&gt;SetFont(&#39;Arial&#39;,&#39;B&#39;,12);
$pdf-&gt;Cell(130	,5,&#39;Description&#39;,1,0);
$pdf-&gt;Cell(25	,5,&#39;Taxable&#39;,1,0);
$pdf-&gt;Cell(34	,5,&#39;Amount&#39;,1,1);//end of line
$pdf-&gt;SetFont(&#39;Arial&#39;,&#39;&#39;,12);
//Numbers are right-aligned so we give &#39;R&#39; after new line parameter
$pdf-&gt;Cell(130	,5,&#39;UltraCool Fridge&#39;,1,0);
$pdf-&gt;Cell(25	,5,&#39;-&#39;,1,0);
$pdf-&gt;Cell(34	,5,&#39;3,250&#39;,1,1,&#39;R&#39;);//end of line
$pdf-&gt;Cell(130	,5,&#39;Supaclean Diswasher&#39;,1,0);
$pdf-&gt;Cell(25	,5,&#39;-&#39;,1,0);
$pdf-&gt;Cell(34	,5,&#39;1,200&#39;,1,1,&#39;R&#39;);//end of line
$pdf-&gt;Cell(130	,5,&#39;Something Else&#39;,1,0);
$pdf-&gt;Cell(25	,5,&#39;-&#39;,1,0);
$pdf-&gt;Cell(34	,5,&#39;1,000&#39;,1,1,&#39;R&#39;);//end of line
//summary
$pdf-&gt;Cell(130	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Subtotal&#39;,0,0);
$pdf-&gt;Cell(4	,5,&#39;$&#39;,1,0);
$pdf-&gt;Cell(30	,5,&#39;4,450&#39;,1,1,&#39;R&#39;);//end of line
$pdf-&gt;Cell(130	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Taxable&#39;,0,0);
$pdf-&gt;Cell(4	,5,&#39;$&#39;,1,0);
$pdf-&gt;Cell(30	,5,&#39;0&#39;,1,1,&#39;R&#39;);//end of line
$pdf-&gt;Cell(130	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Tax Rate&#39;,0,0);
$pdf-&gt;Cell(4	,5,&#39;$&#39;,1,0);
$pdf-&gt;Cell(30	,5,&#39;10%&#39;,1,1,&#39;R&#39;);//end of line
$pdf-&gt;Cell(130	,5,&#39;&#39;,0,0);
$pdf-&gt;Cell(25	,5,&#39;Total Due&#39;,0,0);
$pdf-&gt;Cell(4	,5,&#39;$&#39;,1,0);
$pdf-&gt;Cell(30	,5,&#39;4,450&#39;,1,1,&#39;R&#39;);//end of line
$pdf-&gt;Output();
?&gt;

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,&quot;select * from add_courier&quot;);
foreach loop starts here 
&lt;button &gt; &lt;a href=&quot;printme.php&amp;id=&lt;?php echo $row[&#39;id&#39;]?&gt;&quot;&gt;Print&lt;/a&gt;
&lt;/button&gt;

for each loop ends here

in your FPDF page get the ID variable

//example//

require(&#39;fpdf.php&#39;);
$con=mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
mysqli_select_db($con,&#39;lexipressdb&#39;);
$id=$_GET[&#39;id&#39;];

alter your query with this on FPDF page

$query=mysqli_query($con,&quot;select * from add_courier where id=&#39;$id&#39;&quot;);

答案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();
?>

请注意,这些翻译是根据您提供的代码内容生成的,不包括代码中的注释。如果需要更多帮助或有其他问题,请随时提出。

英文:
&lt;table&gt;
&lt;thead&gt;
&lt;th style=&quot;font-weight:bold;&quot;&gt;Tracking&lt;/th&gt;
&lt;th style=&quot;font-weight:bold;&quot;&gt;Print Invoice&lt;/th&gt;               
&lt;/thead&gt;
&lt;tbody&gt;
&lt;?php
$ret=mysqli_query($con,&quot;select *from add_courier&quot;);
$cnt=1;
while ($row=mysqli_fetch_array($ret)) {
?&gt;                                        
&lt;tr&gt;
&lt;td style=&quot;font-weight:bold;&quot;&gt;&lt;?php  echo $row[&#39;trackingid&#39;];?&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;a href=&quot;invoice/generate_pdf.php?id=&lt;?php  echo $row[&#39;trackingid&#39;];
//this is your tracking primary key column name
?&gt;&quot;&gt;Generate&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;?php
}
?&gt;			 
&lt;/tbody&gt;     
&lt;/table&gt;
--------------------------------------------------------------------------------------------------------

generate_pdf.php code

 &lt;?php
require(&#39;fpdf.php&#39;);
$con=mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);
mysqli_select_db($con,&#39;lexipressdb&#39;);
$id=$_GET[&#39;id&#39;];

// 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-&gt;SetFont(&#39;Arial&#39;,&#39;B&#39;,15);
//dummy cell to put logo
//$this-&gt;Cell(12,0,&#39;&#39;,0,0);
//is equivalent to:
$this-&gt;Cell(12);
//put logo
$this-&gt;Image(&#39;gift.jpg&#39;,10,10,10);
$this-&gt;Cell(100,10,&#39;Invoice&#39;,0,1);
//dummy cell to give line spacing
//$this-&gt;Cell(0,5,&#39;&#39;,0,1);
//is equivalent to:
$this-&gt;Ln(5);
$this-&gt;SetFont(&#39;Arial&#39;,&#39;B&#39;,11);
$this-&gt;SetFillColor(180,180,255);
$this-&gt;SetDrawColor(180,180,255);
$this-&gt;Cell(40,5,&#39;Order Invoice&#39;,1,0,&#39;&#39;,true);
$this-&gt;Cell(25,5,&#39;Quantity&#39;,1,0,&#39;&#39;,true);
$this-&gt;Cell(65,5,&#39;Weight&#39;,1,0,&#39;&#39;,true);
$this-&gt;Cell(60,5,&#39;Cost&#39;,1,1,&#39;&#39;,true);
}
function Footer(){
//add table&#39;s bottom line
$this-&gt;Cell(190,0,&#39;&#39;,&#39;T&#39;,1,&#39;&#39;,true);
//Go to 1.5 cm from bottom
$this-&gt;SetY(-15);
$this-&gt;SetFont(&#39;Arial&#39;,&#39;&#39;,8);
//width = 0 means the cell is extended up to the right margin
$this-&gt;Cell(0,10,&#39;Page &#39;.$this-&gt;PageNo().&quot; / {pages}&quot;,0,0,&#39;C&#39;);
}
}
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm
$pdf = new PDF(&#39;P&#39;,&#39;mm&#39;,&#39;A4&#39;); //use new class
//define new alias for total page numbers
$pdf-&gt;AliasNbPages(&#39;{pages}&#39;);
$pdf-&gt;SetAutoPageBreak(true,15);
$pdf-&gt;AddPage();
$pdf-&gt;SetFont(&#39;Arial&#39;,&#39;&#39;,9);
$pdf-&gt;SetDrawColor(180,180,255);
$query=mysqli_query($con,&quot;select * from add_courier where id=&#39;$id&#39;&quot;);
while($data=mysqli_fetch_array($query)){
$pdf-&gt;Cell(40,5,$data[&#39;order_inv&#39;],&#39;LR&#39;,0);
$pdf-&gt;Cell(25,5,$data[&#39;r_qnty&#39;],&#39;LR&#39;,0);
$pdf-&gt;Cell(60,5,$data[&#39;r_weight&#39;],&#39;LR&#39;,0);
$pdf-&gt;Cell(60,5,$data[&#39;r_costtotal&#39;],&#39;LR&#39;,1);
}
$pdf-&gt;Output();
?&gt;

huangapple
  • 本文由 发表于 2020年1月6日 16:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608868.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定