【问题标题】:TCPDF: Rotate Text / Cell in Table HeaderTCPDF:旋转表格标题中的文本/单元格
【发布时间】:2011-10-09 05:55:45
【问题描述】:

我在 TCPDF 中创建一个表格时遇到了一些问题,在该表格中,除第一个和最后一个之外的所有标题列都应旋转 90 度(或者:该单元格中的文本应该是垂直的)。

有没有办法只在一个单元格内旋转测试,而不是整个单元格本身?

我希望表格如下所示:

感谢您的帮助。

【问题讨论】:

    标签: tcpdf


    【解决方案1】:

    这是一个古老的讨论,但我仍然面临这个问题。我已经在我的 TCPDF fork 上做了一个解决方法。 http://sourceforge.net/u/mnicolardi/tcpdf/ci/master/tree/

    我添加了一个参数,通知 MultiCell 方法它在 90 度旋转环境中运行。在这种情况下,它使用 $h 将单元格向右移动。

    新方法原型为:

    public function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false,$rotated90Degrees=false) 
    

    使用示例:

     $pdf->StartTransform();          
     $pdf->Rotate(90);
     $pdf->MultiCell(100, 10, "rotated text", 1, 'C', false, 0, "", "", true, 0, false, true, 0, "T", false, true);
     $pdf->StopTransform();
    

    希望对你有帮助!

    【讨论】:

      【解决方案2】:

      很简单,请检查以下代码。

      $pdf->StartTransform();
      $pdf->Rotate(-90);
      $pdf->Cell(0,0,'This is a sample data',1,1,'L',0,'');
      $pdf->StopTransform();
      

      【讨论】:

      • 当您使用 MultiCell 并指定宽度时,在 StopTransform() 之后,下一个单元格开始时就像旋转的单元格是水平的一样。这意味着旋转的单元格和下一个水平单元格之间有一个空间。你明白我想说什么吗? X_X 请帮帮我!!
      【解决方案3】:

      过去两周我一直在努力解决这个问题。

      调用 Rotate 方法时,TCPDF 标记运行良好,但不是我不喜欢的。 我的解决方案是:

      1. 创建一个方法来获取带有 TCPDF 标签的每一列的 X 和 Y;
      2. 写完一个HTML表格后,你必须设置你得到的每一列的变换Rotate,根据页眉设置一个新的X和Y;
      3. 为了解决这个问题,如果您的多页表的每一页都有一个标题复制,我得到了页数并为每一页设置了旋转文本。

      现在,代码示例...


      // Extend the TCPDF class to create custom Header and Footer
      class MYPDF extends TCPDF {
          //Page header
          public function Test( $ae ) {
              if( !isset($this->xywalter) ) {
                  $this->xywalter = array();
              }
              $this->xywalter[] = array($this->GetX(), $this->GetY());
          }
      }
      
      // create new PDF document
      $pdf = new MYPDF('L', PDF_UNIT, 'A1', true, 'UTF-8', false);
      
      // set Rotate
      $params = $pdf->serializeTCPDFtagParameters(array(90));
      
      // other configs
      $pdf->setOpenCell(0);
      $pdf->SetCellPadding(0);
      $pdf->setCellHeightRatio(1.25);
      
      // create some HTML content
      $html = '<table width="100%" border="1" cellspacing="0" cellpadding="5">
      <thead>
      <tr bgcolor="#E6E6E6">
      <th rowspan="2" width="15%" align="center">ATIVIDADES E PROCESSOS</th>
      <th rowspan="2" width="10%" align="center" valign="bottom">ASPECTOS</th>
      <th rowspan="2" width="10%" align="center">IMPACTOS</th>
      <th colspan="3" width="6%" align="center">MEIO</th>
      <th rowspan="2" width="3%" align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th colspan="9" width="18%" align="center">CLASSIFICA&Ccedil;&Otilde;ES</th>
      <th rowspan="2" width="3%" align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th rowspan="2" width="10%" align="center">PROGRAMA</th>
      <th rowspan="2" width="10%" align="center">SUBPROGRAMA</th>
      <th rowspan="2" width="15%" align="center">A&Ccedil;&Otilde;ES DE CONTROLE, MEDIDAS MITIGADORAS, COMPENSAT&Oacute;RIAS E POTENCIALIZADORAS</th>
      </tr>
      <tr bgcolor="#E6E6E6">
      <th align="center" height="200"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      <th align="center"><tcpdf method="Test" params="'.$params.'" /></th>
      </tr>
      </thead>
      <tr bgcolor="#E6E6E6">
      <td colspan="20" align="center">Planejamento</td>
      </tr>
      <tr bgcolor="#FFFFFF">
      <td rowspan="3" width="15%" align="left" bgcolor="#FFFFFF">Divulga&ccedil;&atilde;o do empreendimento</td>
      <td rowspan="2" width="10%" align="left">Oferta de empregos diretos e indiretos</td>
      <td rowspan="2" width="10%" align="left">Atra&ccedil;&atilde;o de popula&ccedil;&atilde;o para as comunidades do entorno</td>
      <td rowspan="2" width="2%" align="center"></td>
      <td rowspan="2" width="2%" align="center"></td>
      <td rowspan="2" width="2%" align="center">X</td>
      <td rowspan="2" width="3%" align="center">AII</td>
      <td rowspan="2" width="2%" align="center">-</td>
      <td rowspan="2" width="2%" align="center">Ind</td>
      <td rowspan="2" width="2%" align="center">T</td>
      <td rowspan="2" width="2%" align="center">Mp</td>
      <td rowspan="2" width="2%" align="center">Po</td>
      <td rowspan="2" width="2%" align="center">D</td>
      <td rowspan="2" width="2%" align="center">R</td>
      <td rowspan="2" width="2%" align="center">M</td>
      <td rowspan="2" width="2%" align="center">M</td>
      <td rowspan="2" width="3%" align="center">M</td>
      <td width="10%">Programa de Apoio ao Desenvolvimento Socioeconomico da Regi&atilde;o</td>
      <td width="10%">Subprograma de Apoio ao Desenvolvimento Habitacional</td>
      <td width="15%">Coibir ocupa&ccedil;&atilde;o indevida de &aacute;reas inadequadas</td>
      </tr>
      <tr bgcolor="#FFFFFF">
      <td>Programa de Comunica&ccedil;&atilde;o Social</td>
      <td> -</td>
      <td>A&ccedil;&otilde;es de comunica&ccedil;&atilde;o sobre o empreendimento e quest&otilde;es ambientais</td>
      </tr>
      <tr bgcolor="#FFFFFF">
      <td align="left">Gera&ccedil;&atilde;o de expectativas na popula&ccedil;&atilde;o</td>
      <td align="left">Gera&ccedil;&atilde;o de expectativas junto a popula&ccedil;&atilde;o</td>
      <td align="center"></td>
      <td align="center"></td>
      <td align="center">X</td>
      <td align="center">AII</td>
      <td align="center">-</td>
      <td align="center">Dir</td>
      <td align="center">T</td>
      <td align="center">Im</td>
      <td align="center">Co</td>
      <td align="center">L</td>
      <td align="center">R</td>
      <td align="center">P</td>
      <td align="center">P</td>
      <td align="center">B</td>
      <td>Programa de Comunica&ccedil;&atilde;o Social</td>
      <td> -</td>
      <td>A&ccedil;&otilde;es de comunica&ccedil;&atilde;o sobre o empreendimento e questoes ambientais</td>
      </tr>
      </table>';
      
      // output the HTML content
      $pdf->writeHTML($html, true, false, true, false, '');
      
      // array with names of columns
      $arr_nomes = array(
          array("ABRANGÊNCIA", 8, 59), // array(name, new X, new Y);
          array("SIGNIFICÂNCIA", 8, 59),
          array("FÃSICO", 4, 52),
          array("BIÓTICO", 4, 52),
          array("SOCIOECONÔMICO", 4, 52),
          array("NATUREZA", 4, 52),
          array("ORIGEM", 4, 52),
          array("DURAÇÃO", 4, 52),
          array("OCORRÊNCIA / TEMPORALIDADE", 4, 52),
          array("FREQUÊNCIA", 4, 52),
          array("ESPACIALIZAÇÃO", 4, 52),
          array("REVERSIBILIDADE", 4, 52),
          array("MAGNITUDE", 4, 52),
          array("RELEVÂNCIA", 4, 52)
      );
      
      // num of pages
      $ttPages = $pdf->getNumPages();
      for($i=1; $i<=$ttPages; $i++) {
          // set page
          $pdf->setPage($i);
          // all columns of current page
          foreach( $arr_nomes as $num => $arrCols ) {
              $x = $pdf->xywalter[$num][0] + $arrCols[1]; // new X
              $y = $pdf->xywalter[$num][1] + $arrCols[2]; // new Y
              $n = $arrCols[0]; // column name
              // transforme Rotate
              $pdf->StartTransform();
              // Rotate 90 degrees counter-clockwise
              $pdf->Rotate(90, $x, $y);
              $pdf->Text($x, $y, $n);
              // Stop Transformation
              $pdf->StopTransform();
          }
      }
      
      // reset pointer to the last page
      $pdf->lastPage();
      
      // ---------------------------------------------------------
      
      //Close and output PDF document
      $pdf->Output('example_006.pdf', 'I');
      

      【讨论】:

      • 我不得不接受另一个答案,因为我没有使用 HTML 表格。但我相信这也是一个不错的解决方案。谢谢你。
      • Wrong page number on setPage() function: 0找不到原因。
      猜你喜欢
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多