javascript - FPDF - 使用 PDF_JS 时在没有对话框的情况下打印

标签 javascript php pdf fpdf

正如标题所说。在这里搜索,我发现了 FPDF 的“扩展”,它允许打印文档。现在,我需要直接打印(即没有对话框)。我正在关注作者在 this page 中留下的评论但对我不起作用:(。我也尝试过复制和粘贴,但无法找到解决方案。

注意:我使用的是 FireFox(最新版本),但该过程根本不起作用。我还尝试使用 Chrome 和 Yandex 浏览器;两者都有效,但仍显示对话框。

PS:感谢您的宝贵时间!

我使用的代码如下。

pdf_js.php

require('fpdf.php');

class PDF_JavaScript extends FPDF {

    var $javascript;
    var $n_js;

    function IncludeJS($script) {
        $this->javascript=$script;
    }

    function _putjavascript() {
        $this->_newobj();
        $this->n_js=$this->n;
        $this->_out('<<');
        $this->_out('/Names [(EmbeddedJS) '.($this->n+1).' 0 R]');
        $this->_out('>>');
        $this->_out('endobj');
        $this->_newobj();
        $this->_out('<<');
        $this->_out('/S /JavaScript');
        $this->_out('/JS '.$this->_textstring($this->javascript));
        $this->_out('>>');
        $this->_out('endobj');
    }

    function _putresources() {
        parent::_putresources();
        if (!empty($this->javascript)) {
            $this->_putjavascript();
        }
    }

    function _putcatalog() {
        parent::_putcatalog();
        if (!empty($this->javascript)) {
            $this->_out('/Names <</JavaScript '.($this->n_js).' 0 R>>');
        }
    }
}

ex.php

<?php
require('pdf_js.php');

class PDF_AutoPrint extends PDF_JavaScript
{
function AutoPrint($dialog=false)
{
    //Open the print dialog or start printing immediately on the standard printer
    $param=($dialog ? 'true' : 'false');
    $script="print($param);";
    $this->IncludeJS($script);
}

function AutoPrintToPrinter($server, $printer, $dialog=false)
{
    //Print on a shared printer (requires at least Acrobat 6)
    $script = "var pp = getPrintParams();";
    if($dialog)
        $script .= "pp.interactive = pp.constants.interactionLevel.full;";
    else
        $script .= "pp.interactive = pp.constants.interactionLevel.automatic;";
    $script .= "pp.printerName = '\\\\\\\\".$server."\\\\".$printer."';";
    $script .= "print(pp);";
    $this->IncludeJS($script);
}
}

$pdf=new PDF_AutoPrint();
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->Text(90, 50, 'Print me!');
//Open the print dialog
$pdf->AutoPrint(true);
$pdf->Output();
?>

最佳答案

您正在将“dialog”变量设置为 true。您需要将其设置为 false。

AutoPrint 函数接受一个“对话框”变量 AutoPrint($dialog=false),它决定是否显示打印对话框。

在您的代码中,您为对话传递了 true。将 ex.php 的最后三行更改为以下内容:

//Do not open the print dialog
$pdf->AutoPrint(false);
$pdf->Output();

关于javascript - FPDF - 使用 PDF_JS 时在没有对话框的情况下打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38958643/

相关文章:

javascript - 从 Web 应用程序生成设计好的 PDF 报告

javascript - html - 选择范围 - 获取范围+起始节点+结束节点+距离

javascript - react forwardRef : ref and other props

javascript - 如何使用 jQuery 禁用 onmousedown 功能?

php - 使用 php 列出 mysql 数据库

php - 确定 PDF 文件是否具有可在 PHP 中搜索的文本

javascript - 如何排序导致 nodejs - mongodb 搜索,但是,通过调用动态方法

php - 使用php脚本在任何网站的搜索框中输入关键字

php - MySQL 日期格式

php - 是否可以使用 PHP 从 PDF 文件中删除密码?