2017-08-09 65 views
0

我在通過瀏覽器查看FPDF/PHP生成的PDF中運行JavaScript時遇到了一些問題。腳本應該顯示打印對話框或消息框。這個功能已經工作了好幾年了,但似乎已經被Edge/Windows的最新更新破壞了。經過調查,似乎基於瀏覽器/瀏覽器的行爲不同。PDF JavaScript不再運行在邊緣

的調查結果完全更新的Windows 10:

  • 邊緣:PDF邊沿自己的瀏覽器中打開和打印對話框不出現
  • 火狐:PDF在Firefox自己的瀏覽器打開並顯示打印對話框
  • 資源管理器:PDF在默認PDF查看器中打開和打印對話框出現

同樣的故事與呼叫對於t他的消息框。

我正在擴展像FPDF JavaScript support示例中指定的FPDF。

<? 

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功能?

+0

你檢查了瀏覽器控制檯日誌嗎? – instead

+0

@instead:你在談論F12控制檯選項卡嗎?在那裏,我只獲取一個信息(導航發生)和兩個警告(DOCTYPE預期和禁用後退和前進緩存)。 –

回答

1

是不是可以指望PDF在不同瀏覽器/瀏覽器中允許使用JavaScript功能?

這是不可能的。您嘗試運行的JavaScript僅適用於Adobe Acrobat和Reader以及其他一些桌面PDF查看器和一個我知道的移動PDF查看器。該代碼在Edge中不起作用,因爲Edge有自己的內置PDF查看器,Explorer將使用Adobe Reader或Acrobat,Foxit,Nuance,Nitro或其他一些操作系統級別的Reader來顯示PDF,這就是爲什麼您會看到對話框。

簡而言之,您只是無法依靠正確的查看器/瀏覽器/操作系統組合來運行PDF中的任何JavaScript。

+0

但是,它在邊緣直到大約一個月前... –

+0

我不確定這是怎麼可能的。 – joelgeraci