html2canvas - 在不显示页面的情况下渲染图片。

问题描述 投票:-1回答:1

我有一个$_POST的php API,我从请求中获取API密钥和QR代码,然后我的php服务器生成html代码。<div>, <img> <html> <body> 等......这意味着完整的网站 - 从mysql服务器获取数据并渲染到html页面.在php代码的最后,我有这样的javascript代码。

?>
<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>

当我显示页面时,图片被保存(使用html2canvas js),一切正常。

问题是,当我从另一个php代码中调用这个API请求时,页面没有显示,因此图片没有呈现。

有没有什么方法可以在不显示生成的html代码的情况下,渲染html页面并保存图片?

我修改了一下代码--没有$_POST ,而是$_GET,用这段代码测试。

<?php
    $key='my_api_key';
    $qrcode='my_code';

    $test = file_get_contents('https://myserver/API/apipage.php?key='.$key.'&qrcode='.$qrcode);
    //var_dump($test) ;
?>

当var_dump从代码中移除时,页面没有呈现,图片也没有保存。当我启用var_dump(或简单的print_r或echo)时,图片正确生成。问题是,我不想显示html代码(我认为这是正常的,当我谈论API调用)。

谢谢你的任何提示

php api html2canvas
1个回答
0
投票

好吧,我的解决方案如下。

在我的Linux服务器上安装了xvfb。

为php安装了ssh2

在我的linux服务器上安装了CutyCapt。

我没有直接调用和渲染图片--我是用专门创建的用户(只具有做这项工作的权限)直接从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}"

所以我知道,没有简单的方法,但它是可行的。

希望对大家有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.