How to take a screenshot or make a image of current screen using php and javascript with jquery or make a pdf file of current scrren

For create a image of current screen using javascript add below script into your file

<script type="text/javascript">
function printDiv()
{
    jQuery('.year_box').remove();
// add your container id for capcture screen here i write "main_panel_body" for example
    html2canvas([document.getElementById('main_panel_body')], {  
        onrendered: function(canvas) 
        {
            var img = canvas.toDataURL();
            jQuery('.overlay_box').show();
// calling ajax method for create image from binary data
            $.post("save.php", {data: img}, function (file) {
                jQuery('.overlay_box').hide();
            window.open("download.php?business_idsv="+business_id+"&limit="+limit+"&path="+ file,'_blank');});  
        }
    });
    jQuery('.year_box').show();        
}
</script>

Here is my save.php file

require_once("config.php");
$data = $_POST['data'];
$file =  'tempimage.png'; // here creating a temporary file

// remove "data:image/png;base64,"
$dir_to_save = "tempimage/";

$uri =  substr($data,strpos($data,",")+1);

// save to file
file_put_contents($dir_to_save.$file, base64_decode($uri));


echo $dir_to_save.$file; exit;

// save.php file end

Below is my download.php file for serve image file

<?php
$file = trim($_GET['path']);

// force user to download the image
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: image/png');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    unlink($file);
    exit;
}
else {
    echo "$file not found";
}

If you want to create a pdf file and add image into pdf file then use below script in download.php file

ob_start();
?>
<div class="page-top">
   Pdf report
   </div>
   <div class="image"><img src="<?php echo $file; ?>" width="750" /></div>
<?php
$html = ob_get_clean();
// here i use html2pdf library for create a pdf file
require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
    $html2pdf = new HTML2PDF('P','A4','en',true,'UTF-8',array(5, 5, 5, 8));
    $html2pdf->WriteHTML($html);
    $html2pdf->Output($business_info['business_name'].'.pdf','I');
ob_clean();
    flush();
    readfile($file);
    unlink($file);
    exit();

Comments

Popular posts from this blog

How to add image option in nav menu in wordpress

How to change user id on checkout page for assign order to different user in woocommerce wordpress

How to add a custom sorting or order by option on category archive or product archive page in woocommerce wordpress