yii2 mpdf pdf无法正确呈现

时间:2016-05-03 14:42:10

标签: pdf yii

我想在yii上从我的页面生成pdf。 我正在使用github instructions 所以,我创建了视图pdf.php,其中只有"test"字符串 然后创建了一个pdf控制器

public function actionGeneratePdf()
{
    $content = $this->renderPartial('pdf');
    $pdf = new Pdf([
        // set to use core fonts only
        'mode' => Pdf::MODE_CORE, 
        // A4 paper format
        'format' => Pdf::FORMAT_A4, 
        // portrait orientation
        'orientation' => Pdf::ORIENT_PORTRAIT, 
        // stream to browser inline
        'destination' => Pdf::DEST_BROWSER, 
        // your html content input
        'content' => $content,  
        // format content from your own css file if needed or use the
        // enhanced bootstrap css built by Krajee for mPDF formatting 
        'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
        // any css to be embedded if required
        'cssInline' => '.kv-heading-1{font-size:18px}', 
         // set mPDF properties on the fly
        'options' => ['title' => 'Krajee Report Title'],
         // call mPDF methods on the fly
        'methods' => [ 
            'SetHeader'=>['Krajee Report Header'], 
            'SetFooter'=>['{PAGENO}'],
        ]
    ]);
    return $pdf->render(); 
}

当我通过链接转到此控制器时,我得到一个这样的字符串:

%PDF-1.4 %���� 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x��R=O�0��+n�ŵ_⏬HPS�H���nQE�����y�W�R�21�I.��绠��J��Z�����dF�W���>��+��)0�#h% o*V�-D���S1��5�;~7�� x_%�_�����:)|MidJ�U;��7uJf�'RN:�(���c�؄�Y������e��QlXy�V��DJ�P�H��[��(�CC��_��cox�X�|O�c�ٔ����ah╏�C���\����I�< S�a��[��6��N���V�ce&�2��m�qmc��z�Hôc;�ag�H�k5z���3��#��۹��p[�:䮉%����������4O��\:�K*��n�R endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj 7 0 obj <> endobj 8 0 obj <> endobj 9 0 obj <> endobj 2 0 obj <> /ExtGState << /GS1 5 0 R >> >> endobj 10 0 obj << /Producer (��mPDF 6.1) /Title (��Krajee Report Title) /CreationDate (20160503174102+03'00') /ModDate (20160503174102+03'00') >> endobj 11 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /XYZ null null 1] /PageLayout /OneColumn >> endobj xref 0 12 0000000000 65535 f 0000000640 00000 n 0000001193 00000 n 0000000015 00000 n 0000000223 00000 n 0000000729 00000 n 0000000790 00000 n 0000000888 00000 n 0000000984 00000 n 0000001085 00000 n 0000001339 00000 n 0000001513 00000 n trailer << /Size 12 /Root 11 0 R /Info 10 0 R /ID [ ] >> startxref 1623 %%

有没有经验的图书馆? 忘了添加。我已经通过作曲家安装了它。

php composer.phar require kartik-v/yii2-mpdf "*"
php composer.phar update

不再是

2 个答案:

答案 0 :(得分:2)

尝试使用changin&#39;模式&#39;

public function actionGeneratePdf()
{
  $content = $this->renderPartial('pdf');
  $pdf = new Pdf([
    // set to use core fonts only
    'mode' => Pdf::MODE_BLANK,
    // A4 paper format

答案 1 :(得分:0)

问题是由不正确的内容类型(text / html而不是application / pdf)引起的。 另一种解决方案是自己设置响应头

public function actionGeneratePdf()
{
  // set response header
  \yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
  \yii::$app->response->headers->add('Content-Type', 'application/pdf');

  // create pdf as described in demo
  $content = $this->renderPartial('pdf');
  $pdf = new Pdf([
    ::
  ]);
  return $pdf->render();
}