如何在 wordpress 中使用 ajax 执行 wkhtmltopdf 命令
How to excute wkhtmltopdf command using ajax in wordpress
我创建了一个 wp_ajax 函数并在单击按钮时调用该函数。在 ajax 函数中,我添加了执行 wkhtmltopdf 库的命令,但我的代码无法正常工作。请检查。
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) {
}
});
});
提前致谢
我不确定,但试试
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});
});
我创建了一个 wp_ajax 函数并在单击按钮时调用该函数。在 ajax 函数中,我添加了执行 wkhtmltopdf 库的命令,但我的代码无法正常工作。请检查。
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) {
}
});
});
提前致谢
我不确定,但试试
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});
});