Spis treści

Biblioteka do odczytywania metadanych PDF

https://github.com/smalot/pdfparser

Generowanie PDF do przeglądarki

$html = $this->renderPartial('html-pdf', ['model' => $model]);
 
//rozwiązuje problem z kodowaniem znaków
$html = mb_convert_encoding($html, 'UTF-8', 'UTF-8'); 
 
$mpdf = new \Mpdf\Mpdf([
      'tempDir' => sys_get_temp_dir(),
      'mode' => 'utf-8',
      //'format' => 'A4-L',
      'default_font_size' => 8,
      'margin_left' => 15,
      'margin_right' => 10,
      'margin_top' => 5,
      'margin_bottom' => 1,
      'margin_header' => 0,
      'margin_footer' => 2,
      'setAutoBottomMargin' => 'stretch'
  ]);
$mpdf->SetTitle($nazwa_pliku);
$mpdf->SetHTMLFooter('
        <table width="100%">
            <tr>
                <td width="33%">{DATE j-m-Y}</td>
                <td width="33%" align="center">{PAGENO}/{nbpg}</td>
                <td width="33%" style="text-align: right;">' . $stopka . '</td>
            </tr>
        </table>');
 
$mpdf->WriteHTML($html);
        ob_start();
        $mpdf->Output($nazwa_pliku, 'I');
        $output = ob_get_clean();
        // yii2 headers for pdf
        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
        Yii::$app->response->headers->add('Content-Type', 'application/pdf');
        Yii::$app->response->headers->add('Content-Disposition', 'inline; filename="' . $nazwa_pliku . '"');
        Yii::$app->response->headers->add('Content-Length', strlen($output));
        return $output;

Inna wersja

$path = Yii::getAlias('@pliki') . '/prace-dyplomowe/' . $nazwa_pliku;
            $mpdf = new \Mpdf\Mpdf([
                'tempDir' => sys_get_temp_dir(),
                'mode' => 'utf-8',
                //'format' => 'A4-L',
                'default_font_size' => 10,
                'margin_left' => 15,
                'margin_right' => 10,
                'margin_top' => 5,
                'margin_bottom' => 1,
                'margin_header' => 0,
                'margin_footer' => 2,
                'setAutoBottomMargin' => 'stretch'
            ]);
            $mpdf->SetTitle('Wniosek');
            $mpdf->SetHTMLFooter('
                <table width="100%">
                    <tr>
                        <td width="80%">Wydruk z dnia: {DATE j-m-Y} Numer sprawy ' . $crd['znak'] . ' </td>
                        <td width="20%" align="right">Strona: {PAGENO} z {nbpg}</td>
                    </tr>
                </table>'
            );
 
            $mpdf->WriteHTML($tresc_pdf);
            $mpdf->Output($path, 'F');

Zapisywanie do pliku

$mpdf->Output("./your_folder_location/".$nazwa_pliku, 'F');

Zapisywanie do bazy - blob

...
$mpdf->WriteHTML($html);
$model = new Dokument();
$model->plik = $mpdf->Output('protokol', 'S');
$model->save();

Odczytywanie z bazy i wyświetlanie w przeglądarce

$model = Dokument::find()
                ->where(['id'=>1])
                ->one();
header('Content-Type: application/pdf');
header('Content-Disposition:inline; filename=' . $model->nazwa. '.pdf');
echo $model->plik;