在 Codeigniter 上生成多页 TCPDF
Generate Multiple Page TCPDF on Codeigniter
我有一个 table 用于显示数据并根据每一行生成报告。 table 会是这样的:
-----------------------------------------------------------------------------------------
| employee_name | division | period | allowance | download | checkbox |
| tony | advertising | august 2018| 15.00 |(downloadbtn)|(checkboxbtn)|
| lola | advertising | august 2018| 20.00 |(downloadbtn)|(checkboxbtn)|
-----------------------------------------------------------------------------------------
到目前为止,这是我生成 pdf 的控制器:
<?php
class AllowanceReport extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('allowance_m');
}
public function index(){
$this->load->view('allowance_v');
}
public function reportpdf($id){
ob_start();
$allowance= $this->allowance_m->get_allowance_byid($id);
$this->load->library('pdf_surat');
$pdf = new
PDF_SURAT('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
$pdf->setXY(12,40);
$txt_pembuka = 'Allowance Report';
//$pdf->SetFontSize(16);
$pdf->SetFont('times', 'B', 16, '', 'false');
$pdf->MultiCell(0, 5, $txt_pembuka, 0, 'C', 0, 2, '', '', true);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->SetFont('times', '', 12, '', 'false');
$pdf->cell(35,5,"Nama");
$pdf->cell(0,5,": ".$allowance->employee_name);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Periode");
$periode = strtotime($allowance->periode);
$formatperiode = date('F Y',$periode);
$pdf->cell(0,5,": ".$formatperiode);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Uang Makan");
$pdf->cell(0,5,": Rp.".$allowance->allowance);
$pdf->Output();
ob_end_flush();
}
}
?>
这是视图:
<div class="table">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>Employee Name</center></th>
<th><center>Division</center></th>
<th><center>Period</center></th>
<th><center>Allowance</center></th>
<th><center>Download</center></th>
<th><center><input type="checkbox" class="checkAll" /></center></th>
<?php } ?>
</tr>
</thead>
<?php
$no = 1;
if(is_array($query)){
foreach($query as $row):
?>
<tr>
<td><?php echo $row->employee_name ?></td>
<td><?php echo $row->division ?></td>
<td><?php echo date("F-Y", strtotime($row->period)) ?></td>
<td><?php echo $row->allowance ?></td>
<td>
<center>
<a href="<?php echo base_url(). "index.php/allowancereport/reportpdf/".$row->idallowance;?>" class="btn btn-success" id="cetaksurat_"><b><span class="glyphicon glyphicon-download"></span></b></a>
</center>
</td>
<td>
<center>
<input type="checkbox" class="checkitem" value="<?php echo $row->idmakan; ?>"/>
</center>
</td>
<td>
<center>
<a href="<?php echo base_url();?>absengaji/edit/<?php echo $row->idmakan;?>" class="btn btn-primary btn-xs edit" data-toggle="modal" id="edit_">
<span class="glyphicon glyphicon-pencil"></span></a>
</center>
</td>
<?php } ?>
</tr>
<?php endforeach ; } ?>
</table>
</div>
</div>
</div>
</div>
这是我的模型
public function get_allowance_byid($id)
{
$sql = "SELECT * FROM allowance WHERE idallowance='".$id."'";
return $this->db->query($sql)->row();
}
我设法为每一行生成了一个 pdf。但是是否可以根据选中的复选框生成包含多个页面的文件? pdf文件是否可以生成每个数据,并在文件中的不同页面进行检查?
你可以像下面那样做
前端
在table标签前后添加表单标签,将表单目标设置为新方法(allreportpdf
),同时添加提交按钮触发全部保存report inside the form (maybe before the form close tag) :
<form action="<?php echo base_url() . "index.php/allowancereport/allreportpdf" ?>" method="POST" role="form">
<table class="table table-striped table-bordered table-hover">
...
...
...
</table>
<button type="submit" class="btn btn-primary">Get All Report</button>
</form>
在复选框输入上添加一个名称(可能是 eid
),它将用作新方法(allreportpdf
)上的 post 数据。
<center>
<input type="checkbox" class="checkitem" name="eid[]" value="<?php echo $row->idmakan; ?>"/>
</center>
后端
由于这个页面使用不同的布局,你应该在AllowanceReport
class上创建一个新方法,使用之前设置的post名称(eid
),你可以在 foreach()
循环中使用它,并修改 pdf 设置,使每个 id 在每个页面上分开:
public function allreportpdf(){
ob_start();
$this->load->library('pdf_surat');
$pdf = new
PDF_SURAT('P','mm','A4',true,'UTF-8',false);
foreach ($this->input->post('eid') as $id) {
$allowance= $this->allowance_m->get_allowance_byid($id);
$pdf->AddPage();
$pdf->setXY(12,40);
$txt_pembuka = 'Allowance Report';
//$pdf->SetFontSize(16);
$pdf->SetFont('times', 'B', 16, '', 'false');
$pdf->MultiCell(0, 5, $txt_pembuka, 0, 'C', 0, 2, '', '', true);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->SetFont('times', '', 12, '', 'false');
$pdf->cell(35,5,"Nama");
$pdf->cell(0,5,": ".$allowance->employee_name);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Periode");
$periode = strtotime($allowance->periode);
$formatperiode = date('F Y',$periode);
$pdf->cell(0,5,": ".$formatperiode);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Uang Makan");
$pdf->cell(0,5,": Rp.".$allowance->allowance);
}
$pdf->output();
ob_end_flush();
}
我有一个 table 用于显示数据并根据每一行生成报告。 table 会是这样的:
-----------------------------------------------------------------------------------------
| employee_name | division | period | allowance | download | checkbox |
| tony | advertising | august 2018| 15.00 |(downloadbtn)|(checkboxbtn)|
| lola | advertising | august 2018| 20.00 |(downloadbtn)|(checkboxbtn)|
-----------------------------------------------------------------------------------------
到目前为止,这是我生成 pdf 的控制器:
<?php
class AllowanceReport extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('allowance_m');
}
public function index(){
$this->load->view('allowance_v');
}
public function reportpdf($id){
ob_start();
$allowance= $this->allowance_m->get_allowance_byid($id);
$this->load->library('pdf_surat');
$pdf = new
PDF_SURAT('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
$pdf->setXY(12,40);
$txt_pembuka = 'Allowance Report';
//$pdf->SetFontSize(16);
$pdf->SetFont('times', 'B', 16, '', 'false');
$pdf->MultiCell(0, 5, $txt_pembuka, 0, 'C', 0, 2, '', '', true);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->SetFont('times', '', 12, '', 'false');
$pdf->cell(35,5,"Nama");
$pdf->cell(0,5,": ".$allowance->employee_name);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Periode");
$periode = strtotime($allowance->periode);
$formatperiode = date('F Y',$periode);
$pdf->cell(0,5,": ".$formatperiode);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Uang Makan");
$pdf->cell(0,5,": Rp.".$allowance->allowance);
$pdf->Output();
ob_end_flush();
}
}
?>
这是视图:
<div class="table">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>Employee Name</center></th>
<th><center>Division</center></th>
<th><center>Period</center></th>
<th><center>Allowance</center></th>
<th><center>Download</center></th>
<th><center><input type="checkbox" class="checkAll" /></center></th>
<?php } ?>
</tr>
</thead>
<?php
$no = 1;
if(is_array($query)){
foreach($query as $row):
?>
<tr>
<td><?php echo $row->employee_name ?></td>
<td><?php echo $row->division ?></td>
<td><?php echo date("F-Y", strtotime($row->period)) ?></td>
<td><?php echo $row->allowance ?></td>
<td>
<center>
<a href="<?php echo base_url(). "index.php/allowancereport/reportpdf/".$row->idallowance;?>" class="btn btn-success" id="cetaksurat_"><b><span class="glyphicon glyphicon-download"></span></b></a>
</center>
</td>
<td>
<center>
<input type="checkbox" class="checkitem" value="<?php echo $row->idmakan; ?>"/>
</center>
</td>
<td>
<center>
<a href="<?php echo base_url();?>absengaji/edit/<?php echo $row->idmakan;?>" class="btn btn-primary btn-xs edit" data-toggle="modal" id="edit_">
<span class="glyphicon glyphicon-pencil"></span></a>
</center>
</td>
<?php } ?>
</tr>
<?php endforeach ; } ?>
</table>
</div>
</div>
</div>
</div>
这是我的模型
public function get_allowance_byid($id)
{
$sql = "SELECT * FROM allowance WHERE idallowance='".$id."'";
return $this->db->query($sql)->row();
}
我设法为每一行生成了一个 pdf。但是是否可以根据选中的复选框生成包含多个页面的文件? pdf文件是否可以生成每个数据,并在文件中的不同页面进行检查?
你可以像下面那样做
前端
在table标签前后添加表单标签,将表单目标设置为新方法(allreportpdf
),同时添加提交按钮触发全部保存report inside the form (maybe before the form close tag) :
<form action="<?php echo base_url() . "index.php/allowancereport/allreportpdf" ?>" method="POST" role="form">
<table class="table table-striped table-bordered table-hover">
...
...
...
</table>
<button type="submit" class="btn btn-primary">Get All Report</button>
</form>
在复选框输入上添加一个名称(可能是 eid
),它将用作新方法(allreportpdf
)上的 post 数据。
<center>
<input type="checkbox" class="checkitem" name="eid[]" value="<?php echo $row->idmakan; ?>"/>
</center>
后端
由于这个页面使用不同的布局,你应该在AllowanceReport
class上创建一个新方法,使用之前设置的post名称(eid
),你可以在 foreach()
循环中使用它,并修改 pdf 设置,使每个 id 在每个页面上分开:
public function allreportpdf(){
ob_start();
$this->load->library('pdf_surat');
$pdf = new
PDF_SURAT('P','mm','A4',true,'UTF-8',false);
foreach ($this->input->post('eid') as $id) {
$allowance= $this->allowance_m->get_allowance_byid($id);
$pdf->AddPage();
$pdf->setXY(12,40);
$txt_pembuka = 'Allowance Report';
//$pdf->SetFontSize(16);
$pdf->SetFont('times', 'B', 16, '', 'false');
$pdf->MultiCell(0, 5, $txt_pembuka, 0, 'C', 0, 2, '', '', true);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->SetFont('times', '', 12, '', 'false');
$pdf->cell(35,5,"Nama");
$pdf->cell(0,5,": ".$allowance->employee_name);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Periode");
$periode = strtotime($allowance->periode);
$formatperiode = date('F Y',$periode);
$pdf->cell(0,5,": ".$formatperiode);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Uang Makan");
$pdf->cell(0,5,": Rp.".$allowance->allowance);
}
$pdf->output();
ob_end_flush();
}