关于javascript:HTML2PDF自动打印

HTML2PDF automatic printing

在将pdf输出到浏览器时,是否有一种简单的方法来使HTML2PDF自动打开打印对话框窗口?

我尝试在输出之前设置javascript-标题冲突
我尝试了PDF的输出-没有影响
我像TCPDF建议的那样尝试了PDF(我认为HTML2PDF是在此库上构建的),但是由于PDF中不支持JS,所以它不允许这样做。

人们还有其他方法吗?还是我需要用iframe或Windows入侵某些东西,然后通过该方法声明打印?

任何帮助表示赞赏。


一旦我在pyton上编写脚本,该脚本就会转换HTML文档并将其发送以进行自动打印。
这里的链接:
https://gist.github.com/stopfaner/9b30b2f04aa47c5fb480
如果对您有用,我会很高兴


打印内容取决于您如何让浏览器显示PDF。如果让浏览器"正常"显示它们是插件(Adobe,FoxIT)还是PDF的本机显示,那么您将无能为力,因为您无法使用JavaScript来访问它们。

您可以使用pdf.js,该pdf.js使用JavaScript在可以打印的画布上呈现PDF,例如,参见使用pdf.js打印PDF


好吧,应该有一种方法,尽管我没有更深入地探讨它,但我希望这能激发其他人的灵感(如果我有空的话,我会努力的)。

如本页http://www.fpdf.org/en/script/script36.php所述,可以注入一些javascript(打开打印对话框)。现在,此扩展名适用于FPDF,而不适用于HTML2PDF库。

也许有,或者将要编写一个HTML2PDF扩展名,但是我的直觉是,在创建文档后,仅使用纯PHP会更容易。

如此处所述,应该简单明了:

take an existing PDF, open it in a text editor and find /Catalog and insert the boilerplate after the /Pages reference, and put in your code

(src:http://bililite.com/blog/2012/06/06/adding-javascript-to-pdf-files/)

如果我有更多信息或概念证明(使用HTML2PDF),则会进行更新。

编辑

我刚刚测试了这个概念,并且效果很好。像示例中一样,将此脚本插入/ Catalog之后,并在新行之后插入。

1
2
3
4
5
6
7
8
9
10
11
12
13
/Names << % the Javascript entry
  /JavaScript <<
    /Names [
      (EmbeddedJS)
      <<
        /S /JavaScript
        /JS (
          print(true);
        )
      >>
    ]
  >>
>> % end of the javascript entry

请注意,这只能在Adobe Reader或Acrobat Pro中使用,而在其他PDF阅读器中可能无法使用(例如OSX中的预览应用程序不起作用,但是Chrome中的内置阅读器工作正常)

编辑2-使用HTML2PDF库的概念证明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$printCommand = <<<EOF

/Type /Catalog
 /Names <<
    /JavaScript <<
      /Names [
        (EmbeddedJS)
        <<
          /S /JavaScript
          /JS (
            print(true);
          )
        >>
      ]
    >>
  >>

EOF;

// Using the output method like this, you will get
// the raw ouput back to manipulate
$bin = $html2pdf->Output('', true);

// When the /Names block shows up somewhere later in
// in PDF code, it will override your script and will do nothing.
// This is just for proof of concept, you want to use regex here
if (strpos($bin, '/Names << >>') === false) {
    $bin = str_replace('/Type /Catalog', $printCommand, $bin);
} else {
    $printCommand = str_replace('/Type /Catalog', '', $printCommand);
    $bin = str_replace('/Names << >>', $printCommand, $bin);
}

// Since we don't use the output function from HTML2PDF,
// you have to set the headers manually
header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 29 Jun 1985 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Disposition: inline; filename="your-pdf-title";');

echo $bin;
exit;