【发布时间】:2017-08-09 21:21:56
【问题描述】:
我在通过浏览器查看的 FPDF/PHP 生成的 PDF 中运行 JavaScript 时遇到了一些问题。该脚本应该显示打印对话框或消息框。此功能已使用多年,但似乎已因 Edge/Windows 的重新更新而被破坏。经过调查,似乎行为因浏览器/查看器而异。
关于完全更新的 Windows 10 的发现:
- Edge:PDF 在 Edge 自己的查看器中打开,并且打印对话框不出现
- Firefox:PDF 在 Firefox 自己的查看器中打开并出现打印对话框
- Explorer:PDF 在默认 PDF 查看器中打开并出现打印对话框
消息框的调用也是如此。
我正在扩展 FPDF,就像在 FPDF JavaScript support 示例中指定的那样。
<?
require('fpdf.php');
class PDF_JavaScript extends FPDF {
// My code start
function OpenPrintDialog()
{
$this->IncludeJS("print(true);");
}
function ShowMessageMessage($text)
{
$this->IncludeJS("app.alert('$text', 3);");
}
// My code end
protected $javascript;
protected $n_js;
function IncludeJS($script, $isUTF8=false) {
if(!$isUTF8)
$script=utf8_encode($script);
$this->javascript=$script;
}
function _putjavascript() {
$this->_newobj();
$this->n_js=$this->n;
$this->_put('<<');
$this->_put('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
$this->_put('>>');
$this->_put('endobj');
$this->_newobj();
$this->_put('<<');
$this->_put('/S /JavaScript');
$this->_put('/JS '.$this->_textstring($this->javascript));
$this->_put('>>');
$this->_put('endobj');
}
function _putresources() {
parent::_putresources();
if (!empty($this->javascript)) {
$this->_putjavascript();
}
}
function _putcatalog() {
parent::_putcatalog();
if (!empty($this->javascript)) {
$this->_put('/Names <</JavaScript '.($this->n_js).' 0 R>>');
}
}
}
$pdf = new PDF_JavaScript();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 20);
$pdf->Text(90, 50, 'Print me!');
$pdf->OpenPrintDialog();
// or
//$pdf->ShowMessageMessage('Message box me!');
$pdf->Output();
?>
难道不能指望在不同的浏览器/查看器中允许 PDF JavaScript 功能吗?
【问题讨论】:
-
您检查过浏览器控制台日志吗?
-
@instead:您是在谈论 F12 控制台选项卡吗?那里我只得到一个信息(发生导航)和两个警告(DOCTYPE 预期和禁用来回缓存)。
标签: javascript pdf fpdf