kartik-v / yii2-mpdf

A Yii2 wrapper component for the mPDF library which generates PDF files from UTF-8 encoded HTML.
http://demos.krajee.com/mpdf
Other
161 stars 150 forks source link

blank pdf even with basic example from demo.krajee.com #90

Closed attybean closed 5 years ago

attybean commented 5 years ago

Steps to reproduce the issue

(perhaps same issue as: https://github.com/kartik-v/yii2-mpdf/issues/24) but still not working in v1.0.6)

  1. Copy code from: http://demos.krajee.com/mpdf#demo
  2. Run code

Expected behavior and actual behavior

When I follow those steps, I see...

Blank /empty pdf created, '

I was expecting...

Pdf with contents of privacy.php file in it

Environment

yii2 2.0.15.1 php 7.0 and php 7.2 (same result)

Browsers

Libraries

Isolating the problem

I tested mpdf directly and the basic example worked fine: https://github.com/mpdf/mpdf-examples/blob/development/example01_basic.php

I can var_dump the contents of $pdf->render and it actually contains a correctly formatted pdf. I have tried different response formats, and headers, but to no avail.

attybean commented 5 years ago

I am not sure if it helps to reveal the source of the problem, but I got the pdf to download by doing the following:

        $data = $pdf->render();
        $type = ($destination == "DEST_BROWSER" ? Pdf::DEST_BROWSER : Pdf::DEST_DOWNLOAD);
        header('Content-Disposition: '.$type.'; filename="'.$docName.'.pdf"');              
        header('Content-Length: ' . strlen($data));
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header("Content-Transfer-Encoding: binary ");
        echo $data;
return;

I still do not understand why content-length is 0 when I use return $pdf->render;

attybean commented 5 years ago

Here is a shortened version that hack lets me come around the problem: replace:

return $pdf->render();

with

 $type = ($destination == "DEST_BROWSER" ? Pdf::DEST_BROWSER : Pdf::DEST_DOWNLOAD);
        header("Content-Type: application/pdf");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Transfer-Encoding: binary ");
        if($destination != "DEST_BROWSER")
            header('Content-Disposition: attachment; filename="'.$docName.'.pdf"');              
        echo $pdf->render();
        return;
josealeonf commented 5 years ago

Yes, have the same problem. Clean installation with composer, basic example as in the README and the file is created with 0 bytes. I downloaded the mpdf library separately and the mpdf examples works as expected. But through kartik-v/yii2-mpdf 0 bytes. PHP 5.6

josealeonf commented 5 years ago

I got it to work, my mistake and its because I was updating from previous version. Before you just had to do a $pdf->render(); but with new version 1.0.5 you actually have to do a return $pdf->render();

condesignpoland commented 5 years ago

Here is a shortened version that hack lets me come around the problem: replace:

return $pdf->render();

with

$type = ($destination == "DEST_BROWSER" ? Pdf::DEST_BROWSER : Pdf::DEST_DOWNLOAD);
       header("Content-Type: application/pdf");
       header("Pragma: public");
       header("Expires: 0");
       header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
       header("Content-Transfer-Encoding: binary ");
       if($destination != "DEST_BROWSER")
           header('Content-Disposition: attachment; filename="'.$docName.'.pdf"');              
       echo $pdf->render();
       return;

same here. thanks for quick fix

attybean commented 5 years ago

I got it to work, my mistake and its because I was updating from previous version. Before you just had to do a $pdf->render(); but with new version 1.0.5 you actually have to do a return $pdf->render();

Thank you for the tip, but unfortunately even with return $pdf->render(); The PDF is consistently empty.

kartik-v commented 5 years ago

The demo does run fine. Hope you are having the correct latest release of the extension and ensure you have setup the code / configuration correctly.

Check the demos - where it works fine - the complete controller action that renders PDF looks like below on the demo.

use Yii;
use yii\web\Response;
use kartik\mpdf\Pdf;

// Privacy statement output demo
public function actionViewPrivacy() {
    Yii::$app->response->format = Response::FORMAT_RAW; // this is optional but may help you
    $pdf = new Pdf([
        'mode' => Pdf::MODE_CORE, // leaner size using standard fonts
        'destination' => Pdf::DEST_BROWSER,
        'content' => $this->renderPartial('privacy'),
        'options' => [
            // any mpdf options you wish to set
        ],
        'methods' => [
            'SetTitle' => 'Privacy Policy - Krajee.com',
            'SetSubject' => 'Generating PDF files via yii2-mpdf extension has never been easy',
            'SetHeader' => ['Krajee Privacy Policy||Generated On: ' . date("r")],
            'SetFooter' => ['|Page {PAGENO}|'],
            'SetAuthor' => 'Kartik Visweswaran',
            'SetCreator' => 'Kartik Visweswaran',
            'SetKeywords' => 'Krajee, Yii2, Export, PDF, MPDF, Output, Privacy, Policy, yii2-mpdf',
        ]
    ]);
    return $pdf->render();
}
carlos-landeira commented 5 months ago

I'm having problems with release 1.0.6 and PHP 8.1, whenever i try to render a PDF with browser destination, i get the "Error: Failed to load PDF document." from Chrome. If i set destination to download, it downloads an empty .pdf file with 0b. Opening it in VS code, got this:

pdfError

Tried passing the HTML content using both content property from the Kartik Pdf class and using api->WriteHTML() and then returning $pdf->render().

I'm also using Kartik's GridView and the PDF export for there works fine.