php - DOMPDF error http error 500 -


i want print pdf file table in db whenever client ask have hard copy of records. unfortunately when print record or convert pdf format keeps saying:

localhost unable handle request. http error 500

even when rid of table in try put html element on it.

here code:

<?php //index.php //include autoloader  require_once '../autoload.inc.php';  // reference dompdf namespace  use dompdf\dompdf;  //initialize dompdf class  $document = new dompdf();  $html = '     <style> table {     font-family: arial, sans-serif;     border-collapse: collapse;     width: 100%; }  td, th {     border: 1px solid #dddddd;     text-align: left;     padding: 8px; }  tr:nth-child(even) {     background-color: #dddddd; } </style> <table>   <tr>     <th>company</th>     <th>contact</th>     <th>country</th>   </tr>   <tr>     <td>alfreds futterkiste</td>     <td>maria anders</td>     <td>germany</td>   </tr>   <tr>     <td>centro comercial moctezuma</td>     <td>francisco chang</td>     <td>mexico</td>   </tr>   <tr>     <td>ernst handel</td>     <td>roland mendel</td>     <td>austria</td>   </tr>   <tr>     <td>island trading</td>     <td>helen bennett</td>     <td>uk</td>   </tr>   <tr>     <td>laughing bacchus winecellars</td>     <td>yoshi tannamuri</td>     <td>canada</td>   </tr>   <tr>     <td>magazzini alimentari riuniti</td>     <td>giovanni rovelli</td>     <td>italy</td>   </tr> </table> ';  //$document->loadhtml($html); $page = file_get_contents("cat.html");  //$document->loadhtml($page);  $connect = mysqli_connect("localhost", "root", "", "pdf");  $query = "     select *     preschoolers  "; $result = mysqli_query($connect, $query);  $output = "     <style> table {     font-family: arial, sans-serif;     border-collapse: collapse;     width: 100%; }  td, th {     border: 1px solid #dddddd;     text-align: left;     padding: 8px; }  tr:nth-child(even) {     background-color: #dddddd; } </style> <table>     <tr>         <th>id</th>         <th>name</th>         <th>interp</th>     </tr> ";  while($row = mysqli_fetch_array($result)) {     $output .= '         <tr>             <td>'.$row["p_id"].'</td>             <td>'.$row["last_name"].'</td>             <td>$'.$row["interp"].'</td>         </tr>     '; }  $output .= '</table>';  //echo $output;  $document->loadhtml($output);  //set page size , orientation  $document->setpaper('a4', 'landscape');  //render html pdf  $document->render();  //get output of generated pdf in browser  $document->stream("webslesson", array("attachment"=>0)); //1  = download //0 = preview   ?> 

i using dompdf. looking help. in advance.

this how use dompdf. use time. version using 0.7.0. i'm providing full tested sample, , should know if doesn't work, it's html. dompdf can picky uses html. try sample code, make sure line includes autoload.inc.pdf location of autoload.inc.pdf.

<?php  use dompdf\dompdf; use dompdf\options;  function write_file($path, $data, $mode = 'wb') {     if ( ! $fp = @fopen($path, $mode))         return false;      flock($fp, lock_ex);     fwrite($fp, $data);     flock($fp, lock_un);     fclose($fp);      return true; }  function pdf_create( $html, $filename, $output_type = 'stream', $dompdf_cfg = [] ) {     // remove created headers if streaming output     if( $output_type == 'stream' )          header_remove();      // load dompdf , create object     require_once '../autoload.inc.php';     $options = new options();     $options->set('ishtml5parserenabled', true);     $options->set('isremoteenabled', true);     $dompdf = new dompdf($options);      foreach( $dompdf_cfg $k => $v )         $dompdf->$k($v);      // loads html string     $dompdf->loadhtml( $html );      // create pdf     $dompdf->render();      // if destination browser     if( $output_type == 'stream' )     {         $dompdf->stream( $filename );     }      // return pdf string (useful email attachments)     else if( $output_type == 'string' )     {         return $dompdf->output( ['compress' => 1] );     }      // if saving server     else      {         // save file         write_file( $filename, $dompdf->output() );     } }  $html = '<!doctype html> <html lang="en"> <head>     <meta http-equiv="content-type" content="text/html; charset=utf-8" />     <title>hello world</title>     <style>         h1{color:red;}     </style> </head> <body>     <h1>hello world</h1> </body> </html>';  pdf_create( $html, 'webslesson', 'stream' ); 

this should work you, , if , doesn't html, post html here, , we'll try figure out what's wrong it. guess there's chance dompdf version not same mine. let me know if there problem having. again, code tested, , used time.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -