有什么方法可以使Javascript在DOMPDF生成的PDF中工作? [英] Is there any way to make Javascript work in DOMPDF-generated PDFs?
问题描述
我目前正在测试DOMPDF,并使其达到我的目的,包括CSS样式,显示从mysql数据库获取的内容等.
I am currently testing DOMPDF and got it working quite nice for my purposes, including CSS styling, displaying content fetched from a mysql database etc.
现在,我尝试使用一些Javascript,但是它不起作用.我使用了一个非常简单的脚本进行测试:
Now I tried to use some Javascript, but it doesn't work. I used a very simple script for testing:
页面上某处的HTML:
HTML somewhere on the page:
<div id='mydiv1' style='width: 100%;height:20px;background:#ddd;'></div>
JS(紧接在</body>
标记上方(但我也在打开< body>
标记之后也尝试过):
The JS (just above the closing </body>
tag (but I also tried it right after the opening <body>
tag):
<script>
document.getElementById('mydiv1').innerHTML = 'this is a test';
</script>
当我在浏览器中回显此页面时(回显包含完整HTML/PHP页面的变量),该文本将显示在DIV中.当我将相同的变量放入DOMPDF的 loadHtml
中,然后进行渲染并输出时,脚本生成的文本不会出现在PDF中(页面的其余部分会出现).
When I echo this page in the browser (I am echoing a variable which contains the complete HTML/PHP page), that text appears in the DIV. When I put the same variable in DOMPDF's loadHtml
and then render and output it, the script-generated text doesn't appear in the PDF (the rest of the page does).
所以我的问题是:有什么方法可以使Javascript在DOMPDF生成的PDF中工作?
So my question is: Is there any way to make Javascript work in DOMPDF-generated PDFs?
推荐答案
有一个DomPDF选项可以打开内联javascript:
There is a DomPDF option to turn on inline javascript:
$ isJavascriptEnabled = true;
$isJavascriptEnabled = true;
这里有一个如何使用DomPDF选项的示例:
Heres an example of how to use the DomPDF options:
$HTML = <<<HTML
!DOCTYPE html>
<html>
<head>
<body>
some html
</body>
<script> somejs </script>
</head>
</html>
HTML;
require_once "sites/all/libraries/dompdf/autoload.inc.php";
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->set('isJavascriptEnabled', TRUE);
$dompdf = new Dompdf($options);
$dompdf->load_html($HTML);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('blah.pdf');
我从DomPDF的选项页上获取了此内容: https://github.com/dompdf/dompdf/blob/master/src/Options.php
I took this from DomPDF's options page: https://github.com/dompdf/dompdf/blob/master/src/Options.php
这篇关于有什么方法可以使Javascript在DOMPDF生成的PDF中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!