I have php API with $_POST , where i am getting API key and QR code from the request.
Then my php server generates the html code with <div>, <img> <html> <body>
etc.. it means full website - getting data from the mysql server and rendering to html page.
At the end of the php code i have javascript code like this:
?>
<script>
window.scrollTo(0, 0);
html2canvas(document.getElementById("SelectorToPrint")).then(function (canvas) {
var dd= canvas.toDataURL("image/jpeg",1);
$.ajax({
type: "POST",
url: "https://myserver/API/sendimg.php",
data: {
key: '<?php echo $key;?>',
barkod: '<?php echo $qrcode;?>',
img: dd
}
}).done(function(o) {
});
});
</script>
When i am displaying the page, the picture is being saved (using html2canvas js) , everything is OK.
The problem is, when i am calling this API request from another php code, the page is not displaying, and for this reason the picture is not rendered.
Is there some way to render html page and save a picture without displaying the generated html code?
I changed the code for a while - to not have $_POST , but $_GET and testing with this code:
<?php
$key='my_api_key';
$qrcode='my_code';
$test = file_get_contents('https://myserver/API/apipage.php?key='.$key.'&qrcode='.$qrcode);
//var_dump($test) ;
?>
when the var_dump is removed from the code, the page is not rendered and the picture is not saved. When i enable the var_dump (or simply print_r or echo ) the picture is generated correctly. The problem is, that i dont want to display the html code (i think it's normal when i am talking about API calls).
Thanks for any hint
OK so my solution was the following:
installed xvfb on my linux server
installed ssh2 for php
installed CutyCapt on my linux server
instead of calling and rendering picture directly - i am calling with specially created user (with privileges only to do this job) the following script directly from php API
"/usr/bin/xvfb-run --server-args='-screen 0, 1920x1080x24' /usr/bin/CutyCapt --url='{$url}' --zoom-factor=1.4 --out=/home/myuser/public_html/rendered_images/{$filename}.{$extension}"
so as i learnt, there is no easy way to do it, but it's doable
hope it helps for someone