Hello everyone,
been growing grey hairs(more than my usual few) over the last couple of days trying to output a PDF page from my php code.The code is a simple one from an online example and getting it to run as been one big headache.It's as follows:

<?php
$user = $HTTP_POST_VARS["userName"];
$pdf = pdf_new();
pdf_open_file($pdf, "obiboy.pdf");

pdf_set_info($pdf, "Author", "J.F.Kennedy");
pdf_set_info($pdf, "Title", "Creating a pdf");
pdf_set_info($pdf, "Creator", "J.F.Kennedy");
pdf_set_info($pdf, "Subject", "Creating a pdf");

pdf_begin_page($pdf, 595, 842);

$arial = pdf_findfont($pdf, "Arial", "host", 1);
pdf_setfont($pdf, $arial, 14);

pdf_show_xy($pdf, "<Type your info here>",50, 400);

$gif_image = pdf_open_gif($pdf, "baseball.gif");

pdf_place_image($pdf, $gif_image, 200, 300, 1.0);

pdf_close_image($pdf, $gif_image);

pdf_end_page($pdf);
pdf_close($pdf);

//echo "<A HREF=\"C:\obiboy.pdf\" TARGET=\"_blank\">Open pdf in a new window $user</A>"
?>

Its meant to carry details from a sign in page as reflected by the declared globals on top.Even though I have enabled usage of PDF extension in my php.ini, the code dosent run.

The error it returns is as follows:
Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope in c:\inetpub\wwwroot\pdf_house\pdf_file.php on line 6

so can someone please look it over and tell me where I am screwing it up?
Thanks all.

    if you comment out the line
    pdf_open_file($pdf, "obiboy.pdf");
    do you still get the error?

      The error still persists inspite of me commenting out line 4:
      pdf_open_file($pdf, "obiboy.pdf");

        the reason i asked that is because i saw a user comment in the manual.
        "If you get "Fatal error: PDFlib error: function 'PDF_set_info' must not be called in 'object' scope in script.php on line xxx" when using pdf_open_file with a filename, make sure your webserver has write permissions to the directory your are trying to save your PDF file to."

        That was the only thing I could think of for fixing the error.

        make sure the pdf has proper write permissions, either given by iis or by whatever program you used to create the pdf.

          Thanks dru, I beleiev you r right because on placing an alternative die function after the pdf_open_file(); function call, the string in the die function was outputted to browser meaning the permissions are not correct.Could you please advise me on how to change permissions in IIS?Every other thing I did didnt work including right clicking on the folder and going to properties to give 'everyone' absolute rights.
          Thanks once again.

            first, if the file is in the root of c, try opening it like this:
            pdf_open_file($pdf, "C:\obiboy.pdf");
            because sometimes windows can be strange with paths.
            and also, was the original pdf created with adobe or another professional pdf document generator? it is possible the files are locked and uneditable.

              I am trying to create the PDF file sans adobe or any other proprietary software.I just have d reader so that I can view the work done

                ok, i understand.
                see if this sample code works...

                <?php
                
                $pdf = pdf_new();
                
                pdf_open_file($pdf);
                pdf_begin_page($pdf, 595, 842);
                pdf_set_font($pdf, "Times-Roman", 30, "host");
                pdf_set_value($pdf, "textrendering", 1);
                pdf_show_xy($pdf, "A PDF document created in memory!", 50, 750);
                pdf_end_page($pdf);
                pdf_close($pdf);
                
                $data = pdf_get_buffer($pdf);
                
                $fp = fopen("test.pdf", "w+");
                if(!$fp) {
                    echo "Error opening file!";
                    exit;
                }
                
                fwrite($fp, $data);
                fclose($fp);
                echo "document created.";
                ?>
                

                see if you get that document created fine.

                  Bingo!!
                  Ijust threw open the permissions(temporarily:I am only on an intranet anyway) of my inetpub folder and the file was written in a jiffy.What procedural difference was there between the sleek code you sent and the one I was trying to get to run.
                  Hwy, before I forget, thanks a bunchπŸ˜ƒ

                    im not sure how the function pdf_open_file opens the file you specify. maybe it expected it to exist already and that is why there were problems.
                    basically the code i gave you just manually creates a file, and if it exists, it clears the contents first, and then outputs the pdf data into the file.

                      You are absolutely right, drew.Because the moment the following:

                      $fp = fopen("obiboy.pdf", "w+");

                      taking a cue from your code, into my sample, I noticed a change with PDFLib now sending me an error demanding that I must buffer the handle I used if I wanted to see jack.
                      On addidng:

                      $data = pdf_get_buffer($pdf);

                      again taking a cue from your code, a pdf file by the name specified was actually created except that it was corrupt and acrobat couldnt open it then when I closed the file opening process by writing the buffer to the browser:

                      fwrite($fp, $data);
                      fclose($fp);

                      it turned out right.
                      Thanks a bunch again.Hope your tutorials would be useful for anyone wanting to do some of that stuff allover again, while setting me off on what I need to do.πŸ˜ƒ πŸ˜ƒ

                        Write a Reply...