Search code examples
prestashopprestashop-1.6invoice

Prestashop, Generating invoice(pdf) automatically


So I'm looking for a solution to save invoice automatically into my server folder, when I press view invoice as the generated URL occurs (http://www.example.com/admin11111/index.php?controller=AdminPdf&token="token"&submitAction=generateInvoicePDF&id_order="id").

I also did research on google, but this solution, somehow didnt work for me: https://www.prestash...es-in-a-folder/

From Prestashop Forum I got advice that I should use shell script, but using a shell download like wget or other only gets me html file, because when I download the invoice in Prestashop backoffice.. it takes some time to generate and the download save appears later.


Solution

  • With this 2 overrides you could accomplish this.

    Override PDF.php:

    class PDF extends PDFCore
    {
        public function render($display = true)
        {
            if($this->template == PDF::TEMPLATE_INVOICE)
                parent::render('F', true);
    
            return parent::render($display);
        }
    }
    

    Override PDFGenerator.php:

    class PDFGenerator extends PDFGeneratorCore
    {
        public function render($filename, $display = true)
        {
            if (empty($filename)) {
                throw new PrestaShopException('Missing filename.');
            }
    
            $this->lastPage();
    
            if ($display === true) {
                $output = 'D';
            } elseif ($display === false) {
                $output = 'S';
            } elseif ($display == 'D') {
                $output = 'D';
            } elseif ($display == 'S') {
                $output = 'S';
            } elseif ($display == 'F') {
                $output = 'F';
                $filename = _PS_ROOT_DIR_.'/'.$filename;
            } else {
                $output = 'I';
            }
    
            return $this->output($filename, $output);
        }
    }
    

    Remember to choose another folder other than _PS_ROOT_DIR_. This was just for testing. Try $filename = _PS_ROOT_DIR_.'/../invoices/'.$filename; so it's not a public folder (and you must create the folder with the correct permissions.