【问题标题】:Laravel 4 render() PDF with JavascriptLaravel 4 渲染() PDF 与 Javascript
【发布时间】:2015-06-07 11:26:29
【问题描述】:

我需要使用 Javascript API 从 HTML 视图渲染创建 PDF

这是我在 PHP Laravel 4 中的代码

$con = Contrato::find($id);
        $html = (string) View::make('contratos.contratopdf')->with('con',$con)->render();
        return PDF::load(utf8_decode($html), 'A5', 'landscape')->show();

在视图中我有这个脚本

<script src="http://test.rentacar.cl/js/lib/jquery.signaturepad.min.js"></script>

这个 js 将 Dom 更改为普通 HTML,但是当我以 PDF 显示时不起作用。

谢谢!!

【问题讨论】:

    标签: javascript html pdf dom laravel-4


    【解决方案1】:

    PhantomJS 运行良好,看看这个包:https://github.com/jonnnnyw/php-phantomjs

    完整文档在这里:http://jonnnnyw.github.io/php-phantomjs/

    例子:

    <?php
    
    use JonnyW\PhantomJs\Client;
    
    $client = Client::getInstance();
    
    $request  = $client->getMessageFactory()->createCaptureRequest('http://google.com');
    $response = $client->getMessageFactory()->createResponse();
    
    $file = '/path/to/save/your/screen/capture/file.jpg';
    
    $request->setCaptureFile($file);
    
    $client->send($request, $response);
    

    【讨论】:

    • 您能否提供更多关于如何使用 Phantom 的上下文,而不是仅仅转储工具的链接?
    • 我已经更新了我的答案,提供了完整文档和有效代码示例的链接。
    【解决方案2】:

    根据我对dompdf(非常有名的pdf库)的经验,那些库不支持JS解释(因为php没有解释javascript的能力),我认为您可以尝试以下两种解决方法。

    1.服务器端渲染html,将你的dom操作移到php中以创建dom元素。(推荐)

    2.使用php和phantomJS。php调用phantomJS截取html的屏幕并将截图保存为pdf。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-25
      • 2016-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 2017-12-16
      相关资源
      最近更新 更多