Php 通过ajax发送图像数据,转换为pdf,下载?
我正在使用html2canvas为我的页面拍照,通过ajax将图像数据发送到php,现在我正在尝试让php将图像数据转换为pdf。以下是我所拥有的:Php 通过ajax发送图像数据,转换为pdf,下载?,php,jquery,ajax,pdf,Php,Jquery,Ajax,Pdf,我正在使用html2canvas为我的页面拍照,通过ajax将图像数据发送到php,现在我正在尝试让php将图像数据转换为pdf。以下是我所拥有的: $("a[name='download_report']").click(function(e) { e.preventDefault(); var button = $(this); button.addClass("disabled").attr("disabled", "disabled"); html2ca
$("a[name='download_report']").click(function(e) {
e.preventDefault();
var button = $(this);
button.addClass("disabled").attr("disabled", "disabled");
html2canvas($('#report-wrapper'), {
onrendered: function(canvas) {
var img = canvas.toDataURL("image/png");
$.ajax({
'url' : "{{route('report.download')}}",
'type' : 'POST',
'dataType' : "json",
'data' : {img:img},
'timeout' : 15000
}).success(function() {
button.removeClass("disabled").removeAttr("disabled");
}).error(function() {
alert("There was a problem converting this report to a PDF.");
button.removeClass("disabled").removeAttr("disabled");
});
}
});
});
以下是我在php端的内容:
public function download(Request $request) {
$img = str_replace('data:image/png;base64,', '', $request->get('img'));
$img = str_replace(' ', '+', $img);
$img = base64_decode($img);
}
我不知道从这里到哪里可以将png转换成pdf,然后将其发送回ajax请求,让浏览器自动将其作为pdf下载
我知道图像数据是正确的,因为我可以使用javascript将其输出到浏览器,并查看正确的图像
如有进一步指示,将不胜感激。仅供参考,这是一个laravel应用程序,因此采用了blade语法。任何仍在搜索此问题答案的人,这对我来说都很有用: JS
$('button').on( 'click', function() {
var screenshot = {};
html2canvas( [ document.getElementById( 'main-container' ) ], {
onrendered: function( canvas ) {
screenshot.img = canvas.toDataURL( "image/png" );
screenshot.data = { 'image' : screenshot.img };
$.ajax({
url: "/image_handler.php",
data: screenshot.data,
type: 'post',
success: function( result ) {
console.log( result );
}
});
}
});
});
PHP
$result = file_put_contents( 'myimage.png', base64_decode( str_replace('data:image/png;base64,','',$_POST['image'] ) ) );
引用本文件“halburgiss”中的代码您是否尝试过在浏览器中使用jsPdf进行转换?我尝试过,它只支持jpeg,并且由于某种原因,当我使用
canvas.todata URL(“图像/jpeg”)时代码>背景是黑色的,我无法更改,而且由于页面太大,将图像放在pdf上的缩放比例不正确。所以我在探索不同的选择。