我在通過瀏覽器查看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功能?
你檢查了瀏覽器控制檯日誌嗎? – instead
@instead:你在談論F12控制檯選項卡嗎?在那裏,我只獲取一個信息(導航發生)和兩個警告(DOCTYPE預期和禁用後退和前進緩存)。 –