Sergey Scopin
Sergey Scopin

Reputation: 2245

yii2 mpdf pdf doesn't renders correctly

I want to generate pdf from my page on yii. I'm using github instructions So, I've created view pdf.php where there is only "test" string And then created an pdf-controller

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

When I go to this conroller via link, I get a string like this:

%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 %%

Anyone had experience with that library? Forgot to add. I've installed it via composer.

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

nothing more

Upvotes: 0

Views: 2076

Answers (2)

Barry
Barry

Reputation: 3723

The problem is caused by an incorrect content-type (text/html instead of application/pdf). Another solution is to set the response header yourself

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

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

Try changin 'mode'

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

Upvotes: 2

Related Questions