Scott Fiander
Scott Fiander

Reputation: 135

TCPDF - Getting data from FORM on HTML page to output to PDF

I've just recently started using TCPDF and I've ran into a road block..

I was asked to make a form on our intranet where employees could fill it out (all text fields and one radio button) hit the submit button and it would then use TCPDF to output that filled form to a PDF. I think I know a way of doing this but it would be a case of really bad programming... not to mention it would take a bunch of time.

I saw a post on here before about a similar issue (Creating PDF using TCPDF and a specific div as the content) , but it wasn't quite what I was looking for.

If anyone can suggest anything at all it would really be appreciated!!

Thanks for your time :)

<----EDIT---->

Just made a small break through, nothing big..

Using the post I mentioned above, I was able to come up with this simple thing:

<?php
require_once('tcpdf_include.php');
if(isset($_POST['1'])){
$pdf = new TCPDF();
$pdf->AddPage('P');
for($i = 0; $i<3; $i++){
$html = $_POST($i);
$pdf->writeHTML($html);
}
$pdf->Output();
}
?>

<form method="POST">
Person Travelling: <input name='1' type='text' value=''>
<br>
Request Date: <input name='2' type='text' value=''>
<br>
<br>
<input type='submit' value='Submit'>
</form>

Using this I get the output I kinda want on a pdf... only problem now is the formatting issue, when the form data is sent to the pdf I can only read the values of what was put in the text fields and not the value plus the description (ie. Person Travelling).

So, yeah, any help on this new issue would be great haha

<---------------------------------------EDIT-------------------------------------------->

Alright, so from my previous questions I've been able to figure most of everything out. The description and inputted text is now displaying on the pdf when I hit the submit button. All I needed to do was:

<input name='0' type='hidden' value="Person Travelling: ">

And then it would spit that out on the pdf, simple enough.

The one and only thing I'm having trouble with now is the way the pdf shows all of this...

I'm not quite sure how to put the outputted data exactly where I need it, right now it prints everything on the left margin one piece of data after another. I have a pdf of how they want it to look and I also know how to place headers and change the default header images and text, just not the rest of the page.

So again, if anyone can give me some advice on this it would be REALLY appreciated!!!

<-----------------------------------------EDIT-------------------------------------------->

Alrighty then, I've finally figured it out!! I just used the multicell function of tcpdf and after a few hours of playing around with it I finally was able to put everything where it needed to be so far. It's just the matter of completing the project now by adding more multicells and fixing up the code. Here's what I did (tentatively):

$pdf->SetXY(15,40);
for($i=0; $i<4; $i++){
$html = $_POST[$i];
$pdf->MultiCell(40,5,$html,1,'L',0,0,'','',true,0,false,true,40,'T');
}
$pdf->SetXY(15,60);
for($i=4; $i<8; $i++){
$html = $_POST[$i];
$pdf->MultiCell(40,5,$html,1,'L',0,0,'','',true,0,false,true,40,'T');
}

And so on...

Figured I would post what I had to do to help anyone else experiencing the same issue.

Cheers Guys and Gals!

Upvotes: 0

Views: 7038

Answers (1)

Scott Fiander
Scott Fiander

Reputation: 135

The answer to my question is in the edits I made above, not much of the original code I posted had changed.

Basically, for this project I had to do, in order to get the data from the form I just named all my input fields which would populate a pdf file on click of the submit button.

To get the formatting correct, I used the MultiCell() function in TCPDF. It did take a little while to fully understand what each argument was in the function but after a few hours it was easy enough then.

So again, the answer to my original question is posted in the edits I made above!!

Upvotes: 2

Related Questions