问题描述
我是一个新的codeigniter和tcpdf。现在我想显示我的数据库表行在pdf。我不知道如何做到这一点。
i'm a new one for codeigniter and tcpdf. now i want to show my database table row in pdf. i don`t know how to do that. any one can help me please where i should insert the code and how to write the code.
这是pdf生成器的代码
this is the code for pdf generator
<?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
//============================================================+
}
}
这是我的db表
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库链接()最新版本然后解压zip文件并粘贴MPDF库在应用程序/库路径上使用以下代码 -
i have a code of MPDF library first you have to download codeigniter MPDF library link (http://mpdf1.com/repos/MPDF60.zip) with latest version then extract zip file and paste MPDF library on application/libraries path the use below code --
<a href="<?php echo base_url(); ?>controllerName/PDFFile(method name)">Generate PDF</a>
PDFFile方法是 -
PDFFile Method is --
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 -
My View File 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>
这篇关于如何查看tcpdf中的表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!