Welcome to WebmasterWorld Guest from 184.72.177.182

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Help generating a PDF using FPDF

     
11:26 pm on Sep 26, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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');
}
?>
6:45 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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?
6:46 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 0


I'm sorry, I typed out my command incorrectly, I meant to say that this doesn't work:
$pdf->Write(5,$result['firstname']);
7:12 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 9, 2009
posts: 59
votes: 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.
7:14 pm on Sept 28, 2012 (gmt 0)

Junior Member

joined:June 16, 2011
posts: 79
votes: 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?
7:21 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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.
7:29 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 9, 2009
posts: 59
votes: 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.
7:32 pm on Sept 28, 2012 (gmt 0)

Junior Member

joined:June 16, 2011
posts: 79
votes: 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');
7:35 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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.
7:40 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 9, 2009
posts: 59
votes: 0


Possible the fact that you're looping outside of the PDF code is causing some unexpected results.
7:43 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 0


Hummm... how do you think would be the best way to put it then?
7:47 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 9, 2009
posts: 59
votes: 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.
7:54 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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.
7:59 pm on Sept 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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!
2:39 am on Sept 29, 2012 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:12700
votes: 244


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.
9:38 pm on Sept 29, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 9, 2009
posts: 59
votes: 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.
8:34 am on Sept 30, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 11, 2010
posts: 131
votes: 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 :-)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members