Reputation: 19
How to insert a graph of JpGraph inside a PDF from FPDF.
Hello, I'm programming the final project of my graduation so this is very important to me, in PHP and i'm using two library of code one is FPDF and the other is JpGraph. I want insert the graph generated by JpGraph inside an PDF generated by FPDF.
When I'm using HTML it's easy, I need just write: <img src="mygraph.php">
And "mygraph.php" generate the graph and insert it inside my HTML Page.
But to insert a Image inside a PDF of FPDF Library, I need to use de the following code: $pdf->Image("image.jpg");
So I imagined that a just need the following line of code to insert the graph inside the PDF, the line is: $pdf->Image("mygraph.php");
But when I do that, php prints the following line of Error: FPDF error: Unsupported image type: php
Of Course everything should be dynamic and should be calculated in real time.
So please help me, how can i make this work? How can i solve this problem?
Upvotes: 1
Views: 3940
Reputation: 19
Thank you for your attention, Pedro:
I made what you said, but now the php is printing all the code of mygraph.php
I don't know realy well how to use this line of code header('Content-type: image/jpeg');
So i put it in the mygraph.php file, now the file is in this way:
<?php
header('Content-type: image/jpeg');
require_once("graph/jpgraph.php");
require_once("graph/jpgraph_line.php");
$graph = new Graph(1300, 400);
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->SetMargin(80,20,40,40);
$graph->title->Set("Tempo Medio das Consultas");
$graph->xaxis->title->Set('Meses');
$graph->yaxis->title->Set('Tempo Medio em Minutos');
$graph->yaxis->SetTitleMargin(45);
$lineplot->SetColor("orange");
$lineplot->SetWeight(3);
$graph->Stroke();`
?>
And the line of code that calls the file is in this way:
$pdf->Image(file_get_contents("mygraph.php"));
And the php is printing the following:
FPDF error: Unsupported image type: php"); $graph = new graph(1300, 400);
$graph->setscale("textlin"); $graph->setshadow();
$graph->setmargin(80,20,40,40); $graph->title->set("tempo medio das consultas"); $graph->xaxis->title->set('meses');
$graph->yaxis->title->set('tempo medio em minutos');
$graph->yaxis->settitlemargin(45); $lineplot->setcolor("orange");
$lineplot->setweight(3); $graph->stroke(); ?>
Upvotes: 0
Reputation: 98921
You may want to try:
$pdf->Image(file_get_contents("http://yousite/mygraph.php"));
Make sure you set the correct header
on mygraph.php
to match the image mime type, i.e.:
header('Content-type: image/jpeg');
Upvotes: 1