Jump to content

Generate pdf invoice file when order is completed


Recommended Posts

Hi, 

 

I'm trying to develop a module for my store that automatically generates a PDF of the order invoice upon completion of the order process. 

I used the actionValidateOrder hook and I am able to make my code run at the right moment, but I have no clue how to generate the PDF in a specific folder of my server (the save part is not the issue, but the generate PDF is). 

I'm completely clueless. 

Can anyone help me? 

Link to comment
Share on other sites

//You have the $order object, right?

$order_invoice_list = $order->getInvoicesCollection();
//The pdf generator
$pdf = new PDF($order_invoice_list, PDF::TEMPLATE_INVOICE, $this->context->smarty);
//Actual pdf file
$file_attachement['content'] = $pdf->render(false);
$file_attachement['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
$file_attachement['mime'] = 'application/pdf';

 Happy Coding. :)

Link to comment
Share on other sites

Hi,

Kindly confirm if you want to generate the Invoice PDF OR Custom PDF?

 

To generate the custom PD, You can use the Dompdf library to generate PDF i.e. https://github.com/dompdf/dompdf

For Example:

You can use the below code to generate as well as save the PDF in the specific folder.

include_once(_PS_MODULE_DIR_.'MODULE_NAME/libraries/dompdf/dompdf_config.inc.php');

$html_content = $this->context->smarty->fetch(_PS_MODULE_DIR_.'MODULE_NAME/views/templates/front/front.tpl');
$dompdf = new DOMPDF();
$html = utf8_decode($html_content);
$dompdf->load_html($html);
$dompdf->render();

$file_name = FILE_PATH;
file_put_contents($file_name, $dompdf->output());
$dompdf->stream($file_name, array('Attachment' => 0));

The above code will help you to generate as well as save PDF file. You can customize the PDF as $html_content is used to provide the content of the pdf.

Link to comment
Share on other sites

  • 1 month later...
On 8/8/2018 at 12:54 PM, Ram Chandra said:

//You have the $order object, right?

$order_invoice_list = $order->getInvoicesCollection();
//The pdf generator
$pdf = new PDF($order_invoice_list, PDF::TEMPLATE_INVOICE, $this->context->smarty);
//Actual pdf file
$file_attachement['content'] = $pdf->render(false);
$file_attachement['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
$file_attachement['mime'] = 'application/pdf';

 Happy Coding. :)

 

Hi, I had to put this project on hold for a while, but I will give this a try and let you know if it works.

Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...