【发布时间】:2022-12-09 18:03:18
【问题描述】:
我正在使用 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);
}
模板1.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>
【问题讨论】:
-
How will the javascript code work?....你真的读过我写的东西吗?再一次,为了清楚起见:重新组织您的脚本,以便所有必要的任务都由 php 完成 -
I have enclosed the javascript code inside php tag????你为什么期望它起作用?看来你对基础知识一窍不通。 JavaScript 不是 PHP。 PHP 不是 JavaScript。它们是两种完全独立的语言,运行在完全独立的环境中。 PHP 解释器无法执行 JavaScript 代码。该 JavaScript 只能由 Web 浏览器(包含 JavaScript 运行时引擎)执行。当您通过 cron 运行 PHP 脚本时,基本上就像从命令窗口运行 PHP 一样……因此不涉及浏览器。 -
您不能将 JavaScript 代码(或任何其他代码,例如 C#、python、ruby 或其他任何代码)放在
<?php标记内并期望它神奇地变成 PHP 代码。花点时间停下来好好想想,真的应该把这个想法从你的脑海中赶走。你需要改写PHP 中的 JavaScript 代码,以产生与从浏览器运行 JavaScript 当前所达到的效果相同的效果。 -
怎么办?你的意思是如何重写它?第一步是了解 JS 在做什么。它基本上随机移动
<span标签。现在您必须坐下来思考如何使用 PHP 实现相同的最终结果。然后你试着写一些代码。用同样的方法解决任何编程问题,真的!您是否坚持其中的特定方面? -
基本上,不是将跨度作为静态 HTML,然后在将它们加载到页面后使用 JS 来操作它们,而是需要编写 PHP 代码来获取数组,将其按随机顺序放置,然后回显一些 @ 987654327@ 标签基于数组的随机顺序。
标签: javascript php dompdf