【发布时间】:2015-10-24 23:13:03
【问题描述】:
我有一个生成图表的页面。我有一个为其生成pdf报告的按钮。我想创建这个图表的图像并将其插入到 pdf 中。为了创建图像,我使用html2canvas 并获取我存储在localstorage 中的dataurl。
chart.php
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#download').click(function() {
$.ajax({
type: "POST",
url: "pdfGen.php",
data: 'hello',
success: function(data) {
alert("hi");
}
});
});
}); //END $(document).ready()
</script>
<script>
//<![CDATA[
(function() {
window.onload = function(){
html2canvas(document.getElementById('chart'), {
"onrendered": function(canvas) {
var img = new Image();
img.onload = function() {
img.onload = null;
console.log(canvas.toDataURL("image/png"));
window.localStorage.setItem("imgURL", canvas.toDataURL("image/png"));
};
img.onerror = function() {
img.onerror = null;
if(window.console.log) {
window.console.log("Not loaded image from canvas.toDataURL");
} else {
//alert("Not loaded image from canvas.toDataURL");
}
};
img.src = canvas.toDataURL("image/png");
}
});
};
})();
//]]>
</script>
<body>
<a href="pdfGen.php" id="download" class="button">Report</a>
..more code to generate the chart
</body>
下载按钮调用使用 fpdf 生成报告的 pdfGen.php 脚本。 pdfGen.php
<?php
echo $_POST['data']; //gives error
/*$pdf = new FPDF();
$pdf->AddPage();
//over here I want to add the image from the chart.php page whose data url is now in the localstorage.
..more code to generate report
$pdf->output();*/
?>
如何在 php 脚本中获取图像?我尝试进行 ajax 调用,但在 pdfGen.php 脚本中得到undefined index data。我收到警报 HI 但无法获取服务器上的数据。
它似乎不起作用。
【问题讨论】:
-
使用 ajax 请求将其发布到服务器
-
ajax 是@charlietfl 所说的唯一方法
-
我尝试进行 ajax 调用,但失败了
-
@rkbom9 发生了什么错误?你试过什么代码?
标签: javascript php ajax html2canvas data-uri