如何在 tcpdf 中查看 table 数据
how to view table data in tcpdf
我是 codeigniter 和 tcpdf 的新手。现在我想以 pdf 格式显示我的数据库 table 行。我不知道该怎么做。任何人都可以帮助我,我应该在哪里插入代码以及如何编写代码。
这是 pdf 生成器的代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Createpdf extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library("Pdf");
}
public function create_pdf() {
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('lakmini');
$pdf->SetTitle('RCJ CONSTRUCTIONS');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// 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', '', 14, '', 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
<h1> this is my first pdf </h1>
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
//============================================================+
}
}
这是我的数据库table
CREATE TABLE IF NOT EXISTS `staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`designation_id` int(11) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`contact_no` int(11) NOT NULL,
`work_experience` varchar(250) NOT NULL,
`qualifications` varchar(250) NOT NULL,
`nic` varchar(10) NOT NULL,
`gender` varchar(10) NOT NULL,
`created` date NOT NULL,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `designation_id` (`designation_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
我有一个 MPDF 库的代码首先你必须下载最新版本的 codeigniter MPDF 库 link (http://mpdf1.com/repos/MPDF60.zip) 然后解压 zip 文件并将 MPDF 库粘贴到 application/libraries 路径使用下面的代码 --
<a href="<?php echo base_url(); ?>controllerName/PDFFile(method name)">Generate PDF</a>
PDFFile 方法是--
public function PDFFile()
{
$data['results'] = $this->common_model->federationPDF_file();
// it will fetch data from database
$html = $this->load->view('Print_pdf',$data,true); // here we will pass view file name that contain table data
$this->load->library('MPDF/mpdf');
$mpdf=new mPDF('utf-8','A4', '12', '', 10, 10, 10, 10, 9, 9);
$mpdf->useOnlyCoreFonts = true;
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle(SITE_NAME);
$mpdf->SetAuthor(SITE_NAME);
$mpdf->SetCreator(SITE_NAME);
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($stylesheet,1);
$search = array("<div class=\"row-fluid\">", "<div class=\"span6\">");
$replace = array("<div style='width: 100%;'>", "<div style='width: 100%; float: left;'>");
$html = str_replace($search, $replace, $html);
$name = "PDF File"; // file name
$mpdf->WriteHTML($html);
$mpdf->Output($name, 'D');
exit;
}
我的查看文件Print_pdf --
<!DOCTYPE html>
<html>
<head>
<title>PDF File</title>
</head>
<body>
<div align="center">
</div>
<h2 align="center">Table Data</h2>
<table border="1" align="center" cellpadding="1" cellspacing="0">
<thead>
<tr>
<td><b>S.No.</b></td>
<td><b>First Name </b></td>
<td><b>Last Name </b></td>
<td><b>Mobile</b></td>
<td><b>Address</b></td>
</tr>
</thead>
<tbody>
<?php $count = 1;
foreach($results as $s)
{ ?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $s['fname']; ?></td>
<td><?php echo $s['lname']; ?></td>
<td><?php echo $s['mobile']; ?></td>
<td><?php echo $s['address']; ?></td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</body>
</html>
我是 codeigniter 和 tcpdf 的新手。现在我想以 pdf 格式显示我的数据库 table 行。我不知道该怎么做。任何人都可以帮助我,我应该在哪里插入代码以及如何编写代码。
这是 pdf 生成器的代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Createpdf extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library("Pdf");
}
public function create_pdf() {
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('lakmini');
$pdf->SetTitle('RCJ CONSTRUCTIONS');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// 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', '', 14, '', 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
<h1> this is my first pdf </h1>
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
//============================================================+
}
}
这是我的数据库table
CREATE TABLE IF NOT EXISTS `staff` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`designation_id` int(11) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`contact_no` int(11) NOT NULL,
`work_experience` varchar(250) NOT NULL,
`qualifications` varchar(250) NOT NULL,
`nic` varchar(10) NOT NULL,
`gender` varchar(10) NOT NULL,
`created` date NOT NULL,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
KEY `designation_id` (`designation_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=22 ;
我有一个 MPDF 库的代码首先你必须下载最新版本的 codeigniter MPDF 库 link (http://mpdf1.com/repos/MPDF60.zip) 然后解压 zip 文件并将 MPDF 库粘贴到 application/libraries 路径使用下面的代码 --
<a href="<?php echo base_url(); ?>controllerName/PDFFile(method name)">Generate PDF</a>
PDFFile 方法是--
public function PDFFile()
{
$data['results'] = $this->common_model->federationPDF_file();
// it will fetch data from database
$html = $this->load->view('Print_pdf',$data,true); // here we will pass view file name that contain table data
$this->load->library('MPDF/mpdf');
$mpdf=new mPDF('utf-8','A4', '12', '', 10, 10, 10, 10, 9, 9);
$mpdf->useOnlyCoreFonts = true;
$mpdf->SetProtection(array('print'));
$mpdf->SetTitle(SITE_NAME);
$mpdf->SetAuthor(SITE_NAME);
$mpdf->SetCreator(SITE_NAME);
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($stylesheet,1);
$search = array("<div class=\"row-fluid\">", "<div class=\"span6\">");
$replace = array("<div style='width: 100%;'>", "<div style='width: 100%; float: left;'>");
$html = str_replace($search, $replace, $html);
$name = "PDF File"; // file name
$mpdf->WriteHTML($html);
$mpdf->Output($name, 'D');
exit;
}
我的查看文件Print_pdf --
<!DOCTYPE html>
<html>
<head>
<title>PDF File</title>
</head>
<body>
<div align="center">
</div>
<h2 align="center">Table Data</h2>
<table border="1" align="center" cellpadding="1" cellspacing="0">
<thead>
<tr>
<td><b>S.No.</b></td>
<td><b>First Name </b></td>
<td><b>Last Name </b></td>
<td><b>Mobile</b></td>
<td><b>Address</b></td>
</tr>
</thead>
<tbody>
<?php $count = 1;
foreach($results as $s)
{ ?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $s['fname']; ?></td>
<td><?php echo $s['lname']; ?></td>
<td><?php echo $s['mobile']; ?></td>
<td><?php echo $s['address']; ?></td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</body>
</html>