
第 1 步:创建数据库表
在 mysql 数据库中创建用户表:
create table users ( id int auto_increment primary key, name varchar(50) not null, surname varchar(50) not null, email varchar(100) not null unique, university varchar(100), created_at timestamp default current_timestamp);
第 2 步:创建用户模型
在您的 codeigniter 项目中,在 app/models/:
中创建一个名为 usermodel.php 的模型
<?phpnamespace appmodels;use codeignitermodel;class usermodel extends model{ protected $table = 'users'; protected $primarykey = 'id'; protected $allowedfields = ['name', 'surname', 'email', 'university']; protected $usetimestamps = true; protected $createdfield = 'created_at';}
第 3 步:安装 dompdf
在项目目录中运行以下命令来安装 dompdf:
composer require dompdf/dompdf
第 4 步:创建 pdf 生成控制器
在 app/controllers/:
中创建一个名为 pdfcontroller.php 的控制器
findall(); $options = new options(); $options->set('isremoteenable',true); $dompdf = new dompdf($options); $html = view('user_list_pdf',['users' => $users]); $dompdf->loadhtml($html); $dompdf->render(); $filename = 'user_list_'.date('ymdhis').'pdf'; $dompdf->stream($filename,['attachment'=>false]); }}
第 5 步:为 pdf 创建 html 视图
在 app/views/ 中,创建一个名为 user_list_pdf.php 的新文件,其中包含以下内容:
table { border-collapse: collapse; width: 100%; } th, td { border: 1px solid black; padding: 8px; } th { background-color: lightgray; } list of users
| no | name and surname | university | created date | |
|---|---|---|---|---|
第 6 步:定义路线
在app/config/routes.php中,添加访问pdf生成功能的路由:
$routes->get('generate-user-list-pdf', 'PdfController::generateUserListPdf');
第 7 步:测试设置
在浏览器中访问 http://yourdomain.com/generate-user-list-pdf。这应该:
从数据库中检索所有用户。
使用用户数据渲染 user_list_pdf 视图。
使用用户列表生成 pdf 并将其显示在浏览器中。
以上就是如何在 PHP CodeIgniter 中生成 Pdf sing *dompdf*的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1249543.html
微信扫一扫
支付宝扫一扫