在dompdf和cakephp中显示rowspan的问题

时间:2014-09-04 02:15:46

标签: php cakephp dompdf

我一直在网上搜索我的问题的解决方案。我目前正在使用CakePhp + dompdf生成PDF。它在普通表上工作正常,但是当我添加rowpans和colspans时,它会创建表,但边界会被破坏。

这是我用于生成PDF的HTML标记:

<style>table{border-collapse:collapse;}.rows td{border:1px solid brown;</style>
<table class="sub_cat_table">
    <tbody><tr class="rows">
        <td colspan="2">カテゴリ</td>
            <td>重要度</td>
            <td>実現度</td>
            <td colspan="2">項目(キーワード)</td>
            <td colspan="2">重要度</td>
            <td colspan="2">実現度</td>
            <td colspan="2">格差</td>
        </tr>
        <tr class="rows">
            <td rowspan="2" class="sub_cat_letter"> A </td>
            <td rowspan="2" class="sub_cat_name">Situation</td>
            <td rowspan="2" class="sub_cat_imp">3.5</td>
            <td rowspan="2" class="sub_cat_pos">2.0</td>                                            
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">1</td>
            <td class="sub_cat_name">Complete</td>
            <td class="sub_cat_imp_inner">3.0</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">2.0</td>
            <td class="sub_cat_imp_inner">  ●</td>
            <td class="sub_cat_imp_inner">-1</td>
            <td class="sub_cat_imp_inner"></td>
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">2</td>
            <td class="sub_cat_name">Access</td>
            <td class="sub_cat_imp_inner">4.0</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">2.0</td>
            <td class="sub_cat_imp_inner">● </td>
            <td class="sub_cat_imp_inner">-2</td>
            <td class="sub_cat_imp_inner"></td>
        </tr>
        <tr class="rows">
            <td rowspan="3" class="sub_cat_letter">B</td>
            <td rowspan="3" class="sub_cat_name">Technology</td>
            <td rowspan="3" class="sub_cat_imp">3.4</td>
            <td rowspan="3" class="sub_cat_pos">1.9</td>                                            
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">3</td>
            <td class="sub_cat_name">Quality</td>
            <td class="sub_cat_imp_inner">3.7</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">1.7</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">-2</td>
            <td class="sub_cat_imp_inner">▼ </td>
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">4</td>
            <td class="sub_cat_name">Access</td>
            <td class="sub_cat_imp_inner">3.0</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">2.0</td>
            <td class="sub_cat_imp_inner">● </td>
            <td class="sub_cat_imp_inner">-1</td>
            <td class="sub_cat_imp_inner"></td>
        </tr>
    </tbody>
</table>

以HTML格式打印屏幕(抱歉,该表格上方有边框):

enter image description here

在dompdf中生成PDF的打印屏幕:

enter image description here

我一直在尝试使用文件dompdf/includes/cellmap.cls.php,但我无法让它发挥作用。

我已在这里试过这些解决方案:

我已经忘记了一些链接。非常感谢任何建议或解决方法。

3 个答案:

答案 0 :(得分:2)

我认为你不需要改变你的dompdf来创建PDF,你只需要安排你的表特别是行跨度规则。使用行跨度创建表时,它应该是这样的:

<table border="1">
  <tr>
    <td rowspan="3">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

enter image description here

答案 1 :(得分:0)

它真的不是一个“答案”,但我建议使用 tcpdf 切换。我发现它在将HTML渲染为pdf方面做得最好,还有一些比其他pdf库更容易定制。

用于生成示例的代码:

<?php


// Include the main TCPDF library (search for installation path).
require_once('../tcpdf.php');

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


// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// 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 (optional)
if (file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

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

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 6, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// set text shadow effect
//$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

// Set some content to print
$html = <<<EOD
<style>table{border-collapse:collapse;}.rows td{border:1px solid brown;</style>
<table class="sub_cat_table">
    <tbody><tr class="rows">
        <td colspan="2">カテゴリ</td>
            <td>重要度</td>
            <td>実現度</td>
            <td colspan="2">項目(キーワード)</td>
            <td colspan="2">重要度</td>
            <td colspan="2">実現度</td>
            <td colspan="2">格差</td>
        </tr>
        <tr class="rows">
            <td rowspan="2" class="sub_cat_letter"> A </td>
            <td rowspan="2" class="sub_cat_name">Situation</td>
            <td rowspan="2" class="sub_cat_imp">3.5</td>
            <td rowspan="2" class="sub_cat_pos">2.0</td>
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">1</td>
            <td class="sub_cat_name">Complete</td>
            <td class="sub_cat_imp_inner">3.0</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">2.0</td>
            <td class="sub_cat_imp_inner">  ●</td>
            <td class="sub_cat_imp_inner">-1</td>
            <td class="sub_cat_imp_inner"></td>
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">2</td>
            <td class="sub_cat_name">Access</td>
            <td class="sub_cat_imp_inner">4.0</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">2.0</td>
            <td class="sub_cat_imp_inner">● </td>
            <td class="sub_cat_imp_inner">-2</td>
            <td class="sub_cat_imp_inner"></td>
        </tr>
        <tr class="rows">
            <td rowspan="3" class="sub_cat_letter">B</td>
            <td rowspan="3" class="sub_cat_name">Technology</td>
            <td rowspan="3" class="sub_cat_imp">3.4</td>
            <td rowspan="3" class="sub_cat_pos">1.9</td>
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">3</td>
            <td class="sub_cat_name">Quality</td>
            <td class="sub_cat_imp_inner">3.7</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">1.7</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">-2</td>
            <td class="sub_cat_imp_inner">▼ </td>
        </tr>
        <tr class="rows">
            <td class="sub_cat_number">4</td>
            <td class="sub_cat_name">Access</td>
            <td class="sub_cat_imp_inner">3.0</td>
            <td class="sub_cat_imp_inner"></td>
            <td class="sub_cat_imp_inner">2.0</td>
            <td class="sub_cat_imp_inner">● </td>
            <td class="sub_cat_imp_inner">-1</td>
            <td class="sub_cat_imp_inner"></td>
        </tr>
    </tbody>
</table>
EOD;

// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

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

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_001.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

答案 2 :(得分:0)

您只需要正确使用rowspan,就必须在同一行中添加其余的列,这是根据需要创建表的代码。

table {
  border-collapse: collapse;
}

.rows td {
  border: 1px solid brown;
}
<table class="sub_cat_table">
  <tbody>
    <tr class="rows">
      <td colspan="2">カテゴリ</td>
      <td>重要度</td>
      <td>実現度</td>
      <td colspan="2">項目(キーワード)</td>
      <td colspan="2">重要度</td>
      <td colspan="2">実現度</td>
      <td colspan="2">格差</td>
    </tr>
    <tr class="rows">
      <td rowspan="2" class="sub_cat_letter"> A </td>
      <td rowspan="2" class="sub_cat_name">Situation</td>
      <td rowspan="2" class="sub_cat_imp">3.5</td>
      <td rowspan="2" class="sub_cat_pos">2.0</td>
      <td class="sub_cat_number">1</td>
      <td class="sub_cat_name">Complete</td>
      <td class="sub_cat_imp_inner">3.0</td>
      <td class="sub_cat_imp_inner">&nbsp;</td>
      <td class="sub_cat_imp_inner">2.0</td>
      <td class="sub_cat_imp_inner"> ●</td>
      <td class="sub_cat_imp_inner">-1</td>
      <td class="sub_cat_imp_inner"></td>
    </tr>
    <tr class="rows">
      <td class="sub_cat_number">2</td>
      <td class="sub_cat_name">Access</td>
      <td class="sub_cat_imp_inner">4.0</td>
      <td class="sub_cat_imp_inner">&nbsp;</td>
      <td class="sub_cat_imp_inner">2.0</td>
      <td class="sub_cat_imp_inner">● </td>
      <td class="sub_cat_imp_inner">-2</td>
      <td class="sub_cat_imp_inner"></td>
    </tr>
    <tr class="rows">
      <td rowspan="2" class="sub_cat_letter">B</td>
      <td rowspan="2" class="sub_cat_name">Technology</td>
      <td rowspan="2" class="sub_cat_imp">3.4</td>
      <td rowspan="2" class="sub_cat_pos">1.9</td>
      <td class="sub_cat_number">3</td>
      <td class="sub_cat_name">Quality</td>
      <td class="sub_cat_imp_inner">3.7</td>
      <td class="sub_cat_imp_inner">&nbsp;</td>
      <td class="sub_cat_imp_inner">1.7</td>
      <td class="sub_cat_imp_inner">&nbsp;</td>
      <td class="sub_cat_imp_inner">-2</td>
      <td class="sub_cat_imp_inner">▼ </td>
    </tr>
    <tr class="rows">
      <td class="sub_cat_number">4</td>
      <td class="sub_cat_name">Access</td>
      <td class="sub_cat_imp_inner">3.0</td>
      <td class="sub_cat_imp_inner">&nbsp;</td>
      <td class="sub_cat_imp_inner">2.0</td>
      <td class="sub_cat_imp_inner">● </td>
      <td class="sub_cat_imp_inner">-1</td>
      <td class="sub_cat_imp_inner">&nbsp;</td>
    </tr>
  </tbody>
</table>