TC PDF正在生成空白页面

时间:2011-11-23 13:35:25

标签: php pdf-generation tcpdf

我是TCPDF的新手。我正在尝试从数据库生成一些访问者传递并将其显示/保存为PDF。但它正在制作空白页面。请帮我。我的完整代码如下。

<?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>';



?>

请帮我找出哪里错了..

2 个答案:

答案 0 :(得分:0)

确保您的HTML格式有效且不会遗漏任何标记或有任何错误。尝试先输出HTML并查看一切是否正常。

还尝试outout简单的小有效html来测试TCPDF。

答案 1 :(得分:0)

您应该取消注释$pdf->Output('example_002.pdf', 'I'),使用主窗口中的打印和关闭javascript函数。

例如:

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>

在辅助窗口中编写javascript函数将不起作用,然后生成空白页。