我创建了一个wp_ajax函数并在按钮点击时调用此函数。在ajax函数中,我已经为excute wkhtmltopdf libarary添加了一个命令,但我的代码无效。请检查。
add_action( 'wp_ajax_genratepdf', 'genratepdf' );
add_action( 'wp_ajax_nopriv_genratepdf', 'genratepdf' );
function genratepdf(){
shell_exec("wkhtmltopdf http://www.google.com /dir-path/pdf38.pdf");
die('Y');
}
Jquery代码:
jQuery("#myBtnPdf").click(function (event) {
event.preventDefault();
var divContents = jQuery(".pf-content").html();
jQuery.ajax({
type: "post",
url: ajaxurl,
data: { action: "genratepdf", content:divContents },
success:
function(data) {
}
});
});
提前致谢
答案 0 :(得分:1)
我不确定但是试试
function genratepdf(){
$ds = DIRECTORY_SEPARATOR ;
$pdf_path = WP_CONTENT_DIR. $ds. "xyz.pdf";
exec ("wkhtmltopdf http://www.google.com ".$pdf_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($pdf_path).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($pdf_path));
ob_clean();
readfile($pdf_path);
die();
}
它会将pdf文件创建到wordpress内容目录
在Javascript代码中
function autoSubmitForm(method, url, post_data) {
var element = document.getElementById("virtual_form");
if(element != null )
{
element.parentNode.removeChild(element);
}
var form = document.createElement("form");
form.setAttribute("id", "virtual_form");
form.setAttribute("style", "display:none;");
form.setAttribute("target", "_blank");
form.method = method;
form.action = url;
for(i in post_data)
{
var element=document.createElement("input");
element.value=post_data[i];
element.name=i;
form.appendChild(element);
}
document.body.appendChild(form);
form.submit();
form.parentNode.removeChild(form);
}
jQuery("#myBtnPdf").click(function (event) {
event.preventDefault();
var divContents = jQuery(".pf-content").html();
autoSubmitForm('POST',ajaxurl,{action:"genratepdf",content:divContents});
});