【问题标题】:Generate PDF pages with TCPDF and PHP使用 TCPDF 和 PHP 生成 PDF 页面
【发布时间】:2016-03-08 00:43:28
【问题描述】:

我正在尝试生成证书的 pdf 文档。该列表来自表单帖子。使用直接 php,没有 tcpdf 代码,它会生成每个证书。当我添加 tcpdf 代码时,它只生成 1 个证书。 谢谢

// set font
$pdf->SetFont('times', '', 20);
// add a page
$pdf->AddPage();

// LOOP
    while ($rowPart = mysql_fetch_array($result)) 
        {
    $PartName = ucfirst(strtolower($rowPart['firstname'])) . " " . ucfirst(strtolower($rowPart['lastname']));
// PDF STARTS HERE**************************
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetFont('alexbrush', '', 35);
    $pdf->SetY( 67);
    $pdf->Cell(0, 0, $PartName, 0, 0, 'C', 0, '', 3);
    $pdf->SetFont('helveticaB', '', 15);
    $pdf->SetY( 127);
    $pdf->Cell(0, 0, $SemDesc, 0, 1, 'C', 0, '', 3);
    $pdf->SetY( 135);
    $pdf->Cell(0, 0, $SemName, 0, 1, 'C', 0, '', 3);
    $pdf->Ln();
        }
// LOOP ENDS HERE**************************

//Close and output PDF document
$pdf->Output('certificate.php', 'I');

【问题讨论】:

    标签: php tcpdf


    【解决方案1】:

    尝试在循环的开头而不是之前添加一个新页面。我认为您正在创建一个页面并在循环中重写它。

    编辑: 另外,在循环的最后一行代码添加调用 endPage() 函数。

    【讨论】:

    • 谢谢加文。我将 AddPage 移到循环内,但仍然只有 1 个 Cert。 // LOOP while ($rowPart = mysql_fetch_array($result)) { // add a page $pdf->AddPage(); $PartName = ucfirst(strtolower($rowPart['firstname'])) . " " . ucfirst(strtolower($rowPart['lastname'])); // PDF STARTS HERE************************** $pdf->SetTextColor(255, 0, 0);
    • 仍然只有 1. grrr!当我删除 TCPDF 并在循环中回显 $PartName 时,我得到了预期的结果。
    【解决方案2】:

    想要显示从我检查帖子的位置开始的完整代码:

    if (is_array($_POST['partSel'])) {
    // START CERTIFICATE PRINT
    
    // Include the main TCPDF library (search for installation path).
    require_once('tcpdf/tcpdf.php');
    // Define Paramaters
    define('IMG_PATH', 'certificates/');
    
    // Extend the TCPDF class to create custom Header and Footer
    class ETCPDF extends TCPDF {
    //Page header
    public function Header() {
    // get the current page break margin
      $bMargin = $this->getBreakMargin();
    // get current auto-page-break mode
      $auto_page_break = $this->AutoPageBreak;
    // disable auto-page-break
      $this->SetAutoPageBreak(false, 0);
    // set bacground image
      $img_file = IMG_PATH.'etc_cert.png';
      $this->Image($img_file, 0, 0, 297, 210, '', '', '', false, 300, '', false, false, 0);
    // restore auto-page-break status
      $this->SetAutoPageBreak($auto_page_break, $bMargin);
    // set the starting point for the page content
      $this->setPageMark();
      }
    }
    // create new PDF document
    $pdf = new ETCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    
    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('');
    $pdf->SetTitle('Certificate');
    $pdf->SetSubject('');
    $pdf->SetKeywords('');
    
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    
    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(0);
    $pdf->SetFooterMargin(0);
    
    // remove default footer
    $pdf->setPrintFooter(false);
    
    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, 0);
    
    // set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    
    // set some language-dependent strings (optional)
    if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
        require_once(dirname(__FILE__).'/lang/eng.php');
        $pdf->setLanguageArray($l);
    }
    // ---------------------------------------------------------
    
    
    foreach ($_REQUEST['partSel'] as $partSel) {
    $apartSel = mysql_real_escape_string($partSel); 
    if ($_POST['sem'] == 'spcl') {
    $sql = "SELECT spclactivitytitle AS EvtTitle, spclactivitydesc AS EvtDesc 
    FROM tblspclactivity 
    WHERE timeslotid =  '" . $_POST['tsid'] . "'";
    }
    if ($_POST['sem'] == 'sem') {
    $sql = "SELECT tblseminars.seminarpresenter AS EvtTitle, tblseminars.seminartitle AS EvtDesc
    FROM tblseminars Inner Join tblseminar_timeslot ON tblseminar_timeslot.seminarid = tblseminars.seminarid
    WHERE tblseminar_timeslot.timeslotid =  '" . $_POST['tsid'] . "' AND tblseminar_timeslot.seminarid =  '" . $_POST['sid'] . "'";
    }
    if ($debug) { echo "<p>L117 $sql</p>"; }
    $result = mysql_query($sql) or die ('<p>DB Error: ' . mysql_error() . '</p>');
    while ($rowSem = mysql_fetch_array($result)) 
    {
    $SemName = "Presented by: " . $rowSem['EvtTitle'];
    $SemDesc = $rowSem['EvtDesc'];
    }
    
    $sql = "SELECT firstname, lastname FROM tblparticipants WHERE participantid = '$apartSel'";
    $result = mysql_query($sql) or die ('<p>DB Error: ' . mysql_error() . '</p>');
    
    // set font
    $pdf->SetFont('times', '', 20);
    
    // LOOP STARTS HERE
        while ($rowPart = mysql_fetch_array($result)) 
        {
    // add a page
          $pdf->AddPage();
          $PartName = ucfirst(strtolower($rowPart['firstname'])) . " " . ucfirst(strtolower($rowPart['lastname']));
    // PDF STARTS HERE**************************
          $pdf->SetTextColor(255, 0, 0);
          $pdf->SetFont('alexbrush', '', 35);
          $pdf->SetY(67);
          $pdf->Cell(0, 0, $PartName, 0, 0, 'C', 0, '', 3);
    
          $pdf->SetFont('helveticaB', '', 15);
          $pdf->SetY(127);
          $pdf->Cell(0, 0, $SemDesc, 0, 1, 'C', 0, '', 3);
    
          $pdf->SetY( 135);
          $pdf->Cell(0, 0, $SemName, 0, 1, 'C', 0, '', 3);
          $pdf->endPage();
    
        }
    // LOOP ENDS HERE**************************    
    //Close and output PDF document
    $pdf->Output('certificate.php', 'I');
    
    //============================================================+
    // END OF PDF FILE
    //============================================================+
    }
    }
    

    【讨论】:

      【解决方案3】:

      不知道为什么,但 MultiCell 解决了这个问题!谁能解释一下 MultiCell 和 Cell 的区别?

      // LOOP STARTS HERE
      while ($rowPart = mysql_fetch_array($resultPart)) 
      {
      $PartName = ucfirst(strtolower($rowPart['firstname'])) . " " . ucfirst(strtolower($rowPart['lastname']));
      // PDF STARTS HERE**************************
      // add a page
      $pdf->AddPage();
      $pdf->SetTextColor(255, 0, 0);
      $pdf->SetFont('alexbrush', '', 35);
      $pdf->MultiCell(0, 0, $PartName, 0, 'C', false, 0, 10, 67);
      
      $pdf->SetFont('helveticaB', '', 15);
      $pdf->MultiCell(0, 0, $SemDesc, 0, 'C', false, 0, 10, 127);
      $pdf->MultiCell(0, 0, $SemName, 0, 'C', false, 0, 10, 135);
      }
      // LOOP ENDS HERE**************************
      
      //============================================================+
      // END OF PDF FILE
      //============================================================+
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-29
        • 2012-11-13
        相关资源
        最近更新 更多