0

I have a php file of which I want to generate a PDF using dompdf, I have tried it with the code below but I can't get the html of the file from php, any idea on how to get the html elements from php file?

    <?php
    require_once("dompdf/dompdf_config.inc.php");
    ob_start();        
    ?>
    <html>
     <body>
     <?php
         //Code for a colored table
     ?>
     <form method=post action=#><input type=submit name=submit id=submit value="Create PDF"></form>
     </body>
    <html>

    <?php
        if((isset($_POST['submit'])))
        {
            $html = ob_get_contents(); 
                ob_end_flush();
            $dompdf = new DOMPDF();
            $dompdf->load_html($html);
            $dompdf->render();
            $dompdf->stream("Time Table.pdf");
        }
    ?>

EDIT: It now gives an error:

Fatal Error: Maximum execution time of 30 seconds exceeded in .../

2
  • I don't see anything out of sorts here. What do you get when you submit the form? Do you get any errors? Commented Jul 30, 2013 at 1:20
  • Fatal Error: Maximum execution time 30 seconds exceeded Commented Jul 30, 2013 at 2:31

1 Answer 1

1

Not sure how much this matters but your html closing tag is actually just another open html tag. Try closing it and changing your code to the following :

 <?php
    require_once("dompdf/dompdf_config.inc.php");
    ob_start();        
    ?>
    <html>
     <body>
     <?php
         //Code for a colored table
     ?>
     <form method=post action=#><input type=submit name=submit id=submit value="Create PDF"></form>
     </body>
    </html>

    <?php
        if((isset($_POST['submit'])))
        {
            $html = ob_get_contents(); 
                ob_end_flush();
            $dompdf = new DOMPDF();
            $dompdf->load_html($html);
            $dompdf->render();
            $dompdf->stream("Time Table.pdf");
        }
    ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry that was a typo, my code generates pdf but is a corrupted pdf file! also it give fatal error: Maximum execution time exceeds 30s
@TKA If the PDF is corrupted some PHP errors/notices may be leaking into the output stream. Save the PDF and open it in a text editor to see if that's the case. The errors will be at the top of the file.
@BrianS but I do get the result when I echo the $html
@TKA as I said, a corrupted PDF is often because some non-PDF content is injected into the output stream. That doesn't mean the HTML is invalid, or even that the PDF is invalid. It just means there is extra, non-PDF content interfering with your PDF reader's parsing of the document. You could just comment out $dompdf->stream() and see what is returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.