Thread: Php - Fpdf

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    27
    Rep Power
    0

    Unhappy Php - Fpdf


    Hi there

    I am trying to use FPDF to save an HTML form to PDF, but am receiving this error:
    FPDF error: Some data has already been output, can't send PDF file

    Any advice would be greatly appreciated Thank you

    This is the code form my PHP :

    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
       
    $to 'myemail@yahoo.com' ;     //put your email address on which you want to receive the information
       
    $subject 'Conference Quote Request';   //set the subject of email.
       
    $headers  'MIME-Version: 1.0' "\r\n";
       
    $headers .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
       
    $message "<table><tr><td>Enquiry Date</td><td>".$_POST['dateToday']."</td></tr>
                   <tr><td>Conference Name</td><td>"
    .$_POST['conferenceName']."</td></tr>
                   <tr><td>Company Name</td><td>"
    .$_POST['companyName']."</td></tr>
                   <tr><td>First Name</td><td>"
    .$_POST['firstName']."</td></tr>
                   <tr><td>Last Name</td><td>"
    .$_POST['lastName']."</td></tr>
                   <tr><td>Tel No</td><td>"
    .$_POST['telNo']."</td></tr>
                   <tr><td>Cell No</td><td>"
    .$_POST['cellNo']."</td></tr>
                   <tr><td>E-Mail</td><td>"
    .$_POST['email']."</td></tr>
                   <tr><td>Special Requests</td><td>"
    .$_POST['comment']."</td></tr>
                   </tr></table>" 
    ;
                   if(
    mail($to$subject$message$headers)){
           echo 
    "Message sent successfully...<br />";
           echo 
    $message;
        }else{
           echo 
    'Error! try again';
        }
    }
    require(
    'fpdf.php');
    $pdf = new FPDF( );
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(0,10,'PHP - The Good Parts!');
    $pdf->Output();
    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,990
    Rep Power
    375
    i am probably wrong but can you try to comment out the following line and do a test
    echo "Message sent successfully...<br />";
    echo $message;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    27
    Rep Power
    0
    Thank you for your reply, but unfortunately it isn't working, even after commenting out those lines

    Thank you
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Well what else has been output? The error message says nothing can be output before you generate the PDF (which makes sense since it's header-dependent). Any echoes, any prints, any error messages, any warnings, any whitespace before the <?php or after the ?> tags, any HTML, any manual header controls, any cookies being sent, anything which would cause the server to return data to the user cannot happen before you generate.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    27
    Rep Power
    0
    Thank you
    I can see now that that is the problem. I moved the fpdf code to before the mail and echo requests, and it starts to load. but then it says "failed to load pdf document" on the screen?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    875
    Rep Power
    275
    This is directly from the fpdf 1st tutorial [bottom of page]
    Caution: in case when the PDF is sent to the browser, nothing else must be output by the script, neither before nor after (no HTML, not even a space or a carriage return). If you send something before, you will get the error message: "Some data has already been output, can't send PDF file". If you send something after, the document might not display.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    europe
    Posts
    287
    Rep Power
    14
    If you want to display an html message and the pdf in the same page you have to save the pdf as a file and put an iframe into the html page with
    <h3>We sent your email!</h3>
    <iframe src="/path/to/my.pdf"></iframe>
    Last edited by rayz; May 6th, 2013 at 01:44 PM. Reason: additional idea
    http://www.digikix.de
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    27
    Rep Power
    0
    Thank you for your help. I have decided it may be better for the "submit" button to only email the content, and then "post" the information to another page.

    I have added a print button to this page for the user to print the submitted contents.

    Is it possible to have a "print to pdf" button on this page to print the data which is displayed on the page?

    Thank you
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    europe
    Posts
    287
    Rep Power
    14
    You can create a PDF from the input data and display it but that will not be automatically printed. You can print the displayed page via javascript. Make a button with onclick="window.print()"

    Of course this will only work if javascripot in the user's browser is not switched off. But most users have it enabled.
    http://www.digikix.de

IMN logo majestic logo threadwatch logo seochat tools logo