Generate graphs in PDF file using TCPDF

What have you tried picture What have you tried · Aug 19, 2013 · Viewed 11.7k times · Source

There are tons of topics on this, none of which have solved my issue. What I'd like to do is simple - generate a bar graph and then embed this graph into a pdf file that I will be generating with a library called TCPDF.

I'm having no problems generating HTML content using TCPDF but when it comes to generating the graph and including it in the pdf file, I'm having all kinds of issues.

Generating the graph

I'm creating the graph using a library called svggraph. Generating the graph is very simple, the only problem is that there are headers being sent through the inclusion of the main class file. When headers are sent, TCPDF cannot generate the PDF document.

My setup now:

generatereport.php - TCPDF generates the pdf document on this page graph.php - SVGGraph generates the bar graph on this page

I've tried:

  • file_get_contents('graph.php') from generatereport.php - nothing is being output in the pdf report when I use the built in writeHTML function that TCPDF offers
  • require_once('graph.php') - headers already sent error
  • echo file_get_contents('graph.php') - Headers already sent, but that was expected. The good news is that the graph was displayed properly.

Goal (What I'd like to happen) TCPDF has a built in ImageSVG function that is used for this exact purpose. The first parameter can take a XML string of SVG data; the problem here is that I can't figure out how to return XML data from the graph.php page (I've read every documentation page I could find).

Does anyone have any experience using either of these two libraries?

Thanks!

Edit: Some code

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);
}

The @ symbol tells the function that XML data is being sent to it, as opposed to an SVG file.

Answer

Vrashabh Irde picture Vrashabh Irde · Aug 27, 2013

Use fetch - See below

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

and then feed it to TCPDF (Since fetch without options generates the XML declaration and doctype) This should generate $output of the format:

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

Feed it like this

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

In line with the comment above from $VSOverFlow.

Of course you can also save the output in a file and then provide the path to the file like so

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