TC PDF 正在生成空白页

发布于 2024-12-18 01:27:26 字数 6515 浏览 0 评论 0原文

我对 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>';



?>

请帮我看看哪里错了..

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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许久 2024-12-25 01:27:26

确保您要转换为 PDF 的 html 有效,并且没有丢失任何标签或有任何错误。首先尝试输出 HTML 并查看是否一切正常。

还尝试输出简单的小型有效 html 来测试 TCPDF。

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.

太阳哥哥 2024-12-25 01:27:26

您应该取消注释 $pdf->Output('example_002.pdf', 'I'),使用主窗口中的 print 和 close 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函数将不起作用,然后会产生空白页面。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文