【问题标题】:How to generate pdf using Codeigniter?如何使用 Codeigniter 生成 pdf?
【发布时间】:2017-12-30 12:15:24
【问题描述】:

我尝试过使用 dompdf。以下是Codeigniter中的控制器

function get_pdf( )
{
$this->load->library('Pdf');
$this->pdf->load_view('report.php' );
$this->pdf->load_html('report.php','',true);
$this->pdf->render();
$this->pdf->stream("welcome.pdf");
}

report.php 中的数据

 
  $(document).ready(function(){ 
      
   
   
    
 $.ajax({
                type: "POST",
                url: $u+"get",
                data: {
                  
                  role:"report",
                   },

                datatype: "json",
                crossDomain: true,
                success: function(result) {
                   var result = $.parseJSON(result);
                     for (var result in syncx) {

                    $("#tta tbody").append("<tr ><td class='cyan'>"+x+"</td><td>"+syncx[x].total+ "</td> </tr>");
                          
                      }
                   
                       Morris.Line({
                element: 'total-line',
                data: source,
                behaveLikeLine: true,
               parseTime : false, 
                xkey: 'x',
                ykeys: [ 'totol'],
                labels: [  'total'],
                pointFillColors: ['#707f9b'],
                pointStrokeColors: ['#ffaaab'],
                lineColors: ['#f26c4f', '#00a651', '#00bff3'],
                redraw: true 
            });

                }
}
                          });

  
 });

  

 
  
 
<body>
   <div class="row ">
     
      
          <div id="responsive" class=" ">
        <h4  > Record</h4>
        <div class=" ">
          <div id="flip-scroll" class="card">
            <p> </p>
            <table id="tta"   >
              <thead>
                <tr>
                    <th>x</th>
                    <th>Total </th>
                  
                </tr>
              </thead>
              <tbody> </tbody>
            </table>
          </div>
        </div>

      </div>

     
       </div>

      
    
    <div class="col  ">
      <div class="card">
        <div class="card-image">
          <div  id="total-line"></div>
          <span class="card-title"> </span>
          
        </div>
        <div class="card-content">   <p>Total</p>
    </div>
      </div>
    </div>
 
 
  

</body>
 

生成了 pdf 报告,但那里只有 html 数据。 ajax 和 jquery 数据丢失。没有生成图表,表格也没有数据。我应该在这里做什么来生成完整的报告,表格中的图表和 ajax 数据完成。此处视图按原样获取,但从 ajax 获取的数据全部丢失。

编辑:请提出其他适合实现这一目标的方法。

【问题讨论】:

    标签: php codeigniter dompdf


    【解决方案1】:

    试试这个

    //File name
    $filename = "Whatever-you-wish";
    
    //Data can be any variables that you want to pass it on to the view.If you don't have anything to pass, just add null to the 2nd parameter.
    $html = $this->load->view('sample',$data, true);  
    
    //load library. I assume you have already uploaded the dom files under  application/library folder
    $this->load->library('pdf');
    
    //Intitialize dompdf object
    $dompdf = new Dompdf\Dompdf();
    
    //add view
    $dompdf->load_html($html);
    
    //Render the PDF
    $dompdf->render();
    
    //If you have any options to add it to this function
    $options = new Dompdf\Options();
    $options->set('isRemoteEnabled', TRUE);
    
    //Default paper size
    $dompdf->set_paper('DEFAULT_PDF_PAPER_SIZE', 'portrait');
    
    //Output the result
    $output = $dompdf->output();
    
    //Save it to the folder. Folder has to be created outside the application folder.
    file_put_contents('your-folder-name/' . $filename . '.pdf', $output);
    

    希望这能奏效。谢谢。

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多