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 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

In case no-one has welcome you to the forum: welcome.

Down to business:
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:3. Do not make multiple, identical posts. This is viewed as spam and will be deleted.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://de2.php.net/manual/en/ref.pdf.php wrote: In PHP 4, first a PDF resource has to be retrieved with a function call like

$p = PDF_new().

This PDF resource is used as the first parameter in all further function calls, such as in

PDF_begin_document($p, "", "").

In PHP 5 however, a PDFlib object is created with

$p = new PDFlib().

This object offers all PDFlib API functions as methods, e.g. as with

$p->begin_document("", "").
You have mixed the two styles.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

oh! thanks a lot volka.

yeah now my code works fine but is there any way to create a PDF file with multiple pages.

The above mentioned code generates a pdf file with only a single page!

I would like to create a pdf file with multiple pages with each page having a different template altogether.
and each template should have a minimum of 20 to 30 textboxes!

Is this possible using PHP?

For more detailed information, here's the requirement:

I need a php application to create pdf files. The following features must be implemented:
The pdf documents created can be multiple pages long.
Each page can be of a different templated layout
The text selected for each page can contain upto 30 dynamic text variables
20 layout templates must be created with text areas in different locations
The system must be so flexible that for each document, it should select both the correct template for a page and the data for each page and then create the entire document.

Can anybody Help??

Regards
Dream2rule
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

dream2rule wrote:Is this possible using PHP?
Don't know, but what does pdf_begin_page() do?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

volka wrote:
dream2rule wrote:Is this possible using PHP?
Don't know, but what does pdf_begin_page() do?
well.. pdf_begin_page() starts a page in your pdf document.

For Example:

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

In the above code, the pdf file is created, opened and a page is added to it at the beginning of the file.
For a A4 sized page use the dimensions 595x842, for a letter head use 612x792 and so on..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

dream2rule wrote:I would like to create a pdf file with multiple pages with each page having a different template altogether.
dream2rule wrote:well.. pdf_begin_page() starts a page in your pdf document.
and does a second call to pdf_begin_page() append a new page to the current document?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

yes it does!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

meaning: problem solved?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

No.. the problem still persists..

I need an automated script that would generate multiple pages.

As of now.. i am adding each page into the pdf document because i know the total number of pages.

Help anyone??

Regards
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Do you mean ... a loop? Something like

Code: Select all

while ( $x < $ y) {
  pdf_begin_page($pdf_file, 595, 842);
  PDF_setfont(...
  PDF_set_text_pos(...
  PDF_show(...
...
}
?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

yea exactly and i don't know how to go about it.

Thanks and Regards
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

dream2rule wrote:The system must be so flexible that for each document, it should select both the correct template for a page and the data for each page and then create the entire document.
Select from where? How?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

select from the database.

Hey Volka.. my problem has been resolved..
I was successfully able to generate multipaged PDF documents.

Thanks and Regards
Post Reply