5

For a small side project, I had to create a call that gets the PDF via WHMCS. I see the API can get the variables, such as quantity, invoice items etc, but I want the same PDF that the system would send if a client had placed an order. I have a PHP app.

UPDATE

Following the awesome advice below, I was able to solve this in one line:

$pdf->Output('invoice.'.$invoicenum.'.pdf', 'F');

Now every time the invoice is viewed, or emailed, the latest version (paid or unpaid) is stored at the location I chose.

David
  • 2,053
  • 2
  • 16
  • 26
rockstardev
  • 13,479
  • 39
  • 164
  • 296

2 Answers2

2

There is an article Store Pdf Invoice on ftp with this information:

1-Change in this code
INVOICESDIRECTORY - directory where I'm keeping PDF invoices
ADMINDIRECTORY - administration directory
2- Paste it in last line of invoicepdf.tpl file in Your template.

if ($status=="Paid") {
        if(strpos($_SERVER['PHP_SELF'],"ADMINDIRECTORY") === false) {
                if((strpos($_SERVER['PHP_SELF'],"dl.php") !== false) || (strpos($_SERVER['PHP_SELF'],"dl.html") !== false)) {
                        if(!file_exists("./INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf")) {
                                $pdf->Output("./INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf", "F");
                        }              
                        $fullPath = "./INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf";    
                        if ($fd = fopen ($fullPath, "r")) {
                                $fsize = filesize($fullPath);
                                $path_parts = pathinfo($fullPath);
                                $ext = strtolower($path_parts["extension"]);
                                switch ($ext) {
                                        case "pdf":
                                        header("Content-type: application/pdf"); // add here more headers for diff. extensions
                                        header("Content-Disposition: attachment; filename=\"".str_replace("-", "/", $path_parts["basename"])."\""); // use 'attachment' to force a download
                                        break;
                                        default;
                                        header("Content-type: application/octet-stream");
                                        header("Content-Disposition: filename=\"".str_replace("-", "/", $path_parts["basename"])."\"");
                                }
                                header("Content-length: $fsize");
                                header("Cache-control: private"); //use this to open files directly
                                while(!feof($fd)) {
                                        $buffer = fread($fd, 2048);
                                        echo $buffer;
                                }
                        }
                        fclose ($fd);
                        exit;
                }
        }
        else {
                if(!file_exists("./../INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf")) {
                        $pdf->Output("./../INVOICESDIRECTORY/".str_replace("/", "-", $invoicenum).".pdf", "F");
                }
        }
}
MikroDel
  • 6,705
  • 7
  • 39
  • 74
  • Perfect for what I need. Can't believe I didn't think of just writing it out when it gets viewed, instead of trying to access it as an admin and all kinds of crazy hacks. Thank you! – rockstardev Nov 18 '13 at 17:59
  • @coderama ok nice to help you =) why dont you upvote and accept than? – MikroDel Nov 18 '13 at 18:01
  • Was just on it. Just wanted to make sure it works as I expected. :-) I'm going to comment my quick hack. A lot of what you posted is a bit overkill. – rockstardev Nov 18 '13 at 18:18
  • @coderama - good :) And what is about this bounty? Will it be given automatically after some times? – MikroDel Nov 19 '13 at 09:03
  • Yes, bounty will be given to you when the timer expires. I think it already has. – rockstardev Nov 22 '13 at 07:18
1

A better solution can be found here. This involves creating an API call that base64 encodes the result to you. Much more sophisticated.

rockstardev
  • 13,479
  • 39
  • 164
  • 296