使用TCPDF在PDF文件中生成图形

时间:2013-08-18 23:13:43

标签: tcpdf svggraph

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

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

生成图表

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

我现在的设置:

generatereport.php - TCPDF在此页面上生成pdf文档 graph.php - SVGGraph在此页面上生成条形图

我试过了:

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

目标(我想要发生的事情) TCPDF具有内置ImageSVG函数,用于此目的。第一个参数可以采用SVG数据的XML字符串;这里的问题是我无法弄清楚如何从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文件。

1 个答案:

答案 0 :(得分:6)

使用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声明和doctype) 这应该生成$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);
相关问题