Jump to content

How to save pdf invoices manually and automatically?


Somepub

Recommended Posts

I have solution for saving invoices to my server

 

I override the pdf files

Here is my code:

 

PDF.PHP

<?php

class PDF extends PDFCore
{
    public function render($display = true)
    {
        if($this->template == PDF::TEMPLATE_INVOICE)
            parent::render('F', true); //This renders to automatic save

        return parent::render($display);
    }
}

?>

PDFGenerator.PHP

<?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 = '/place/folder/invoices/print_it/'.str_replace("#", "", $filename); //file name starts with #, replacing for printing
        } else {
            $output = 'I';
        }

        return $this->output($filename, $output);
    }
}

?>

All is working fine with my automatic invoice save, but when I'm downloading invoice manually(clicking view invoice in adminpanel), I'm getting an error or just a blank pdf file. So I can understand that this problem occurs with PDF.PHP file..

 

I tried to render both, but that dident help:

 

PDF.PHP

<?php

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

        return parent::render($display);
    }
}

?>

It only renders the first of the parent::render, if  parent::render('I', true) was first, I could download invoices manually, but automatic invoice save would be blank

 

Soo.. is there any solution to this?

Link to comment
Share on other sites

  • 5 months later...

Hello @compleus,

I did found a solution for this!
 

class PDF extends PDFCore
{
    public function render($display = true)
    {
        if($this->template == PDF::TEMPLATE_INVOICE && Tools::getValue('printer') == 1) {
			// Output PDF to local file
            parent::render('F');
            //echo "<script>window.close();</script>"; // added javascript for automatic closure after link print clicked
        }
        else {
            return parent::render($display); // Saving manually
        }
    }
}

The link for downloading pdf:

http://www.example.com/admin/index.php?controller=AdminPdf&submitAction=generateInvoicePDF&id_order=3230&print=1
Link to comment
Share on other sites

  • 4 years later...
1 minute ago, LucasV said:

Hi @Somepub, I'd like to integrate this aswel on my PS installation. 

I can't seem to find the pdf.php file. Where do I find these files and where do I override them? 

Thanks!

Lucas

Just found the files in classes/pdf/  Now I'm still lokkig for the correct way to override. 

Link to comment
Share on other sites

  • 2 years later...

Hi everyone,

I am still looking for the right solution, but while I'm investigating I have to share that @compleus proposal is not the right way to go. First of all, adding a condition "Tools::getValue('printer') == 1" means the pdf file will not be saved when you finally succeed to display the pdf file instead of the blank page with errors inside by clicking on a the link.

Second, even if its not the cause of the issue, you are not required to put an "else" AND a "return" for the normal behavior case. For readability it's best to only use one syntax at a time.
Why removing "else" ? => When you calling the "parent:render()" function with a return, there is no way to access the next "return parent::render($display);" in the same execution cycle.

Additionally, but this is just a personal requirement, I would like the credit notes (oddly named OrderSlip in Prestashop) to also be stored, because like invoices, they are accounting documents that I need to be able to download easily.

So even though the following code doesn't solve the problem of blank PDF while saving it, I show you a more elegant way to write the code:

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

        return parent::render($display);
    }
}
Edited by qu3nd1 (see edit history)
Link to comment
Share on other sites

Okay, by analyzing how the tcpdf Output() function works, I found the perfect solution. We need to understand that destination "F" will only store the file without being able to send it to the browser properly, while "FI" can store and send online content to the browser (pdf file will be displayed in the same tab basically) , and "FD" can store and trigger a download of the file (in a modern browser this will usually open a new tab with the file displayed inside). I chose download.

PDF.php

<?php

class PDF extends PDFCore
{
    public function render($display = true)
    {
        if(in_array($this->template, [PDF::TEMPLATE_INVOICE, PDF::TEMPLATE_ORDER_SLIP]))
            return parent::render('FD');

        return parent::render($display);
    }
}

PDFGenerator.php

<?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 (in_array($display, ['F', 'FI', 'FD'])) {
            $output = $display;
            $filename = _PS_ROOT_DIR_.'/invoices/'.$filename;
        } else {
            $output = 'I';
        }

        return $this->Output($filename, $output);
    }
}

Everyone can modify this to suit their needs, but this is the more robust solution I've found. 😁

Edited by qu3nd1 (see edit history)
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...