Php Create Pdf Invoice

PHP create PDF invoice

I use TCPDF (see http://www.tcpdf.org/) for this: its pretty capable and not too painful to get setup. I will say that depending on your data source you may have some issues. In my case my data is sourced from a SOAP interface to my accounting system and use CodeIgniter for my app, and I can do this:

$address = $extraclient->get_company_address();

// generate the PDF invoice
$this->load->library('pdfinvoice');

// set document information
$this->pdfinvoice->SetSubject("Invoice " . $data_invoice['code_invoice']);

// add a page
$this->pdfinvoice->AddPage();
$this->pdfinvoice->StartPageOffset();

// write the client's details out
$width = $this->pdfinvoice->GetPageWidth()/2;
$margins = $this->pdfinvoice->getMargins();
$this->pdfinvoice->SetFont('times', 'b', $this->pdfinvoice->bigFont );
$this->_row($width, array("From:", "To:"));
$this->pdfinvoice->SetFont('times', 'i', $this->pdfinvoice->smallFont );
$this->_row($width, array("MY NAME", $customer['name_contact']));
$this->_row($width, array($address['phone'], $customer['name_customer']));
$this->_row($width, array($address['street'], $customer['address1_street']));
$this->_row($width, array($address['city']. ", ".$address['state']." ".$address['zipcode'],
$customer['address1_city']. ", ".$customer['address1_state']." ".$customer['address1_zip

The full code is quite frankly too long to insert here, but you should get the idea, and you get fairly precise layout control.

Generate and get invoice PDF via PHP with WooCommerce PDF Invoices & Packing Slips

If you want to create a PDF in code, you can do it like this:

$invoice = wcpdf_get_document( 'invoice', $order, true );
$pdf_data = $invoice->get_pdf();

You can then use the PDF data to save to a file (file_put_contents etc)

If you want to use the admin-ajax link, you can create the nonce like this:

wp_nonce_url( admin_url( 'admin-ajax.php?action=generate_wpo_wcpdf&template_type=invoice&order_ids=' . $list_of_order_ids ), 'generate_wpo_wcpdf' )

how to create invoice pdf with php and fpdf?

Firstly I would suggest to use TCPDF

You can do following steps to generate pdf:

  1. Gather all the data in session.

  2. After confirming order get all the data from the session and use that data in generating pdf file

  3. Use following link to get the example how to generate pdf. TCPDF Examples It will help you to code for generating pdf.

  4. For unique user pdf generation you can rename the file name with user_id + timestamp

possible options to create pdf file using html elements to generate invoice in php and codeigniter

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

How to Generate PDF invoice to XML format using mPDF

first you have to create the XML file then include it inside SetAssociatedFiles like this:

your code:

'PDFA' => true,
'PDFAauto' => true,
//'PDFAversion'=> 'A-3',
]);

$mpdf->SetAssociatedFiles([[
'name' => 'public_filename.xml',
'mime' => 'text/xml',
'description' => 'some description',
'AFRelationship' => 'Alternative',
'path' => __DIR__ . '/TaxInvoice.xml'
]]);

$rdf = '<rdf:Description rdf:about="" xmlns:zf="urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#">'."\n";
$rdf .= ' <zf:DocumentType>INVOICE</zf:DocumentType>'."\n";
$rdf .= ' <zf:DocumentFileName>ZUGFeRD-invoice.xml</zf:DocumentFileName>'."\n";
$rdf .= ' <zf:Version>1.0</zf:Version>'."\n";
$rdf .= ' <zf:ConformanceLevel>BASIC</zf:ConformanceLevel>'."\n";
$rdf .= '</rdf:Description>'."\n";

$mpdf->SetAdditionalXmpRdf($rdf);

I edited:

'PDFA' => true,
'PDFAauto' => true,
//'PDFAversion'=> 'A-3',
]);

$mpdf->SetAssociatedFiles([[
'name' => 'thisisfilecreatedbympdf.xml',
'mime' => 'text/xml',
'description' => 'some description',
'AFRelationship' => 'Alternative',
'path' => __DIR__ . '/hereyourxmlinvoice.xml'
]]);

Codeigniter how to create PDF

Please click on this link it should work ..

http://www.php-guru.in/2013/html-to-pdf-conversion-in-codeigniter/

Or you can see below

There are number of PHP libraries on the web to convert HTML page to PDF file. They are easy to implement and deploy when you are working on any web application in core PHP. But when we try to integrate this libraries with any framework or template, then it becomes very tedious work if the framework which we are using does not have its own library to integrate it with any PDF library. The same situation came in front of me when there was one requirement to convert HTML page to PDF file and the framework I was using was codeigniter.

I searched on web and got number of PHP libraries to convert HTML page to PDF file. After lot of research and googling I decided to go with TCPDF PHP library to convert HTML page to PDF file for my requirement. I found TCPDf PHP library quite easy to integrate with codeigniter and stated working on it. After successfully completing my integration of codeigniter and TCPDF, I thought of sharing this script on web.

Now, let’s start with implimentation of the code.

Download the TCPDF library code, you can download it from TCPDF website http://www.tcpdf.org/.

Now create “tcpdf” folder in “application/helpers/” directory of your web application which is developed in codeigniter. Copy all TCPDF library files and paste it in “application/helpers/tcpdf/” directory. Update the configuration file “tcpdf_config.php” of TCPDF, which is located in “application/helpers/tcpdf/config” directory, do changes according to your applicatoin requirements. We can set logo, font, font size, with, height, header etc in the cofing file. Give read, write permissions to “cache” folder which is there in tcpdf folder. After defining your directory structure, updating configuration file and assigning permissions, here starts your actual coding part.

Create one PHP helper file in “application/helpers/” directory of codeigniter, say “pdf_helper.php”, then copy below given code and paste it in helper file

Helper: application/helpers/pdf_helper.php

function tcpdf()
{
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
}

Then in controller file call the above helper, suppose our controller file is “createpdf.php” and it has method as pdf(), so the method pdf() will load the “pdf_helper” helper and will also have any other code.

Controller: application/controllers/createpdf.php

function pdf()
{
$this->load->helper('pdf_helper');
/*
---- ---- ---- ----
your code here
---- ---- ---- ----
*/
$this->load->view('pdfreport', $data);
}

Now create one view file, say “pdfreport.php” in “application/views/” directory, which is also loaded in pdf() method in controller. So in view file we can directly call the tcpdf() function which we have defined in “pdf_helper” helper, which will load all required TCPDF classes, functions, variable etc. Then we can directly use the TCPDF example codes as it is in our current controller or view. Now in out current view “pdfreport” copy the given code below:

View: application/views/pdfreport.php

tcpdf();
$obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$obj_pdf->SetCreator(PDF_CREATOR);
$title = "PDF Report";
$obj_pdf->SetTitle($title);
$obj_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, $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->SetHeaderMargin(PDF_MARGIN_HEADER);
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$obj_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$obj_pdf->SetFont('helvetica', '', 9);
$obj_pdf->setFontSubsetting(false);
$obj_pdf->AddPage();
ob_start();
// we can have any view part here like HTML, PHP etc
$content = ob_get_contents();
ob_end_clean();
$obj_pdf->writeHTML($content, true, false, true, false, '');
$obj_pdf->Output('output.pdf', 'I');

Thus our HTML page will be converted to PDF using TCPDF in CodeIgniter. We can also embed images,css,modifications in PDF file by using TCPDF library.



Related Topics



Leave a reply



Submit