Page 1 of 1

Generating PDF files using PHP : Error occured!

Posted: Fri Jun 15, 2007 1:46 am
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]

Posted: Fri Jun 15, 2007 2:41 am
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.

Posted: Fri Jun 15, 2007 5:36 am
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.

Posted: Fri Jun 15, 2007 5:55 am
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

Posted: Fri Jun 15, 2007 6:46 am
by volka
dream2rule wrote:Is this possible using PHP?
Don't know, but what does pdf_begin_page() do?

Posted: Fri Jun 15, 2007 7:32 am
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..

Posted: Fri Jun 15, 2007 7:40 am
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?

Posted: Fri Jun 15, 2007 11:47 pm
by dream2rule
yes it does!

Posted: Sat Jun 16, 2007 9:06 am
by volka
meaning: problem solved?

Posted: Sun Jun 17, 2007 11:54 pm
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

Posted: Mon Jun 18, 2007 1:49 am
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(...
...
}
?

Posted: Mon Jun 18, 2007 3:19 am
by dream2rule
yea exactly and i don't know how to go about it.

Thanks and Regards

Posted: Mon Jun 18, 2007 3:31 am
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?

Posted: Mon Jun 18, 2007 11:14 pm
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