homepage Welcome to WebmasterWorld Guest from 54.237.235.12
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help generating a PDF using FPDF
Jamier101



 
Msg#: 4500426 posted 11:26 pm on Sep 26, 2012 (gmt 0)

I have a script that I'm trying to use to produce a PDF, I can generate the file but unfortunately it appears blank everytime. I'm using FPDF to generate the document but think I'm doing something wrong... any ideas?
Request Form
<form action="create_pdf_shipping_label.php" method="POST">
<input name="requestid" type="text"/>
<input name="submit" type="submit" value="Submit" />
</form>


Create_pdf_shipping_label.php

<?php

error_reporting(E_ALL|E_STRICT);

require_once("connections/connection.php"); // Connection to the server
require_once('fpdf/fpdf.php'); //FPDF classes

$tbl_name="sample_requests"; // Table name

$requestid = ($_POST['requestid']);

$query = "SELECT * FROM $tbl_name WHERE request = $requestid ";
$result = mysql_query($query) or die('Error, query failed');

while($rows = mysql_fetch_array($result));
{
$firstname = $rows['firstname'];

$pdf=new FPDF();

$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','B',14);
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);

$pdf->Text(40,10,$firstname);

$pdf->Output('mailing_labels.pdf','D');
}
?>

 

Jamier101



 
Msg#: 4500426 posted 6:45 pm on Sep 28, 2012 (gmt 0)

I've been looking at the problem further, it appears that the issue is something to do with the $pdf section and actually writing to the document.

This works:
$pdf->Write(5,'Hello World!');

This doesn't:
$pdf->Write(5, $firstname);

Any idea's?

Jamier101



 
Msg#: 4500426 posted 6:46 pm on Sep 28, 2012 (gmt 0)

I'm sorry, I typed out my command incorrectly, I meant to say that this doesn't work:
$pdf->Write(5,$result['firstname']);
wesmaster

5+ Year Member



 
Msg#: 4500426 posted 7:12 pm on Sep 28, 2012 (gmt 0)

$result['firstname'] is not a valid way to get to the firstname field. Should be $rows['firstname'] given the code you have.

I have FPDF implemented on one of my sites. If you continue to have problems I can probably help. But, it appears that you're just getting to your data incorrectly.

tec4



 
Msg#: 4500426 posted 7:14 pm on Sep 28, 2012 (gmt 0)

Was that a mis-type?

If you're looping through your results it should be $rows['firstname'];

But in your first example you are writing $rows['firstname'] into the $firstname variable - using the $firstname var isn't working? What is the output of $rows['firstname'] on a blank canvas?

Jamier101



 
Msg#: 4500426 posted 7:21 pm on Sep 28, 2012 (gmt 0)

Yes, I meant to say that I was putting
$pdf->Write(5,$result['firstname']); and not $pdf->Write(5, $firstname);

I've amended the code as follows:
<?php

error_reporting(E_ALL|E_STRICT);

require_once("connections/connection.php"); // Connection to the server
require_once('fpdf/fpdf.php'); //FPDF classes

$tbl_name="sample_requests"; // Table name

$query = "SELECT * FROM $tbl_name WHERE request = 2 ";
$result = mysql_query($query) or die('Error, query failed');

while($rows = mysql_fetch_array($result));
{
$pdf=new FPDF();

$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial','B',14);
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);

$pdf->Write(5,$rows['firstname']);

$pdf->Output('mailing_labels.pdf','D');
}
?>

However, I simply get a PDF generated onto a blank canvas.

wesmaster

5+ Year Member



 
Msg#: 4500426 posted 7:29 pm on Sep 28, 2012 (gmt 0)

Your exact PDF code works fine on my server if I put "test" in place of $rows['firstname'] . So either $rows['firstname'] is empty or your FPDF is somehow failing.

tec4



 
Msg#: 4500426 posted 7:32 pm on Sep 28, 2012 (gmt 0)

Is the text not being written to the PDF either? So just a white pdf file?

When I use this, I generally grab a pdf template, write on top of it, store it locally then grab and email it, for example.

A simple example of my use (not sure if your use would be similar, but using a template might be a good option - depending)

// initiate FPDI
$pdf = new FPDI();

// add a page
$pdf->AddPage();

// set the sourcefile
$pdf->setSourceFile($base . '/email/form.pdf');

// import page 1
$template = $pdf->importPage(1);

// use the imported page and place it at point
$pdf->useTemplate($template, -5, 0, 220, 300);

$pdf->SetXY(49, 46);
$pdf->Write(0, $name);

$pdf->SetXY(38, 64);
$pdf->Write(0, $other_name);

$pdf_file= $base . '/email/results/'.time().'.pdf';
$pdf->Output($pdf_file, 'F');

Jamier101



 
Msg#: 4500426 posted 7:35 pm on Sep 28, 2012 (gmt 0)

I've found that if I replace $rows['firstname'] with 'Test' I also get a PDF with the words firstname at the top but nothing when I try and pass something out of my database. I think its got to be something to do with the syntax that I'm using but I was sure that $pdf->Write(); was correct.

wesmaster

5+ Year Member



 
Msg#: 4500426 posted 7:40 pm on Sep 28, 2012 (gmt 0)

Possible the fact that you're looping outside of the PDF code is causing some unexpected results.

Jamier101



 
Msg#: 4500426 posted 7:43 pm on Sep 28, 2012 (gmt 0)

Hummm... how do you think would be the best way to put it then?

wesmaster

5+ Year Member



 
Msg#: 4500426 posted 7:47 pm on Sep 28, 2012 (gmt 0)

You're outputting the file for each db record, is that what you want? Does this even work? Wouldn't expect this to work, but if it does, and that's what you're trying to do then, OK. I don't know what you're trying to do, as obviously outputting a PDF with just a first name is not your objective.

Jamier101



 
Msg#: 4500426 posted 7:54 pm on Sep 28, 2012 (gmt 0)

Basically, I've written a PHP that displays all of the pending sample_requests at a company. Within the information that is requested is the name and address of the person who we'll be sending the sample too. I'm looking to export the name and address from the database and produce a PDF that I can then print as a shipping label. I thought I'd start by getting the person's first name printing on the PDF and then build things up from there.

Jamier101



 
Msg#: 4500426 posted 7:59 pm on Sep 28, 2012 (gmt 0)

I've never had to output a PDF before and I thought I was working along the right lines but I'm starting to think that I'm miles off!

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4500426 posted 2:39 am on Sep 29, 2012 (gmt 0)

I'm looking to export the name and address from the database and produce a PDF that I can then print as a shipping label.

Ah. I wondered about that at the beginning of the thread. If you're sending it to someone else then I see why it has to be a PDF-- but if you're printing it yourself, does format matter? That is, don't spend time tearing out your hair if you can just use the same data to generate an appropriately formatted html page-- which I assume you can do standing on your head-- and print that instead.

wesmaster

5+ Year Member



 
Msg#: 4500426 posted 9:38 pm on Sep 29, 2012 (gmt 0)

Thought I was working along the right lines but I'm starting to think that I'm miles off!


You're doing it fine. You just need to make a PDF test page first, don't worry about the database. THEN add the database connectivity.

If you send me your email I can send you my FPDF code for creating an invoice so that you can check it out. I use the Cell command instead of Write so that I can control the text better. But it might be useful to see the code.

Jamier101



 
Msg#: 4500426 posted 8:34 am on Sep 30, 2012 (gmt 0)

Thank you wesmaster, I can print something like "Hello World" to a PDF but the database bit appears to be the difficult bit.

I've sent you a message with my email address on it :-)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved