Generating PDF files using PHP : Error occured!

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

Post Reply
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Generating PDF files using PHP : Error occured!

Post by dream2rule »

Hello All,

I am a newbie and just learning PHP. I have just started to code wherein i need to generate a pdf file using PHP.

But i am experiencing too many Fatal Errors!

Error Logs:
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 107489525 bytes) in D:\Xampp\xampp\htdocs\sample\pdf_file.php on line 67

Fatal error: Uncaught exception 'PDFlibException' with message 'Function must not be called in 'object' scope' in D:\Xampp\xampp\htdocs\sample\pdf_file.php:17 Stack trace: #0 D:\Xampp\xampp\htdocs\sample\pdf_file.php(17): pdf_begin_page() #1 {main} thrown in D:\Xampp\xampp\htdocs\sample\pdf_file.php on line 17

Here's the code!

Code: Select all

<?php
//retrieving form values
$user_name = $_POST['user_name'];

//creating a blank pdf file
$pdf_file = pdf_new();

pdf_open_file($pdf_file,"D:\XYZ.pdf");

//setting properties for the creaed pdf file
pdf_set_info($pdf_file,"Author","XYZ");
pdf_set_info($pdf_file,"Title","Creating pdf files on the fly");
pdf_set_info($pdf_file,"Creator","XYZ");
pdf_set_info($pdf_file,"Subject","Creating pdf files on the fly!");

//pdf manipulation; A4 size:595x842, Letter:612x792, Legal:612x1008
pdf_begin_page($pdf_file,595,842);

//setting a font 
$font = pdf_findfont($pdf_file, "Helvetica-Bold", "winansi",0);
pdf_setfont($pdf_file, $font, 12);

//displaying text in a pdf file
pdf_show_xy($pdf_file, "Welcome 2 the world of PHP!",100, 100);

/*
//including images and logo's in a pdf doc
$jpeg_image = pdf_open_jpeg($pdf_file, "Sunset.jpeg"); //pdf_gif($pdf_file,"image_name.gif"); can be used for gif images

//To put the object onto the pdf file you use the pdf_place_image function with the parameter being pdf file, image file, x-value, //y-value and scale repectively.
pdf_place_image($pdf_file, $jpeg_image, 200, 300, 1.0);

//One must close the image to put it out of use.
pdf_close_image($pdf_file, $jpeg_image);
*/

//Lets end the pdf manipulation process by using the pdf_end_page and the pdf_close functions.
pdf_end_page($pdf_file);
pdf_close($pdf_file);

//Outputting the PDF file (Getting the buffer)
$buffer = pdf_get_buffer($pdf_file); 

//Outputting the Buffer to the browser
header("Content-type: application/pdf");
header("Content-Length: ".strlen($buffer));
header("Content-Disposition: inline; filename=XYZ.pdf");

echo $buffer;

//Cleaning up
pdf_delete($pdf_file);

//Now to view your pdf file, simply create a link to open the pdf in a new window.
//echo "<a href=\"D:\XYZ.pdf\" TARGET=\"_blank\">Open pdf in a new window $user_name</a>";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
I need some help in debugging the errors and generating an error free PDF file!

Regards...
Dream2rule
Post Reply