Skip to content

Instantly share code, notes, and snippets.

@karamansky
Last active December 26, 2022 13:17
Show Gist options
  • Save karamansky/8be97a066a95f08612d1fd156b91f5f9 to your computer and use it in GitHub Desktop.
Save karamansky/8be97a066a95f08612d1fd156b91f5f9 to your computer and use it in GitHub Desktop.
// reference the Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;
$text_data = [
'<p><b>Analyst:</b> <span>SEAN WIELAND</span> (PIPER SANDLER)</p>',
'<p><b>The Stock:</b> <span>CHNG</span> (Change Healthcare Inc)</p>',
'<p><b>Price Target Change:</b> $494->$549 = $55 (11.13%)</p>',
'<p><b>Average Time For PT To Be Met:</b> 251</p>',
'<p><b>Potential Upside Change:</b> $109.6->$164.6; 198%->133%</p>',
'<p><b>Change Price Target Met Ratio:</b> 18/22 (81.82%)</p>',
];
$font_size = 20;
$font_height = imagefontheight($font_size) * 2; //2 - line-height
//pt + image height + ( line-height * line count ) + pb
$full_height = 20 + 100 + ($font_height * count($text_data)) + 20;
$html_img = '<!DOCTYPE html><html lang="en"><head><title>PDF</title>
<style>
@page{margin: 0; font-family: Lato, sans-serif;}
body {margin: 0;}
b{font-weight: bold;}
p{font-size: '. $font_size .'px; color: #000; line-height: '. $font_size .'px;}
span{display: inline; color: #0A7CFC; font-weight: bold;}
img{margin-top: 0; margin-bottom: 0;}
.twitter-img-block{width: 100%; max-width: 100%; height: 100%; position: relative; background: rgb(248, 250, 255); padding: 20px;}
.analyst-img{border-radius: 50%;}
.logo{position: absolute; right: 60px; top: 20px;}
</style>
</head>
<body>
<div class="twitter-img-block">
<img src="'. get_stylesheet_directory_uri() .'/assets/test/head.jpg" class="analyst-img" style="width: 100px; height: 100px;" alt="" >
<img src="'. get_stylesheet_directory_uri() .'/assets/test/logo.png" class="logo" alt="" >';
foreach ( $text_data as $key => $line ){
$html_img .= trim($line);
}
$html_img .= '
</div>
</body>
</html>';
$html_img = trim(str_replace(array("\n", "\r"), '', $html_img));
$options = new Options();
$options->set('defaultFont', 'Lato');
$options->set('isHtml5ParserEnabled', true);
$options->set('isRemoteEnabled', true);
// instantiate and use the dompdf class
$dompdf = new Dompdf($options);
$dompdf->loadHtml($html_img);
// (Optional) Setup the paper size
$custom_paper = [0,0,420,$full_height];
$dompdf->setPaper($custom_paper);
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (open or download PDF file)
// $dompdf->stream();
//Save pdf file on server
$pdf = $dompdf->output();
file_put_contents(__DIR__ . '/file.pdf', $pdf);
$img = new Imagick();
$img->setResolution(72,72);
$img->readimage(__DIR__ . '/file.pdf[0]');
$img->setImageFormat('jpeg');
$img->writeImage(__DIR__ .'/sk__twitter.jpg');
$img->clear();
$img->destroy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment