Php and dompdf
I have a code to send my form with dompdf to a pdf file. Everything works except the php result. I can't display the php result of $total on my pdf.. Hope someone can help me!! You can find my code if you follow the link
http://searchitforyou.com/Nieuw%20bestand.txt
Code (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
if (!empty($_POST)) {
// Used for later to determine result
$success = $error = false;
// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;
// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
// Check for blank fields
if (empty($post->name) OR empty($post->email))
$error = true;
else {
// Get this directory, to include other files from
$dir = dirname(__FILE__);
// Get the contents of the pdf into a variable for later
ob_start();
require_once($dir.'/pdf.php');
$pdf_html = ob_get_contents();
ob_end_clean();
// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
// Get the contents of the HTML email into a variable for later
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('How To Create and Send An HTML Email w/ a PDF Attachment') // Message subject
->setTo(array($post->email => $post->name)) // Array of people to send to
->setFrom(array('[email protected]' => 'Nettuts+')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'nettuts.pdf', 'application/pdf')); // Attach the generated PDF from earlier
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
}
}
$sympathisant=250;
$brons=1000;
$zilver=1500;
$goud=2000;
$perssymp=125;
$persbrons=125;
$perszilver=125;
$persgoud=125;
$table_price=20;
$chair_price=5;
$drinks_price=2;
$snacks_price=1;
$total=0;
if(isset($_POST['sympathisant']) && $_POST['sympathisant']=='1')
{
$total=$total+$sympathisant;
}
if(isset($_POST['brons']) && $_POST['brons']=='1')
{
$total=$total+$brons;
}
if(isset($_POST['zilver']) && $_POST['zilver']=='1')
{
$total=$total+$zilver;
}
if(isset($_POST['goud']) && $_POST['goud']=='1')
{
$total=$total+$goud;
}
if(isset($_POST['drinks']))
{
$num=(int)$_POST['drinks']; // make sure only integers are accepted
if($num>=1) // ignore negative values
{
$total=$total+($perssymp*$num);
}
}
if(isset($_POST['perssymp']))
{
$num=(int)$_POST['perssymp']; // make sure only integers are accepted
if($num>1) // ignore negative values
{
$total=$total+($perssymp*$num-125);
}
}
if(isset($_POST['persbrons']))
{
$num=(int)$_POST['persbrons']; // make sure only integers are accepted
if($num>4) // ignore negative values
{
$total=$total+($persbrons*$num-500);
}
}
if(isset($_POST['perszilver']))
{
$num=(int)$_POST['perszilver']; // make sure only integers are accepted
if($num>6) // ignore negative values
{
$total=$total+($perszilver*$num-750);
}
}
if(isset($_POST['persgoud']))
{
$num=(int)$_POST['persgoud']; // make sure only integers are accepted
if($num>8) // ignore negative values
{
$total=$total+($persgoud*$num-1000);
}
}
echo "<p>Total price is \$$total</p>\n";
?>
if (!empty($_POST)) {
// Used for later to determine result
$success = $error = false;
// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;
// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));
// Check for blank fields
if (empty($post->name) OR empty($post->email))
$error = true;
else {
// Get this directory, to include other files from
$dir = dirname(__FILE__);
// Get the contents of the pdf into a variable for later
ob_start();
require_once($dir.'/pdf.php');
$pdf_html = ob_get_contents();
ob_end_clean();
// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
// Get the contents of the HTML email into a variable for later
ob_start();
require_once($dir.'/html.php');
$html_message = ob_get_contents();
ob_end_clean();
// Load the SwiftMailer files
require_once($dir.'/swift/swift_required.php');
$mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer
$message = Swift_Message::newInstance()
->setSubject('How To Create and Send An HTML Email w/ a PDF Attachment') // Message subject
->setTo(array($post->email => $post->name)) // Array of people to send to
->setFrom(array('[email protected]' => 'Nettuts+')) // From:
->setBody($html_message, 'text/html') // Attach that HTML message from earlier
->attach(Swift_Attachment::newInstance($pdf_content, 'nettuts.pdf', 'application/pdf')); // Attach the generated PDF from earlier
// Send the email, and show user message
if ($mailer->send($message))
$success = true;
else
$error = true;
}
}
$sympathisant=250;
$brons=1000;
$zilver=1500;
$goud=2000;
$perssymp=125;
$persbrons=125;
$perszilver=125;
$persgoud=125;
$table_price=20;
$chair_price=5;
$drinks_price=2;
$snacks_price=1;
$total=0;
if(isset($_POST['sympathisant']) && $_POST['sympathisant']=='1')
{
$total=$total+$sympathisant;
}
if(isset($_POST['brons']) && $_POST['brons']=='1')
{
$total=$total+$brons;
}
if(isset($_POST['zilver']) && $_POST['zilver']=='1')
{
$total=$total+$zilver;
}
if(isset($_POST['goud']) && $_POST['goud']=='1')
{
$total=$total+$goud;
}
if(isset($_POST['drinks']))
{
$num=(int)$_POST['drinks']; // make sure only integers are accepted
if($num>=1) // ignore negative values
{
$total=$total+($perssymp*$num);
}
}
if(isset($_POST['perssymp']))
{
$num=(int)$_POST['perssymp']; // make sure only integers are accepted
if($num>1) // ignore negative values
{
$total=$total+($perssymp*$num-125);
}
}
if(isset($_POST['persbrons']))
{
$num=(int)$_POST['persbrons']; // make sure only integers are accepted
if($num>4) // ignore negative values
{
$total=$total+($persbrons*$num-500);
}
}
if(isset($_POST['perszilver']))
{
$num=(int)$_POST['perszilver']; // make sure only integers are accepted
if($num>6) // ignore negative values
{
$total=$total+($perszilver*$num-750);
}
}
if(isset($_POST['persgoud']))
{
$num=(int)$_POST['persgoud']; // make sure only integers are accepted
if($num>8) // ignore negative values
{
$total=$total+($persgoud*$num-1000);
}
}
echo "<p>Total price is \$$total</p>\n";
?>
{table}
<form id="pricecalculation" method="post" action="">
<table>
<tr>
<td><label for="name">Your Name:</label></td>
<td><input type="text" name="name" id="name" class="input" /></td>
<td><label for="email">Your Email:</label>
<td><input type="text" name="email" id="email" class="input" /></td>
<tr>
<td>Sympathisant</td>
<td><input type="checkbox" name="sympathisant" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
<tr>
<td>Hoeveel personen? </td>
<td><input name="perssymp" type="text" id="perssymp" size="3"></td>
</tr>
</br>
<tr>
<td>Brons</td>
<td><input type="checkbox" name="brons" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
</tr>
<tr>
<td>Hoeveel personen? </td>
<td><input name="persbrons" type="text" id="persbrons" size="3"></td>
</tr>
<br>
<tr>
<td>Zilver</td>
<td><input type="checkbox" name="zilver" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
</tr>
<tr>
<td>Hoeveel personen?</td>
<td><input name="perszilver" type="text" id="perszilver" size="3"></td>
</tr>
<tr>
</br>
<tr>
<td>Goud</td>
<td><input type="checkbox" name="goud" value="1" />Yes</td>
<td><input type="hidden" name="foo" value="0" /></td>
</tr>
<tr>
<td>Hoeveel personen? </td>
<td><input name="persgoud" type="text" id="persgoud" size="3"></td>
</tr>
</table>
<div id='total' name='total'>
<script type="text/php">
echo "<p>Total price is \$$total</p>\n";
</script>
</div>
<input name="submit" type="submit" value="Calculate price"/>
</form>
[/table]
Gewijzigd op 13/10/2015 13:23:02 door Greg gil
Its a better way of handling i think.
Rickert Bombaklats op 13/10/2015 13:23:37:
Why.. don't you just make a real object and work that one out.
Its a better way of handling i think.
Its a better way of handling i think.
Bedankt voor de reactie maar kan je iets meer uitleg geven?
Als je nu een object maakt of ene class als service die tussen je applicatie staat en je pdf-lib. dan kan je die vullen e.d en daarna met een process() method het object laten omzetten naar een pdf.
Meerdere objecten die allemaal 1 pdf zijn.
Hopelijk snap je wat ik bedoel.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
<?php
class pdfService {
}
$offerte = new PdfService();
$offerte->contentData();
$offerte->process();
$offerte->saveTo('path/to/save?');
?>
class pdfService {
}
$offerte = new PdfService();
$offerte->contentData();
$offerte->process();
$offerte->saveTo('path/to/save?');
?>
Het is maar wat bedenksel maar wellicht kan je er iets mee. Zelf zou ik proberen van alles een object te maken en zo weinig mogelijk arrays te gebruiken.
OOP is immers Object gericht.