【问题标题】:Laravel 4 wkHtmlToPdf routes, POST and GET issueLaravel 4 wkHtmlToPdf 路由,POST 和 GET 问题
【发布时间】:2013-11-08 01:55:15
【问题描述】:

对, 我已经为 wkhtmltopdf (https://github.com/ignited/laravel-pdf) 安装了服务提供者 当我添加

Route::get('/', function() {
    $pdf = PDF::make();
    $pdf->addPage('<html><head></head><body><b>Hello World</b></body></html>');
    $pdf->send();
});

在我的 routes.php 中它会生成一个 pdf 文件。

我要做的是将整个 div 发布到我的控制器,然后从中生成一个 PDF。

我的表格:

<form id="convert" action="{{{ URL::to('') }}}/pdf" method="post">
<input type="hidden" name="body" id="body">
<input type="submit" class="btn btn-success pdf" value="Done? Convert to PDF!">
</form>

jQuery:

$('form#convert').submit(function(){
                  $("input#body").val($("#preview").html());
              });

routes.php:

Route::post('pdf', 'PdfController@index');

最后是我的控制器 (PdfController):

<?php

class PdfController extends BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {

        $data = Input::get('body');


                $pdf = PDF::make();
            $pdf->addPage($data);
            $pdf->send('test.pdf');
            if(!$pdf->send())
        return $pdf->getError();
    }



}

不知何故,我认为这是一个基本的 POST 与 GET 的事情,或者我做的不对。 现在我收到错误Could not run command '/var/www/docassembly/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'

但是如果我用直接的 html 替换控制器中的变量,我会得到同样的错误。

如果我在表单和路由中使用 GET 并将直接 html 作为参数传递,则错误是不同的:

Could not run command '/var/www/docassembly/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64' test /tmp/tmp_WkHtmlToPdf_Hq98EZ: Loading pages (1/6) [> ] 0% [======> ] 10% [============================================================] 100% Error: Failed loading page http://test (sometimes it will work just to ignore this error with --load-error-handling ignore) 

因此将它放在控制器中也是一个问题。 有线索吗?

【问题讨论】:

    标签: post laravel laravel-routing


    【解决方案1】:

    根据您提供的documentation of the package,它说您需要在您的composer.json 文件中包含二进制文件才能使其工作:


    注意(您还必须包含 wkhtmltopdf 二进制文件) 32 位系统

    {
        "require": {
            "h4cc/wkhtmltopdf-i386": "*"
        }
    }
    

    64 位系统

    {
        "require": {
            "h4cc/wkhtmltopdf-amd64": "*"
        }
    }
    

    如果需要,您可以同时包含这两个。


    听起来 Laravel 正在尝试执行 64 位二进制文​​件但找不到它。确保:

    • 您已将它放在正确的文件夹中。
    • 它的执行权限设置为chmod

    【讨论】:

    • 二进制文件包含在内,当我的代码在 routes.php 中但不在我的控制器中时,L 可以找到它们,就像我在原始帖子中提到的那样。我还手动导航到二进制文件进行仔细检查。
    • 你确定你真的在运行你的控制器动作吗?在您的第二个错误中,似乎它正在尝试加载Error: Failed loading page http://test。尝试以action 参数的形式使用URL::action('PdfController@index')
    【解决方案2】:

    原来答案很简单,只需要用

    开始输入

    &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt; 并以&lt;/body&gt;&lt;/html&gt; 结尾

    由于我对 whtml2pdf 的输入是 div 的内容,因此我只需将这些 html 标记插入到要发送到控制器的输入值中,瞧。

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2015-05-28
      • 2013-01-07
      • 2015-08-22
      • 2013-09-17
      相关资源
      最近更新 更多