Laravel 4 使用 Javascript 渲染()PDF

Laravel 4 render() PDF with Javascript

我需要使用 Javascript Apis

从 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 以正常 HTML 更改 Dom 但是当我以 PDF 显示时不起作用。

谢谢!!

根据我对 dompdf(非常著名的 pdf 库)的体验,这些库不支持 JS 解释(因为 php 没有能力解释 javascript),这里有两个我认为你可以尝试的解决方法。

1.Server 侧渲染 html,将你的 dom 操作移动到 php 以创建 dom 元素。(推荐)

2.Use php和phantomJS.The php调用phantomJS截屏html并保存为pdf

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);