Create PDF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Create PDF

Post by icesolid »

I tried to use this code provided on the php.net web site and I get this error:

Fatal error: Call to undefined function: pdf_new() in /home/caposcom/public_html/savecase.php on line 2

Code: Select all

<?php
$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, "Hello world!");
PDF_continue_text($p, "(says PHP)");
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);
?>
Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your host doesn't have the pdf extension loaded.
Last edited by feyd on Sun Feb 19, 2006 5:06 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

http://php.net/pdf
Requirements

PDFlib is available for download at http://www.pdflib.com/products/pdflib/index.html, but requires that you purchase a license for commercial use.
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post by RobertPaul »

Did you install the pdflib extension?


[edit]Foiled once again! :oops: [/edit]
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

My Host Sucks

Post by icesolid »

Nah I just talked to my host, they dont have it installed, I also have another problem they don't have the exif lib installed.

I'm moving to a liquidweb.com dedicated server tomorrow so I will be back for more help I'm sure.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

http://www.fpdf.org/ .. in my opinion, the best PDF library for PHP. It doesn't require any extensions.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

That's Pretty Handy

Post by icesolid »

That a pretty nice and easy to use library.

Requires a lot of work to get the final PDF document to look how you want it tho.

Explain to me, how is using that not illegal, aren't you suppose to pay to create PDF files?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

aren't you suppose to pay to create PDF files?
In a word, no. You only have to pay if you are using the pdflib library for commercial use.. and even then all you have to do is buy a license..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: That's Pretty Handy

Post by onion2k »

icesolid wrote:Requires a lot of work to get the final PDF document to look how you want it tho.
No more than any other output.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Check in code snippets....i posted a quick intro to creating PDFs
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Question

Post by icesolid »

I am trying to use this library http://www.fpdf.org/ to create PDFs.

I want to print out all of the columns in one record from my database into a PDF.

I tried using their example script here http://www.fpdf.org/en/script/script10.php , but it is for printing out all of the records in the database.

I just want to print out all of the columns in one row.

I want to result to look like this

Code: Select all

<table>
  <tr>
    <td colspan="2">TICKET INFORMATION FOR CONTROL <?php echo $row["control_number"]; ?></td>
  </tr>
  <tr>
    <td>column title 1</td>
    <td><?php echo $row["column 1"]; ?></td>
  </tr>
  <tr>
    <td>column title 2</td>
    <td><?php echo $row["column 2"]; ?></td>
  </tr>
  <tr>
    <td>column title 3</td>
    <td><?php echo $row["column 3"]; ?></td>
  </tr>
  etc...
</table>
Anyone?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

$pdf = new PDF('P','mm','A4');
$pdf->SetTitle('My First PDF');
$pdf->SetAuthor('Chris Neale');
	
$pdf->AddPage();

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

while ($record = mysql_fetch_object($result)) {				
	$pdf->Cell(100,7,"TICKET INFORMATION FOR CONTROL ".$record->control_number,'BT',0,'L',0);
	$pdf->Cell(40,7,$record->column1,'BT',0,'L',0);
	$pdf->Cell(40,7,$record->column2,'BT',0,'L',0);
	$pdf->Cell(30,7,$record->column3,'BT',0,'L',0);
	$pdf->Ln();
}
Basically.. $pdf->Cell is the equivalent of <td>, and $pdf->Ln() is the equivalent of <tr> .. if you need more complex layouts remember that you can move around the page with $pdf->setXY() .. but it gets complicated if you do.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

I get an error using the code below on line 13 (bold tags around it):

Fatal error: Cannot instantiate non-existent class: pdf in /home/caposcom/public_html/savecase.php on line 13

Code: Select all

<?php
define('FPDF_FONTPATH','font/');
require('fpdf153/fpdf.php');

//Connect to your database
include("connect.php");

[b]$pdf = new PDF('P','mm','A4'); [/b]
$pdf->SetTitle('My First PDF'); 
$pdf->SetAuthor('Chris Neale'); 
     
$pdf->AddPage(); 

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

$result = mysql_query("SELECT * FROM cases WHERE id='" . $_GET["id"] . "'");

while ($record = mysql_fetch_object($result)) {                 
    $pdf->Cell(100,7,"TICKET INFORMATION FOR CONTROL ".$record->control_number,'BT',0,'L',0); 
    $pdf->Cell(40,7,$record->column1,'BT',0,'L',0); 
    $pdf->Cell(40,7,$record->column2,'BT',0,'L',0); 
    $pdf->Cell(30,7,$record->column3,'BT',0,'L',0); 
    $pdf->Ln(); 
}

$pdf->Output();
?>
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

Include your PDF class, it's non-existant in your script. May want to check letter case as well.
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Post by icesolid »

This is the class right here isent it?

require('fpdf153/fpdf.php');

I have it included in my code.
Post Reply