【问题标题】:PDF JavaScript no longer run in EdgePDF JavaScript 不再在 Edge 中运行
【发布时间】: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


【解决方案1】:

难道不能指望在不同的浏览器/查看器中允许 PDF JavaScript 功能吗?

这是不可能的。您尝试运行的 JavaScript 仅适用于 Adob​​e Acrobat 和 Reader 以及我知道的其他一些桌面 PDF 查看器和一个移动 PDF 查看器。该代码在 Edge 中不起作用,因为 Edge 有自己的内置 PDF 查看器,Explorer 将使用 Adob​​e Reader 或 Acrobat、Foxit、Nuance、Nitro 或其他一些操作系统级别的阅读器来显示 PDF,这就是你得到对话框的原因.

简而言之,您不能指望正确的查看器/浏览器/操作系统组合能够在 PDF 中运行任何 JavaScript。

【讨论】:

  • 我不确定这怎么可能。
猜你喜欢
  • 1970-01-01
  • 2017-07-29
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 2020-07-12
  • 1970-01-01
相关资源
最近更新 更多