-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice.php
93 lines (86 loc) · 3.33 KB
/
invoice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
require 'vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;
// http://localhost:8000/invoice.php?invoice_id=INV2674984&customer_id=CUST1589
//Data Retreival/////Ek sal hier n query op die DB doen
$customer_id = $_GET["customer_id"];
$invoice_id = $_GET["invoice_id"];
$email_to = "[email protected]";
$email_from = "[email protected]";
$file_path = 'tmp/' . $invoice_id. '_' . $customer_id . '.pdf';
// Generate HTML and convert to PDF
$pdf = new Pdf();
$pdf->addPage(
"<html>"
. "<body>"
. "<div>Invoice " . $invoice_id
. "<div>From"
. "<div class='detail'>From Name</div>"
. "<div class='detail'>From Details</div>"
. "<div class='detail'>From Address</div>"
. "</div>"
."<div>To"
. "<div class='detail'>email</div>"
. "<div class='detail'>contact number</div>"
. "<div class='detail'>physical address</div>"
. "<div class='detail'>medical aid</div>"
. "<div class='detail'>number and main memmber</div>"
. "<div class='detail'>ID number</div>"
. "</div>"
."<table>Items"
."<tr>"
. "<th class='detail'>Description</th>"
. "<th class='detail'>Quantity</th>"
. "<th class='detail'>Price</th>"
. "<th class='detail'>Total</th"
."</tr>"
."<tr>"
."<td>Test item</td>"
."<td>2</td>"
."<td>10</td>"
."<td>20</td>"
. "</tr>"
."<tr>"
."<td>Test item 2</td>"
."<td>2</td>"
."<td>20</td>"
."<td>40</td>"
. "</tr>"
. "</table>"
."<div>Message"
. "<div class='detail'>Footer van die Invoice</div>"
."</div>"
. "</div>"
. "</body>"
. "</html>"
);
// Save the pdf to the temp directory
if (!$pdf->saveAs($file_path)) {
// an error has occurred notify the user
echo $pdf->getError();
die();
} else {
// The pdf has successfully been created, send the email with the pdf attached
$sendgrid = new SendGrid('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');
$email = new SendGrid\Email();
$email
->addTo($email_to)
->setFrom($email_from)
->setSubject('Invoice: ' . $invoice_id . ' for customer: ' . $customer_id)
->setText('Hi, Please find attached your requested invoice.')
->setHtml('Hi, <br /><br />Please find attached your requested invoice.')
->addAttachment($file_path);
try {
// Send the email
$sendgrid->send($email);
// Delete the pdf from the tmp dir
unlink($file_path);
} catch(\SendGrid\Exception $e) {
// The email could not be sent, notify the user
echo $e->getCode();
foreach($e->getErrors() as $er) {
echo $er;
}
}
}
?>