Link to home
Start Free TrialLog in
Avatar of The_Big_Tuna
The_Big_TunaFlag for United States of America

asked on

How do I create a PDF file from a form in PHP with simple calculations. Basically...a PHP to PDF form. Help!

I am turning to the Experts on this one (MANY thanks in advance)...
I need to create a form that users fill in that asks for how many items they want and when the Submit button is pushed, it (the form) multiplies the # of items x a price (that we adjust) and then adds all the prices (from all the items x cost) and then spits the total cost in a PDF form that the user can see/download.  

Any thoughts on this?  Please help!  

-Tuna
Avatar of slyong
slyong

Hi,

If you have PDFLib, you can create PDF on the fly using PHP scripts.  The documentation is here http://au2.php.net/manual/en/ref.pdf.php.  If you have something more specific you can post it here.
Avatar of The_Big_Tuna

ASKER

ugh...I was hoping to use something free like FPDF.  I think the main question is "how hte heck do I do the calculations?"  I know it it scripting...but I am not that good at scripting. :)
The code has not been tested.. so you might need to debug here and there.  Say you have a order.html:

<html><body>
<h4>My Order Form</h4>
<form action="process.php" method="post">
<select name="item">
<option>Apple</option>
<option>Orange</option>
<option>Pear</option>
</select>
Quantity: <input name="quantity" type="text" />
<input type="submit" />
</form>
</body></html>

and a process.php
<?php
$line = "You ordered ". $_POST['quantity'] . " " . $_POST['item'];

$p = PDF_new();

/*  open new PDF file; insert a file name to create the PDF on disk */
if (PDF_begin_document($p, "", "") == 0) {
   die("Error: " . PDF_get_errmsg($p));
}

PDF_set_info($p, "Creator", "hello.php");
PDF_set_info($p, "Author", "Rainer Schaaf");
PDF_set_info($p, "Title", "Hello world (PHP)!");

PDF_begin_page_ext($p, 595, 842, "");

$font = PDF_load_font($p, "Helvetica-Bold", "winansi", "");

PDF_setfont($p, $font, 24.0);
PDF_set_text_pos($p, 50, 700);
PDF_show($p, $line);
PDF_end_page_ext($p, "");

PDF_end_document($p, "");

$buf = PDF_get_buffer($p);
$len = strlen($buf);

header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $buf;

PDF_delete($p);

?>
I could not get it to work with fpdf.  What am I doing wrong?
I was hoping to have a form that looked like this (I dont know if I needed the value in it or not):

<form id="form1" name="form1" method="post" action="">
<table width="400" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td width="200" align="right">Number of Item #1 </td>
    <td><input name="item1" type="text" size="10" /></td>
  </tr>
  <tr>
    <td width="200" align="right">Number of Item #2</td>
    <td><input name="item2" type="text" size="10" /></td>
  </tr>
  <tr>
    <td width="200" align="right">Number of Item #3</td>
    <td><input name="item3" type="text" size="10" /></td>
  </tr>
  <tr>
    <td width="200" align="right">Number of Item #4</td>
    <td><input name="item4" type="text" size="10" /></td>
  </tr>
  <tr>
    <td width="200" align="right">Number of Item #5</td>
    <td><input name="item5" type="text" size="10" /></td>
  </tr>
  <tr>
    <td width="200" align="right">Number of Item #6 </td>
    <td><input name="item6" type="text" size="10" /></td>
  </tr>
  <tr>
    <td width="200" align="right">Total cost </td>
    <td><input name="total" type="text" size="10" /></td>
  </tr>
</table>
</form>
May I ask what is this for?  Is it for your work or for school project or just for learning how to do this?  Because if you don't have any basic knowledge, then I would explain it in more details.
Well...to be frank...it is for work. I was tasked with a project to create a PDF proposal that is generated from a form in PHP.  Although I am tasked with the website stuff (until we get someone that knows what they are doing), I have little to no knowloedge of PHP...mostly generic design, HTML, Flash.  I hope that helps.  I have been working on this for weeks...but no success.  I gave up and decided to ask ya all.  :)
I trust you have PHP setup and running correctly on your web server?

And you also have the fpdf files? Now, copy the fpdf.php file + the whole font folder found in the fpdf package to the web server folder where you site's files are contained.

In your HTML file were the form is, change <form id="form1" name="form1" method="post" action=""> to <form id="form1" name="form1" method="post" action="process.php">
Also put a submit button on your form.

Now, create a text file called process.php and cut and paste the following:

<?php

//set the prices here...
$item1p = 1.89;
$item2p = 2.34;
$item3p = 3.56;
$item4p = 4.12;
$item5p = 5.78;
$item6p = 6.12;

$item1 = $_POST['item1'] * $item1p;
$item2 = $_POST['item2'] * $item2p;
$item3 = $_POST['item3'] * $item3p;
$item4 = $_POST['item4'] * $item4p;
$item5 = $_POST['item5'] * $item5p;
$item6 = $_POST['item6'] * $item6p;

require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->Cell(40,10,'Item1: $'.$item1);

$pdf->setXY(10,20);
$pdf->Cell(40,10,'Item2: $'.$item2);

$pdf->setXY(10,30);
$pdf->Cell(40,10,'Item3: $'.$item3);

$pdf->setXY(10,40);
$pdf->Cell(40,10,'Item4: $'.$item4);

$pdf->setXY(10,50);
$pdf->Cell(40,10,'Item5: $'.$item5);

$pdf->setXY(10,60);
$pdf->Cell(40,10,'Item6: $'.$item6);

$pdf->setXY(10,70);
$pdf->Cell(40,10,'Total: $'.($item1+$item2+$item3+$item4+$item5+$item6));

$pdf->Output();
?>


OK, now save this file and copy to your web server folder. All these files must be in the same folder.

That should do it! I've tested this and it works.

j
SOOOO close!  I tested it and everything is working great...except...is there a way to make sure that the total for each item and the grand total that comes out is two decimals?  It looks like if the total is $6.50 it only shows "$6.5". Also, how can i list the quantity of items next to the item number?  i.e. the output would be Qty of item1 x price under the line $pdf->Cell(40,10,'Item1: $'.$item1); but in a smaller font?  
Is that possible? If you can help me with this I will leave my wife for you...after she approves it.  :)

Use something like:

$formatted = sprintf("%01.2f", $item1);
$pdf->Cell(40,10,'Item1: $'.$formatted);
thanks slyong :-)

replace process.php with this:

<?php

//set the prices here...
$item1p = 1.89;
$item2p = 2.34;
$item3p = 3.56;
$item4p = 4.12;
$item5p = 5.78;
$item6p = 6.12;

$item1 = sprintf("%01.2f", $_POST['item1'] * $item1p);
$item2 = sprintf("%01.2f", $_POST['item2'] * $item2p);
$item3 = sprintf("%01.2f", $_POST['item3'] * $item3p);
$item4 = sprintf("%01.2f", $_POST['item4'] * $item4p);
$item5 = sprintf("%01.2f", $_POST['item5'] * $item5p);
$item6 = sprintf("%01.2f", $_POST['item6'] * $item6p);

//qty text
$i1 = $_POST['item1'].' x $'.$item1p;
$i2 = $_POST['item2'].' x $'.$item2p;
$i3 = $_POST['item3'].' x $'.$item3p;
$i4 = $_POST['item4'].' x $'.$item4p;
$i5 = $_POST['item5'].' x $'.$item5p;
$i6 = $_POST['item6'].' x $'.$item6p;

require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->Cell(40,10,'Item1: $'.$item1);

$pdf->setXY(10,20);
$pdf->Cell(40,10,'Item2: $'.$item2);

$pdf->setXY(10,30);
$pdf->Cell(40,10,'Item3: $'.$item3);

$pdf->setXY(10,40);
$pdf->Cell(40,10,'Item4: $'.$item4);

$pdf->setXY(10,50);
$pdf->Cell(40,10,'Item5: $'.$item5);

$pdf->setXY(10,60);
$pdf->Cell(40,10,'Item6: $'.$item6);

$pdf->setXY(10,70);
$pdf->Cell(40,10,'Total: $'.($item1+$item2+$item3+$item4+$item5+$item6));

$pdf->SetFont('Arial','B',10);

$pdf->setXY(10,15);
$pdf->Cell(40,10,$i1);

$pdf->setXY(10,25);
$pdf->Cell(40,10,$i2);

$pdf->setXY(10,35);
$pdf->Cell(40,10,$i3);

$pdf->setXY(10,45);
$pdf->Cell(40,10,$i4);

$pdf->setXY(10,55);
$pdf->Cell(40,10,$i5);

$pdf->setXY(10,65);
$pdf->Cell(40,10,$i6);

$pdf->Output();
?>



YOU GUYS ROCK!
ok...it looks like we are on the last step.  Do you know how to get the total to come out as a 2 decimal outcome?  
Also, how would I put a header/footer picture in the output?  
<?php

class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('my_logo.png',10,8,33);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(80);
    //Title
    $this->Cell(30,10,'Title',1,0,'C');
    //Line break
    $this->Ln(20);
}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

//set the prices here...
$item1p = 1.89;
$item2p = 2.34;
$item3p = 3.56;
$item4p = 4.12;
$item5p = 5.78;
$item6p = 6.12;

$item1 = sprintf("%01.2f", $_POST['item1'] * $item1p);
$item2 = sprintf("%01.2f", $_POST['item2'] * $item2p);
$item3 = sprintf("%01.2f", $_POST['item3'] * $item3p);
$item4 = sprintf("%01.2f", $_POST['item4'] * $item4p);
$item5 = sprintf("%01.2f", $_POST['item5'] * $item5p);
$item6 = sprintf("%01.2f", $_POST['item6'] * $item6p);
$total = sprintf("%01.2f", $item1+$item2+$item3+$item4+$item5+$item6);

//qty text
$i1 = $_POST['item1'].' x $'.$item1p;
$i2 = $_POST['item2'].' x $'.$item2p;
$i3 = $_POST['item3'].' x $'.$item3p;
$i4 = $_POST['item4'].' x $'.$item4p;
$i5 = $_POST['item5'].' x $'.$item5p;
$i6 = $_POST['item6'].' x $'.$item6p;

require('fpdf.php');

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->Cell(40,10,'Item1: $'.$item1);

$pdf->setXY(10,20);
$pdf->Cell(40,10,'Item2: $'.$item2);

$pdf->setXY(10,30);
$pdf->Cell(40,10,'Item3: $'.$item3);

$pdf->setXY(10,40);
$pdf->Cell(40,10,'Item4: $'.$item4);

$pdf->setXY(10,50);
$pdf->Cell(40,10,'Item5: $'.$item5);

$pdf->setXY(10,60);
$pdf->Cell(40,10,'Item6: $'.$item6);

$pdf->setXY(10,70);
$pdf->Cell(40,10,'Total: $'.$total);

$pdf->SetFont('Arial','B',10);

$pdf->setXY(10,15);
$pdf->Cell(40,10,$i1);

$pdf->setXY(10,25);
$pdf->Cell(40,10,$i2);

$pdf->setXY(10,35);
$pdf->Cell(40,10,$i3);

$pdf->setXY(10,45);
$pdf->Cell(40,10,$i4);

$pdf->setXY(10,55);
$pdf->Cell(40,10,$i5);

$pdf->setXY(10,65);
$pdf->Cell(40,10,$i6);

$pdf->Output();
?>
Hmmmm...I cant get it to publish a picture.  In fact...it doesnt even publish the PDF file with this:
class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('my_logo.png',10,8,33);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(80);
    //Title
    $this->Cell(30,10,'Title',1,0,'C');
    //Line break
    $this->Ln(20);
}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}


I have validated the PNG file in the directory...but still no dice.  If I remove the above code everything works...but no picture.  Thoughts?  I just need a header and footer attachement and then the ability to add some static text and this is all done...and then I will leave my wife for you.  :)
I increased the value as you guys are really helping me.  As soon as I get the logos and a cell that I can put text in it...we are done!  
Sorry forgot that you have to move the

require('fpdf.php');

line to the top.  Could you try that?
hmmm...it still didnt work.  Could you get it to work on your end?
I can get the PDF file up...but no picture.  Here is the complete code that I have...and please note that the PNG file is located in the same directory as the process.php file.  Why wouldnt it work?

<?php

require('fpdf.php');

class PDF extends FPDF
{
//Page header
function Header()
{
    //Logo
    $this->Image('header.png',0,0,200);
    //Arial bold 15
    $this->SetFont('Arial','B',15);
    //Move to the right
    $this->Cell(80);
    //Title
    $this->Cell(30,10,'Title',1,0,'C');
    //Line break
    $this->Ln(20);
}

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-15);
    //Arial italic 8
    $this->SetFont('Arial','I',8);
    //Page number
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}


//set the prices here...
$item1p = 1.89;
$item2p = 2.34;
$item3p = 3.56;
$item4p = 4.12;
$item5p = 5.78;
$item6p = 6.12;

$item1 = sprintf("%01.2f", $_POST['item1'] * $item1p);
$item2 = sprintf("%01.2f", $_POST['item2'] * $item2p);
$item3 = sprintf("%01.2f", $_POST['item3'] * $item3p);
$item4 = sprintf("%01.2f", $_POST['item4'] * $item4p);
$item5 = sprintf("%01.2f", $_POST['item5'] * $item5p);
$item6 = sprintf("%01.2f", $_POST['item6'] * $item6p);
$total = sprintf("%01.2f", $item1+$item2+$item3+$item4+$item5+$item6);

//qty text
$i1 = $_POST['item1'].' x $'.$item1p;
$i2 = $_POST['item2'].' x $'.$item2p;
$i3 = $_POST['item3'].' x $'.$item3p;
$i4 = $_POST['item4'].' x $'.$item4p;
$i5 = $_POST['item5'].' x $'.$item5p;
$i6 = $_POST['item6'].' x $'.$item6p;

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$pdf->setXY(10,110);
$pdf->Cell(40,10,'Item1: $'.$item1);

$pdf->setXY(10,120);
$pdf->Cell(40,10,'Item2: $'.$item2);

$pdf->setXY(10,130);
$pdf->Cell(40,10,'Item3: $'.$item3);

$pdf->setXY(10,140);
$pdf->Cell(40,10,'Item4: $'.$item4);

$pdf->setXY(10,150);
$pdf->Cell(40,10,'Item5: $'.$item5);

$pdf->setXY(10,160);
$pdf->Cell(40,10,'Item6: $'.$item6);

$pdf->setXY(10,170);
$pdf->Cell(40,10,'Total: $'.$total);

$pdf->SetFont('Arial','B',10);

$pdf->setXY(10,115);
$pdf->Cell(40,10,$i1);

$pdf->setXY(10,125);
$pdf->Cell(40,10,$i2);

$pdf->setXY(10,135);
$pdf->Cell(40,10,$i3);

$pdf->setXY(10,145);
$pdf->Cell(40,10,$i4);

$pdf->setXY(10,155);
$pdf->Cell(40,10,$i5);

$pdf->setXY(10,165);
$pdf->Cell(40,10,$i6);

$pdf->Output();
?>
ASKER CERTIFIED SOLUTION
Avatar of slyong
slyong

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dude...you ROCK!  Thank you so much!  
Here is a question:  If I wanted to put text in the PDF that is static (doesnt change) how do i do that?  Also, is there a way to do bulet points in the text?

-Tuna
$pdf->setXY(10,165);
$pdf->Cell(40,10,'This is a static text');
will print a static text.

I am not sure about bullet.  But you can draw a rectangle using:

$pdf->Rect(10, 165, 10, 10, DF)
slyong (or anyone),
How would I be able to place a box with text in it that wraps?  Any thoughts?