I am currently working on developing an external tool (in JavaScript) connected to the Prestashop APIs. The Prestashop APIs provide various pieces of information, but there is no API to download or generate attachments.
I need to generate a PDF file per order validated (rather than a global PDF as offered by default).
Do you know if there is a module available to perform this operation? If not, do you have any ideas on how I should proceed to generate PDFs and make them accessible via a new module for dowload invoice by API?
Prestashop Web Services does not allow you to download a 'copy' of invoices. You can consult invoice details such as total paid, delivery date, ... more information can be found in the Prestashop DevDocs (Webservice Api - Order Invoices)
We can build our own php page where we use the built-in functions of Prestashop to make an exact copy of the PDF invoices.
For this we have to use the Prestashop classes OrderInvoice.php
and PDF.php
.
We also want to use some of the standard functions of Prestashop such as 'Context' for this we need to add both config/config.inc.php
and init.php
to our code.
Next, we don't want anyone to have access to this url. Because otherwise we commit infringements against the GDPR. Your invoices contain sensitive address and payment details of all your customers.
In our example we are going to use a secure_key
which will allow only authorized users to access your page.
To round off the whole thing, it is also interesting that we can immediately indicate from which order we want to download the invoice, for this we will use a parameter called id_order
.
Putting all this together we can easily add our file getPDFinvoice.php
in the root of our shop, and you can then access it by surfing to https://yourshop.com/getPDFinvoice.php?secure_key=YOUR_UNIQE_CODE&id_order=WEBSHOP_ORDER_ID
.
getPDFinvoice.php
<?php
// Setup connection with config.inc.php (required for database connection, ...)
include 'config/config.inc.php';
include 'init.php';
include 'classes/order/OrderInvoice.php';
include 'classes/pdf/PDF.php';
// Declare our secure_key. Remeber the secure_key provided is an example, make sure to create one by yourself.
$secure_key = 'ed3fa1ce558e1cfbaa3f99403';
// controle if secure_key is correct, if the secure_key is not set our not equal the php page will stop running.
if (!Tools::getValue('secure_key') || Tools::getValue('secure_key') != $secure_key) die('UNAUTHORIZED: We dont want you on this page!');
$id_order = (int)Tools::getValue('id_order');
if (!$id_order) die("Please provide and order ID!");
$order = new Order($id_order);
if (!Validate::isLoadedObject($order)) die('The order invoice cannot be found within your database.');
$pdf = new PDF($order->getInvoicesCollection(), 'Invoice', Context::getContext()->smarty);
$pdf->render();
Please note: This is code has been tested on a Prestashop 8.0.2 build using PHP 8.1. When using a different version, certain functions may not work or certain error messages may be displayed.