Skip to content

Instantly share code, notes, and snippets.

@rahulvramesh
Created November 9, 2021 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulvramesh/5e82e70bef24314f4cb7e36d1e55340e to your computer and use it in GitHub Desktop.
Save rahulvramesh/5e82e70bef24314f4cb7e36d1e55340e to your computer and use it in GitHub Desktop.
DOMphp + image
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('allow_url_fopen', 1);
require 'vendor/autoload.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isRemoteEnabled', true);
$options->setTempDir('temp');
$dompdf = new Dompdf($options);
$path = base64_encode(file_get_contents("https://quickchart.io/chart?c={type:%27bar%27,data:{labels:[%27Q1%27,%27Q2%27,%27Q3%27,%27Q4%27],%20datasets:[{label:%27Users%27,data:[50,60,70,180]},{label:%27Revenue%27,data:[100,200,300,400]}]}}"));
// HTML Content
$html = <<<EOD
<html>
<head>
</head>
<body>
<img src="data:image/png;base64,{$path}" width="300px" height="300px"> </img> </body>
</body>
</html>
EOD;
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment