1

I was trying to view a PDF from my a MySQL database but it gets stuck on loading.

Here's my code:

<?php
    header("Content-type: application/pdf");
    $con = mysqli_connect("localhost","root","username","password");

    //check if errors occur in connection to database
    if (mysqli_connect_errno())
    {
        //return the error
        echo mysqli_connect_error();
    }
    $filename = $row['name'];
    $query = "SELECT content from images WHERE id = 7"; 
    $result = mysqli_query($query,$con)or die(mysqli_error()); 
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);  

    echo $row['content'];
?>
Marco
  • 56,740
  • 14
  • 129
  • 152

1 Answers1

0

You can use dompdf or TCP pdf library for better and extended functionality.

or try to use ob_clean() method in starting of the line.

or remove first header("Content-type: application/pdf"); and check whether query is working or not.

  • where to put ob_clean() method? – tonystarkPH Apr 21 '15 at 09:31
  • none of these methods works. btw thanks for answering – tonystarkPH Apr 21 '15 at 09:33
  • can you show me the url, so i can see what's happening with client side(browser)? –  Apr 21 '15 at 10:22
  • I am pretty sure if you use DOM PDF library your problem will resolve. –  Apr 21 '15 at 10:23
  • how to use the DOM PDF library? can you provide example or tutorial? – tonystarkPH Apr 21 '15 at 10:37
  • ob_clean (); $dompdf = new DOMPDF(); $dompdf->load_html("$cm_100_html"); // $paper_size = array(50,0,700,885); $dompdf->set_paper($paper_size); $dompdf->render(); $output = $dompdf->output(); $idss="CM-100_".$internal_number; $uploads=wp_upload_dir(); $uploaddir=$uploads['basedir']."/pdf_store/"; $file_to_save = "$uploaddir".$idss.'.pdf'; file_put_contents($file_to_save, $output); –  Apr 21 '15 at 13:11
  • $cm_100_html is the html container. In your example do this $cm_100_html.=$row['content']; –  Apr 21 '15 at 13:12
  • still if you have problem let me know any help( braut@avika.in) –  Apr 21 '15 at 13:13
  • can you inject the dompdf to my example code? so i can try it now. Im confused – tonystarkPH Apr 21 '15 at 13:24
  • Add this in your script –  Apr 22 '15 at 06:23
  • `ob_clean (); $dompdf = new DOMPDF(); $dompdf->load_html($row['content'];); $paper_size = array(50,0,700,885); $dompdf->set_paper($paper_size); $dompdf->render(); $output = $dompdf->output();` –  Apr 22 '15 at 06:46
  • Just test it and let me know whether it works or not –  Apr 22 '15 at 06:49