查看: 928|回复: 3
|
用php来做pdf档案。
[复制链接]
|
|
最近发现到一个蛮好玩的东西。就是fpdf 这个php class。
反正我又有报告要作,所以用来试试看。
第一:
需要有一个database schema。我是用mysql。
CREATE TABLE `log` (
`log_key` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`day` VARCHAR( 255 ) NOT NULL ,
`week` INT NOT NULL ,
`task` TEXT NOT NULL
) ENGINE = MYISAM ;
第二:
需要有个form。这里我用html_quickform。来做这个form。
<?php
/*
* Created on Jun 28, 2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
require('HTML/QuickForm.php');
$form=new HTML_QuickForm('rptForm','get');
$form->AddElement('select','week','week',array("1","2","3","4","5","6","7","8","9","10","11","12","13","14"));
$form->AddElement('select','day','day:',array("monday","tuesday","wednesday","thursday","friday"));
$form->AddElement('textarea','task','task:','rows="5",cols="50"');
$form->AddElement('submit','add','process');
$form->process('process');
$form->display();
function process($value){
$day=$value['day'];
$task=$value['task'];
mysql_connect('localhost','username','**********');
mysql_select_db('log');
$queryins="insert into log(day,task)values('$day','$task');";
$query=mysql_query($queryins);
}
?>
最后,才用fpdf来做个pdf档案。
<?php
/*
* Created on Jun 28, 2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include ("fpdf.php");
$week=$_POST['week'];
$pdf=& new FPDF();
mysql_connect("localhost","username","***********");
mysql_select_db("log");
$pdf->AddPage();
$pdf->SetFont('Helvetica','',14);
$weekstr="week $week";
$pdf->Write(5,$weekstr);
$pdf->Ln(5);
$pdf->SetFont('Helvetica','',14);
$pdf->Cell(40,7,'day',1);
$pdf->Cell(80,7,'task',1);
$pdf->Cell(40,7,'comments',1);
$querystr="select day,task from log where week='$week'";
$result=mysql_query($querystr);
while($row=mysql_fetch_array($result)){
$pdf->Cell(40,7,$row['day']);
$pdf->Cell(80,7,$row['task']);
$pdf->Cell(40,7,'');
$pdf->Ln();
}
$pdf->Write(5,"prepared by,");
$pdf->Output();
exit;
?>
fpdf可以在这里找到:http://www.fpdf.org/
[ 本帖最后由 sweemeng 于 29-6-2006 01:42 PM 编辑 ] |
评分
-
查看全部评分
|
|
|
|
|
|
|

楼主 |
发表于 30-6-2006 12:01 PM
|
显示全部楼层
发现到我的script 有问题。
第一schema table名字不可以是log. 所以用logger.
CREATE TABLE `logger` (
`log_key` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`day` VARCHAR( 255 ) NOT NULL ,
`week` INT NOT NULL ,
`task` TEXT NOT NULL
) ENGINE = MYISAM ;
第二input.php有改。
<?php
/*
* Created on Jun 28, 2006
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
require('HTML/QuickForm.php');
$form=new HTML_QuickForm('rptForm','post');
$form->AddElement('select','week','week',array("1"=>1,"2"=>2,"3"=>3,"4"=>4,"5"=>5,"6"=>6,"7"=>7,"8"=>8,"9"=>9,"10"=>10,"11"=>11,"12"=>12,"13"=>13,"14"=>14));
$form->AddElement('select','day','day:',array("monday"=>"monday","tuesday"=>"tuesday","wednesday"=>"wednesday","thursday"=>"thursday","friday"=>"friday" );
$form->AddElement('textarea','task','task:','rows="5",cols="50"');
$form->AddElement('submit','submit','process');
if($form->validate()){
$form->process('process');
}
$form->display();
function process($value){
$day=$value['day'];
$task=$value['task'];
$week=$value['week'];
mysql_connect('localhost','root','hacker');
mysql_select_db('log');
$queryins="insert into logger(week,day,task)values('$week','$day','$task')";
echo $queryins;
$query=mysql_query($queryins);
}
?>
最后在有用到fpdf的script把这个$querystr="select day,task from log where week='$week'";
改去 $querystr="select day,task from logger where week='$week'";
[ 本帖最后由 sweemeng 于 30-6-2006 12:05 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 1-7-2006 11:32 PM
|
显示全部楼层
我觉得也许我走的太快。。。
所以我在这里解释整样用fpdf。
因为我是在xampp里发现到这个class,因为xampp本身已经有fpdf。所以不知道整样装fpdf。
用fpdf之前必须注意,在用fpdf里不能有output。里面不能有echo,print等keyword。
我们可以开始咯。
我会用这个script来做例子:
1)include ("fpdf.php");
2)$pdf=& new FPDF();
3)$pdf->AddPage();
4)$pdf->SetFont('Helvetica','',14);
5)$pdf->Write(5,"my first pdf");
6)$pdf->Ln();
8)$pdf->Cell(40,7,"first cell",1);
9)$pdf->Cell(40,7,"second cell",1);
10)$pdf->Output();
11)exit;
?>
1)如果要用fpdf。当然要include("fpdf.php"),
2)然后用new弄个fpdf object。
3)$pdf->addpage()呢是要做一个pdf page
4)$pdf->SetFont('Helvetica','',14) 是要选一个font 和size;
5)$pdf->Write(5,"my first pdf") 是用来写需要output的string.;你可以把my first pdf改去你有写的data
6)$pdf->Ln() 是要下一行;
8) 和 9) $pdf->Cell(40,7,"first cell",1) 是用来写成像excel着样。你可以把first cell改去你有写的data.
10)$pdf->Output(); 是把pdf output出来。 |
|
|
|
|
|
|
|
发表于 2-7-2006 11:03 PM
|
显示全部楼层
其實你可以把一些 fpdf 裡面的 function 自己重新 '包裝' 過,
讓你在做 pdf 時更方便. |
|
|
|
|
|
|
| |
本周最热论坛帖子
|