简体   繁体   English

使用打印布局将HTML页面保存为PDF

[英]Save HTML page to PDF with print layout

I want to allow my users to save my page as a PDF. 我想允许我的用户将页面另存为PDF。 I have created a print stylesheet and I can generate the PDF by using Javascript's print function. 我已经创建了一个打印样式表,并且可以使用Javascript的print功能来生成PDF。 Here's my problem: 这是我的问题:

In Chrome , the browser generates a preview and shows it to the user. Chrome中 ,浏览器会生成预览并将其显示给用户。 The user can then either save it or print it. 然后,用户可以保存它或打印它。

However, in IE and FF, print calls up a complex menu, and while it can generate a PDF by "printing" to PDFCreator, it's a complex process that many users won't understand. 但是,在IE和FF中, print调用一个复杂的菜单,尽管它可以通过“打印”到PDFCreator 生成PDF,但这是一个复杂的过程,许多用户无法理解。

What I want to do is to somehow duplicate the Chrome functionality for non-Chrome users. 我要做的是以某种方式为非Chrome用户复制Chrome功能。 Options I have considered: 我考虑过的选择:

  • Screenshot the HTML page and render that image into a PDF with Javascript. 截屏HTML页面,然后使用Javascript将图像渲染为PDF。 There are libraries that do this, but I want my PDF to have the print layout. 有执行此操作的库,但是我希望我的PDF具有打印布局。
  • Generate the PDF on the server and send it to the user's browser. 在服务器上生成PDF并将其发送到用户的浏览器。 This can be done, but it seems difficult to use the same HTML as the standard page. 可以做到这一点,但是似乎很难使用与标准页面相同的HTML。

My server is running PHP and the Zend framework. 我的服务器正在运行PHP和Zend框架。 I can't use NodeJS or any headless browser to render on the server. 我无法使用NodeJS或任何无头浏览器在服务器上进行渲染。 Do I have any options? 我有什么选择吗?

I've done exactly what you want to do before using a library called dompdf ( https://github.com/dompdf/dompdf ). 在使用称为dompdf( https://github.com/dompdf/dompdf )的库之前,我已经完全完成了您想做的事情。 Here is how I used it: 这是我的用法:

I dropped the dompdf library into the "library" folder in my ZF project. 我将dompdf库放入ZF项目的“ library”文件夹中。 Then, in my application when I'm ready to render the page I created a new Zend_View() object and set it up with whatever view script variables it needed. 然后,在准备渲染页面的应用程序中,创建了一个新的Zend_View()对象,并使用所需的任何视图脚本变量对其进行设置。 Then I called the render() function and stored the rendered output into a variable I then provided to dompdf. 然后,我调用render()函数并将渲染的输出存储到变量中,然后将其提供给dompdf。 The code looks like this: 代码如下:

        $html = new Zend_View();
        $html->setScriptPath(APPLICATION_PATH . '/views/scripts/action_name/');
        $html->assign($data); //$data contains the view variables I wanted to populate.
        $bodyText = $html->render('pdf_template.phtml');
        require_once(APPLICATION_PATH."/../library/dompdf/dompdf_config.inc.php");
        $dompdf = new DOMPDF();
        $dompdf->load_html($bodyText);
        // Now you can save the rendered pdf to a file or stream to the browser:
        $dompdf->stream("sample.pdf");

        // Save to file:
        $dompdf->render();
        $pdf = $dompdf->output();
        file_put_contents($filename, $pdf);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM