感谢支持
我们一直在努力

JpGraph联合TCPDF动态生成图文并茂的PDF文件

基础环境:PHP5

因为jpgraph依赖于GD库,所以使用jpgraph之前需确认GD库是否已经安装。(GD库,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。)

如何确认GD库是否安装?方法如下:

在apache网站根目录(默认为/var/www)建立一个文件,如phpinfo.php,内容如下:

<%php
phpinfo()
%>

通过浏览器,访问此php文件,如:http://server_ip/phpinfo.php,若显示有如下内容表示GD库已经安装,若无则需先安装GD库。

如何安装gd库可参考:http://www.boutell.com/gd/

jpgraph

1. 下载jpgraph

http://jpgraph.net/download

2. 安装jpgraph

将下载的jpgraph解压后放到php能访问的目录,这里为/usr/share/php5/,在/usr/share/php5下建立一软连接指向jpgraph库的src目录

ln -s jpgraph-3.5.0b1/src jpgraph

更改php的配置文件/etc/php5/apache2/php.ini和/etc/php5/cli/php.ini,找到include_path项,取消注释并把/usr/share/php5加入到搜索目录,如:

include_path = “.:/usr/share/php5”

TCPDF 的详细介绍:请点这里
TCPDF 的下载地址:请点这里

相关阅读:使用 TCPDF 动态创建 PDF http://www.linuxidc.com/Linux/2013-09/90482.htm

3. 测试jpgraph

到http://jpgraph.net/上面随便找个例子,点击进去,将其源码拷贝出来,这里拷贝的是http://jpgraph.net/features/src/show-example.php?target=new_bar3.php

代码如下:

<?php // content=”text/plain; charset=utf-8″
require_once (‘jpgraph/jpgraph.php’);
require_once (‘jpgraph/jpgraph_bar.php’);

$datay=array(62,105,85,50);

// Create the graph. These two calls are always required
$graph = new Graph(350,220,’auto’);
$graph->SetScale(“textlin”);

//$theme_class=”DefaultTheme”;
//$graph->SetTheme(new $theme_class());

// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);

//$graph->ygrid->SetColor(‘gray’);
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array(‘A’,’B’,’C’,’D’));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// Create the bar plots
$b1plot = new BarPlot($datay);

// …and add it to the graPH
$graph->Add($b1plot);

$b1plot->SetColor(“white”);
$b1plot->SetFillGradient(“#4B0082″,”white”,GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(45);
$graph->title->Set(“Bar Gradient(Left reflection)”);

// Display the graph
$graph->Stroke();
?>

在服务器根目录新增文件jpgraph.php,将拷贝的代码粘贴上去,保存后通过浏览器访问,看到如下图:

因为我的目的是要制作pdf文件,所以需要把生成的图片保存成文件,把代码封装修改一下,制作成一个脚本文件,如下:

#!/usr/bin/php -q
<?php // content=”text/plain; charset=utf-8″
require_once (‘jpgraph/jpgraph.php’);
require_once (‘jpgraph/jpgraph_bar.php’);
require_once (‘fpdf/fpdf.php’);

$datay=array(62,105,85,50);

// Create the graph. These two calls are always required
$graph = new Graph(350,220,’auto’);
$graph->SetScale(“textlin”);

//$theme_class=”DefaultTheme”;
//$graph->SetTheme(new $theme_class());

// set major and minor tick positions manually
$graph->yaxis->SetTickPositions(array(0,30,60,90,120,150), array(15,45,75,105,135));
$graph->SetBox(false);

//$graph->ygrid->SetColor(‘gray’);
$graph->ygrid->SetFill(false);
$graph->xaxis->SetTickLabels(array(‘A’,’B’,’C’,’D’));
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);

// Create the bar plots
$b1plot = new BarPlot($datay);

// …and add it to the graPH
$graph->Add($b1plot);

$b1plot->SetColor(“white”);
$b1plot->SetFillGradient(“#4B0082″,”white”,GRAD_LEFT_REFLECTION);
$b1plot->SetWidth(45);
$graph->title->Set(“Bar Gradient(Left reflection)”);

// Display the graph
//$graph->Stroke();
$graph->Stroke(“/tmp/test1.png”);
?>

给上述文件添加执行权限后,可在/tmp/下生成test1.png图片

tcpdf
1. 下载tcpdf
http://sourceforge.net/projects/tcpdf/files/

2. 安装tcpdf
解压tcpdf压缩包到/usr/share/php5目录

因为安装jpgraph时已经将/usr/share/php5目录添加到php的搜索路径中,所以这里就不必修改php.ini了

3. 测试tcpdf
建立文件tcpdf,内容如下:

#!/usr/bin/php -q
<?
require_once(‘tcpdf/config/lang/eng.php’);

require_once(‘tcpdf/tcpdf.php’);

// 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(‘Nicola Asuni’);

$pdf->SetTitle(‘TCPDF Example’);

$pdf->SetSubject(‘TCPDF Tutorial’);

$pdf->SetKeywords(‘TCPDF, PDF, example, test, guide’);

// remove default header/footer

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

// set default header data

$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// 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

$pdf->setLanguageArray($l);

// ———————————————————

// add a page

$pdf->AddPage();

// set JPEG quality

$pdf->setJPEGQuality(75);

// Image example
$pdf->Image(‘/tmp/test1.png’, 50, 50, 100, 100, ”, ‘http://www.tcpdf.org’, ”, true, 150);
$pdf->Cell( 0, 0, ‘TCPDF test’, 0, 1 );;

// ———————————————————

//Close and output PDF document

$pdf->Output(‘example.pdf’, ‘D’);
?>

给上述文件添加执行权限然后执行,生成pdf如下:

这里只是简单测试,把jpgraph动态生成的图形包含到pdf,若要把pdf整的更加美观,需详细查看jpgraph和tcpdf库。

赞(0) 打赏
转载请注明出处:服务器评测 » JpGraph联合TCPDF动态生成图文并茂的PDF文件
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏