SLIM 3的mPDF问题

时间:2018-02-10 04:50:54

标签: mpdf slim-3

我在SLIM 3中构建应用程序,我需要生成PDF证书。 所以我在我的SLIM应用程序中添加了mPDF库扩展。 我试图用以下方法生成测试pdf:

use Mpdf\Mpdf;
use Slim\Http\Request;
use Slim\Http\Response;
/**
 * Class GenerateCertificate
 * @package saikatbepari\pdf
 */
 class GeneratePDF
 {

   public function generateCertificate(Request $request, Response $response)
  {
    $response = $response->withHeader( 'Content-type', 'application/pdf' );
    $mpdf = new Mpdf(['tempDir' => '../tmp']);
    $mpdf->WriteHTML('<h1>Hello world!</h1>');
    $mpdf->Output();
  }


}

,我得到的输出是:

enter image description here

任何人都可以帮助我,问题是什么以及如何解决它。 任何帮助都会得到满足。

3 个答案:

答案 0 :(得分:0)

您的Slim框架可能会重置内容类型。尝试从mPDF返回一个字符串并将其设置为您的PSR7响应的正文:

// ...
$body = $mpdf->Output('', 'S');
$stream = \GuzzleHttp\Psr7\stream_for($body);
$response = $response->withBody($stream);

return $response;

答案 1 :(得分:0)

  

这是我的简单解决方案

$app->get('/testing_mpdf/', 'testingMpdf');
function testingMpdf($request, $returned, $args) {
  $mpdf     = new Mpdf();
  $mpdf->WriteHTML('<h1>Hello World!</h1>');
  $mpdf->Output();
  return $returned->withHeader('Content-Type', 'application/pdf');
}

答案 2 :(得分:-1)

请遵循此规范

$ mpdf =新\ Mpdf \ Mpdf(['tempDir'=> DIR 。'/ tmp']);     $ url =“ http://localhost/ephotobook/homelink/login/75100/Screen%20Shot%202019-03-02%20at%2012.32.14%20PM.png”;     $ h =“ 30%”;     $ w =“ 40%”;         $ db = new DbOperation();         $ res = $ db-> showimagesbyid(75100);         $ b = $ res ['data'];
        $ tr =“”;
        foreach($ b as $ r){             $ url1 =“ http:// localhost / ephotobook / homelink / login / 75100 /” .rawurlencode($ r ['Filename']);             $ tr。=“”;         }     $ html =“                                            这是图像排序                  

    <div>
        <div>
            .$tr.    
    </div> 
    </body>
    </html>";

    $mpdf->WriteHTML($html);
    //$mpdf->WriteHTML(wireRenderFile('../invoice.php'));
    $mpdf->Output('../pdf/invoice5.pdf', 'F');