0

I'm very new to TCPDF. I'm trying to generate some visitor pass from database and display/save them as PDF. But it is producing Blank Page. Please help me. My full code is below.

<?php require_once('db.php'); 
require_once('pdf.php'); 
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 006');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data

$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('dejavusans', '', 10);
// add a page
$pdf->AddPage();
?>
<?php
function data($name, $data){
    $q=mysql_fetch_row(mysql_query("SELECT `$data` FROM page_setting WHERE setting_name='temp1'"));
    return $q['0'];
}
?>
<?php
$name='temp1';
//echo "<div style='width:".data($name, "pg_width")."'>";
//$name=strip_tags($_POST['setting_name']);
//$company=addslashes($_POST['company_name']);
//$company_member=filter_var($_POST['totaluser'], FILTER_SANITIZE_NUMBER_INT);
$r=mysql_query("SELECT name,company,country,name,country,pic FROM pass WHERE company=25");
//$dir=mkdir('temp_pass', 0777);
$con="temp_pass/";
while($q=mysql_fetch_row($r)){
$myname=$q['0']; /// Name of the Person;
$mycompany=$q['1'];
$mycountry=$q['2'];
$myemail=$q['3'];
$myphone=$q['4'];
$img="http://thirdeyenet.biz/".$q['5'];

$fast= '<table border="0" align="left" width="'.data($name, "pg_width").'" height="'.data($name, "pg_height").'" style="border:#999999 1px solid; background:url("watermark/'.data($name, "watermark").') center no-repeat">
  <tr>
    <td style=" background:#'.data($name, "bgcolor").' url('.data($name, "pg_bg_img").') no-repeat center;" width="'.data($name, "pg_width").'" height="'.data($name, "pg_height").'"><img src="bc/'.data($name,"bar").'.png" style="position:relative; '.data($name,"bc_position").'" align="absmiddle" /><div id="main" style="position:relative;margin-top:'.data($name, "pg_margin_top").'; margin-bottom:'.data($name, "pg_margin_bottom").'; margin-left:'.data($name, "pg_margin_left").'; margin-right:'.data($name, "pg_margin_right").'">';
//        <!-- Name will be here-->
 if(data($name, "name_font_visible")=="visible"){
$fnt= '<div align="left" style="position:relative; '.data($name, "name_position").' visibility:'.data($name, "name_font_visible").'; font-family:'.data($name, "name_font").'; color:'.data($name, "name_font_color").'; font-size:'.data($name, "name_font_size").'; font-style:'.data($name, "name_font_style").'">Name: '.$myname.'</div>
      </div>';
      }

//      <!-- Company Name will be here-->

 if(data($name, "company_font_visible")=="visible"){
 $cmpny ='<div align="left" style="position:relative; '.data($name, "company_position").' visibility:'.data($name, "company_font_visible").'; font-family:'.data($name, "company_font").'; color:'.data($name, "company_font_color").'; font-size:'.data($name, "company_font_size").'; font-style:'.data($name, "company_font_style").'">Company: '.$mycompany.'</div>';
       }

//      <!-- Country Name will be here-->

  if( data($name, "country_font_visible")=="visible"){
 $cntry= '    <div align="left" style="position:relative; '.data($name, "country_position").' visibility:'.data($name, "country_font_visible").'; font-family:'.data($name, "country_font").'; color:'.data($name, "country_font_color").'; font-size:'.data($name, "country_font_size").'; font-style:'.data($name, "country_font_style").'">Country: '.$mycountry.'</div>';
      }

//      <!-- Email will be here-->

if(data($name, "email_font_visible")=="visible"){
    $eml='<div align="left" style="position:relative; '.data($name, "email_position").' visibility:'.data($name, "email_font_visible").'; font-family:'.data($name, "email_font").'; color:'.data($name, "email_font_color").'; font-size:'.data($name, "email_font_size").'; font-style:'.data($name, "email_font_style").'">Email id: '.$myemail.'</div>';
      } 

//      <!-- Phone No will be here-->

      if(data($name, "phone_font_visible")=="visible"){
   $phn= ' <div align="left" style="position:relative; '.data($name, "phone_position").' visibility:'.data($name, "phone_font_visible").'; font-family:'.data($name, "phone_font").'; color:'.data($name, "phone_font_color").'; font-size:'.data($name, "phone_font_size").'; font-style:'.data($name, "phone_font_style").'">Contact No: '.$myphone.'</div>
      </div>';
      }
    $last=  '<div style="position:relative;'.data($name, "photo_position").'"><img src="'.$img.'" alt="" border="0" width="'.data($name, "phot_wd").'" height="'.data($name, "phot_hd").'" /></div><div style="margin-top:0px; margin-bottom:2px;"></div></td>
  </tr>
</table>';
$file=$fast.$fnt.$cmpny.$cntry.$eml.$phn.$last;
/*$myFile = $con.mt_rand().".htm";
$fh = fopen($myFile, 'w');
fwrite($fh, $file);
fclose($fh);
*/
//$pdf->writeHTML($file, true, false, true, false, '');
//phptopdf_url($file, $con, mt_rand().".pdf");

//echo $file;
//$file="<html><body><h1>hi</h1></body></html>";
$pdf->writeHTML($file, true, false, true, true);

$pdf->lastPage();
//$pdf->Output('example_002.pdf', 'I');

}



//echo "</div>";
echo '<script type="text/javascript">
        function PrintWindow()
        {                    
           window.print();            
           CheckWindowState();
        }

        function CheckWindowState()
        {           
            if(document.readyState=="complete")
            {
                window.close(); 
            }
            else
            {           
                setTimeout("CheckWindowState()", 1500)
            }
        }    

       PrintWindow();
</script>';



?>

Please help me to find where is the wrong..

Haren Sarma
  • 2,267
  • 6
  • 43
  • 72

2 Answers2

0

You should to uncomment $pdf->Output('example_002.pdf', 'I'), use the print and close javascript functions from the primary window.

for example:

echo  '<script type="text/javascript" language="javascript">
    window.open("'.$this->url.'", "window_internal_name", '.$this->opc_ventana.');
    window.print();
    setTimeout("window.close();", 10000); </script>';
</pre>

Writing the javascript function in the secondary window will not work and then blank page is produced.

Kara
  • 6,115
  • 16
  • 50
  • 57
0

Make sure your html which is going to PDF is valid and it does not miss any tag and or have any error. Try to output the HTML first and see if everything is fine.

Also try to outout simple small valid html to test the TCPDF.

Arfeen
  • 2,553
  • 5
  • 29
  • 48