Javascript在通过cron作业使用dompdf时不工作



我正在使用include函数调用php页面,并使用Dompdf库将页面转换为PDF。一切正常。但是在PHP页面中编写的javascript不起作用。实际上我正在做的是,我写了一个javascript代码来洗牌PHP页面中的一些内容,我正在使用Dompdf将PHP页面转换为PDF,但内容并没有被洗牌。当我直接在浏览器中命中template1.php页面时,javascript工作良好,但当我在cron作业中设置page1.php页面时,这是不工作的。

page1.php

<?php
$cust_details=array();
$db='';
getTemplate(1, 1, $cust_details, $db);
function getTemplate($no=1, $i, $cust_details,$db){
$customer_information = $cust_details;
$the_template= 'template1.php';

callDompdf($the_template,$i,$no,$cust_details,$db);

}
function callDompdf($outputtempl,$i,$templateno,$cust_details,$db){
ob_start();
include $outputtempl;
$contents = ob_get_contents();
ob_get_clean();



$dompdf = new Dompdf();
$dompdf->loadHtml($contents);
$options = $dompdf->getOptions(); 
$options->set(array('isRemoteEnabled' => true, 'isJavascriptEnabled'=> true));
$dompdf->setOptions($options);
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
$pdf = $dompdf->output();
$filename='reading_'.$i.'_'.$templateno.'_'.date('Y-m-d').'_'.time().'.pdf';
$filepath='pdf/'.$filename;
$data=file_put_contents($filepath,$pdf);
unset($dompdf);
unset($data);

}

template1.php

<html>
<body>
<span id="p_shuffle_0">Outcomes</span>
<span id="p_shuffle_1">Actions</span>
<span id="p_shuffle_2">Actions</span>
<span id="p_shuffle_3">Circumstances</span>
<span id="p_shuffle_4">Soulmate</span>
<?php echo '<script>
//shuffle();
const contentArr = ["Outcomes", "Actions", "Actions", "Circumstances", "Soulmate"];
var newshuffle= shuffle(contentArr);
function shuffle(array) {
let currentIndex = array.length,  randomIndex;

// While there remain elements to shuffle.
while (currentIndex != 0) {

// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;

// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}

return array;
}
newshuffle.forEach(shuffleContent);
function shuffleContent(value, index, array) {
// i is the index of content you want to use depending on which content you want
const newContent = contentArr[index];
const shuffleP = document.getElementById("p_shuffle_"+index);
shuffleP.textContent = newContent;



}
</script>';
?>
</body>

谢谢你的解决方案@Adyson先生,这是我的解决方案

function shuffle_assoc($my_array)
{
$keys = array_keys($my_array);
shuffle($keys);
foreach($keys as $key) {
$new[$key] = $my_array[$key];
}
$my_array = $new;
return $my_array;
}
$content1='This is content1';
$content2='This is content2';
$content3='This is content3';
$content4='This is content4';
$position_names= array("P1"=>$content1, "P2"=>$content2, "P3"=>$content3, "P4"=>$content4);
$shufflearr=shuffle_assoc($position_names);
<?php 
$i=0;
foreach ($shufflearr as $key=>$value){$i++;?>

<h4><strong>Position <?php echo $i;?>: <?php echo $key;?></strong></h4>
<div id="content_<?php echo $i;?>">
<?php echo $value;?>
</div>

<?php }?>

最新更新