2

I want to generate a PDF file with some information on it, then send it to some email address. The information inside the PDF will contain among other stuff, a PIE that I generate using jpgraph. Of course, the chart will be dynamic, so it's not an option to generate a static PIE and use it as a static image. For each PDF I generate, the pie will be different. But I have no idea on how to do it (how to include it in tcpdf). This is my chart:

<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_pie.php');

$sta = $_REQUEST['start'];
$sto = $_REQUEST['end'];

// Some data
$data = array($sta,$sto);
$color   = array('#2ecc71','#e74c3c');

// A new pie graph
$graph = new PieGraph(400,400,'auto');

// Don't display the border
$graph->SetFrame(false);

// Uncomment this line to add a drop shadow to the border
// $graph->SetShadow();

// Setup title
$graph->title->Set("Procentaj realizat");
$graph->title->SetFont(FF_FONT1,FS_BOLD,18);
$graph->title->SetMargin(8); // Add a little bit more margin from the top

// Create the pie plot
$p1 = new PiePlotC($data);

// Set size of pie
$p1->SetSize(0.35);

// Label font and color setup
$p1->value->SetFont(FF_FONT1,FS_BOLD,12);
$p1->value->SetColor('white');

$p1->value->Show();

// Setup the title on the center circle
$p1->midtitle->Set("Procent\nraspunsuri corecte\n83%");
$p1->midtitle->SetFont(FF_FONT1,FS_NORMAL,14);

// Set color for mid circle
$p1->SetMidColor('white');

// Use percentage values in the legends values (This is also the default)
$p1->SetLabelType(PIE_VALUE_PER);

$p1->SetSliceColors($color);

// The label array values may have printf() formatting in them. The argument to the
// form,at string will be the value of the slice (either the percetage or absolute
// depending on what was specified in the SetLabelType() above.
$lbl = array("corecte\n%.1f%%","gresite\n%.1f%%");
$p1->SetLabels($lbl);

// Uncomment this line to remove the borders around the slices
// $p1->ShowBorder(false);

// Add drop shadow to slices
$p1->SetShadow();

// Explode all slices 15 pixels
$p1->ExplodeAll(15);

// Add plot to pie graph
$graph->Add($p1);

// .. and send the image on it's marry way to the browser
$graph->Stroke();

?>

So the above code is in a file called mychart.php. I want to include into a tcpdf file the result of this. How can I do this?

It's not mandatory that I use jpgraph. I see that google charts look quite perfect and seem easy to use. I could use this:

<html>
  <head>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {packages:['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['Corecte', 83]          
        ]);

        var options = {
          width: 400, height: 250,
          redFrom: 90, redTo: 100,
          yellowFrom:75, yellowTo: 90,
          minorTicks: 5
        };

        var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id='chart_div'></div>
  </body>
</html>

However, when including into a tcpdf... they still have a problem... I mean, I heave to add a script inside the HTML of the tcpdf, and the problem is that it seems tcpdf only takes the body tag ... So what can I do? Please help. Thanks

user1137313
  • 2,390
  • 9
  • 44
  • 91

1 Answers1

2

I recently ran into the same requirement for graphs embedded in TCPDF. Click here to see my code.

Explanation:

What you would need to do is capture the output of JPGraph using ob_start() and ob_end_clean() and then use TCPDF::Image() to display the graph, prefixed by the @ symbol (which tells TCPDF that it is dealing with an image data stream and not an image file name). You can see more about this in the TCPDF examples.

You will probably need to play around with the graph dimensions as well as the TCPDF::setImageScale() and TCPDF::setJPEGQuality() methods to get the graph to render sharper. I found that values of 1.7 for the scale and 80 for the quality provided a good balance.

BeesonBison
  • 1,053
  • 1
  • 17
  • 27