Question

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.

Was it helpful?

Solution

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");
                }
        }
}

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top