Create PDF charts using TCPDF

There are many topics in this, none of which solved my problem. What I would like to do is simply generate a histogram, and then paste this graph into a pdf file, which I will generate using the TCPDF library.

I have no problem creating HTML content using TCPDF, but when it comes to creating a graph and including it in a pdf file, I have all kinds of problems.

Graph generation

I am creating a graph using the svggraph library. Generating a graph is very simple, the only problem is that the headers are sent via the inclusion of the main class file. When headers are sent, TCPDF cannot create a PDF document.

My setup now:

generatereport.php - TCPDF creates a pdf document on this page graph.php - SVGGraph generates a histogram on this page

I tried:

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

Target (what I would like) TCPDF has a built-in ImageSVG function that is used for this purpose. The first parameter can accept an SVG XML data string; the problem is that I cannot figure out how to return the XML data from the graph.php page (I read every page of the documentation I could find).

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

Thanks!

Edit: 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 indicates to the function that XML data is sent to it, unlike an SVG file.

+6
source share
1 answer

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 pass it to TCPDF (since fetch without parameters generates an XML declaration and doctype) This should generate $output format $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> 

Fill it as follows

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

According to the comment above from $ VSOverFlow.

Of course, you can also save the output to a file, and then specify the path to the file, for example:

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

Source: https://habr.com/ru/post/951983/


All Articles