2013-08-18 48 views
6

有很多话题都没有解决我的问题。我想要做的事情很简单 - 生成一个条形图,然后将此图形嵌入到一个pdf文件中,我将使用名为TCPDF的库生成该文件。使用TCPDF生成PDF文件中的图形

我没有问题使用TCPDF生成HTML内容,但是当涉及到生成图并将其包含在pdf文件中时,我遇到了各种问题。

生成图表

我创建使用一种称为svggraph库中的图形。生成图形非常简单,唯一的问题是通过包含主类文件发送头文件。发送标题时,TCPDF无法生成PDF文档。

我现在的设置:

generatereport.php - TCPDF产生这个 页graph.php PDF文档 - SVGGraph生成此页面上的条形图

我已经试过:

  • file_get_contents('graph.php') from generatereport.php - 当我使用内置的writeHTML功能时,pdf报告中没有内容输出,TCPDF提供
  • require_once('graph.php') - 标题已发送错误
  • echo file_get_contents('graph.php') - 标题已经发送,但这是预期的。好消息是图表正确显示。

目标(我想什么发生) TCPDF有一个内置的是用于此确切目的ImageSVG功能。第一个参数可以是一个XML字符串的SVG数据;这里的问题是,我无法弄清楚如何从graph.php页面返回XML数据(我已经阅读了我能找到的每个文档页面)。

有没有人有任何使用这两个库的经验?

谢谢!

编辑:一些代码

Graph.php:

<?php 
require_once 'svggraph/SVGGraph.php'; 
$graph = new SVGGraph(500, 400); 
$graph->Values(1, 4, 8, 9, 16, 25, 27); 
$graph->Render('LineGraph', true, true) 
?> 

generatereport.php

$html = file_get_contents('http://localhost:8080/vu/graph.php'); 

if(!empty($file)){ 
//$pdf->Write(0, $html, '', 0, 'L', true, 0, false, false, 0); 
//$pdf->writeHTML($html, true, false, true, false, ''); 
$pdf->ImageSVG('@' . $html, $x=15, $y=30, $w='', $h='', $link='http://www.tcpdf.org', $align='', $palign='', $border=1, $fitonpage=false); 
} 

@符号告诉XML数据被发送给它的功能,而不是SVG文件。

+0

我不是将其作为一个答案,因为我还没有尝试过。 http://www.goat1000.com/svggraph-using.php第4节讨论Render()调用的两个选项来抑制某些返回值。更重要的是,有一个$ Fetch函数可以生成图形,而不会将它发送给浏览器。这可能允许您保存SVG文件并读取ImageSVG命令。另外http://www.tcpdf.org/doc/code/classTCPDF.html#a56536508fb1b5aede7d2ed27f56c2353表明@选项需要实际的SVG数据字符串。 – VSOverFlow

回答

4

使用fetch - 见下面

<?php 
require_once 'svggraph/SVGGraph.php'; 
$graph = new SVGGraph(500, 400); 
$graph->Values(1, 4, 8, 9, 16, 25, 27); 
$output = $graph->fetch('LineGraph'); 
?> 

,然后将其输送到TCPDF(由于fetch不带选项生成的XML声明和文档类型) 这应该生成格式的$output

<svg style="overflow: hidden; position: relative;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1226" version="1.1" height="826"><image transform="matrix(1.0364,-0.3305,0.3305,1.0364,-41.846,108.0143)" preserveAspectRatio="none" x="10" y="10" width="205" height="154" xlink:href="wallpaper.jpg" opacity="1" stroke-width="1"></image><rect transform="matrix(1.0364,-0.3305,0.3305,1.0364,-41.846,108.0143)" x="165" y="114" width="50" height="50" r="0" rx="0" ry="0" fill="#C0C0C0" stroke="#000" opacity="0" stroke-width="1"></rect><image transform="matrix(1.1575,0.2385,-0.2385,1.1575,-442.1395,-145.4163)" preserveAspectRatio="none" x="500" y="10" width="205" height="154" xlink:href="wallpaper.jpg" opacity="1" stroke-width="1"></image><rect transform="matrix(1.1575,0.2385,-0.2385,1.1575,-442.1395,-145.4163)" x="655" y="114" width="50" height="50" r="0" rx="0" ry="0" fill="#C0C0C0" stroke="#000" opacity="0" stroke-width="1"></rect></svg> 

像这样喂它

$pdf->ImageSVG('@' . $output, $x=15, $y=30, $w='', $h='', $link='http://www.tcpdf.org', $align='', $palign='', $border=1, $fitonpage=false); 

符合$ VSOverFlow中的上述注释。

当然,你也可以输出保存到一个文件,然后提供文件路径,像这样

$pdf->ImageSVG($file='images/file.svg', $x=15, $y=30, $w='', $h='', $link='', $align='', $palign='', $border=0, $fitonpage=false); 
+0

然而,接受这个答案在技术上并不正确。将编辑答案只是一个接触,使未来的观众很好。 –

+0

糟糕对不起一些快速打字和忽略错误。感谢编辑。 – Slartibartfast

+0

获取方法不包括图表图例。有没有办法来解决这个问题?我很确定我给SvgGraph btw的传奇选项。 – Soroush