Search code examples
phppdfpdf-generationwkhtmltopdfdompdf

Enable to generate pdf (It give me plan html text in a file) - dompdf


$content['profile_data'] = file_get_contents(base64_decode($profile_data,true));
$pdf = PDF::loadHtml(htmlentities($content['profile_data']));
$pdf->setOptions(['setIsHtml5ParserEnabled'=>true]);
// $pdf->setPaper('A4','landscape');
$pdf->stream();
$pdf->save($path . '/' . $file_name.'.pdf');

The whole code seems ok. The html coming from a url. I am trying to save it in a file as pdf.

But it gives me a plan Text HTML when i try to open. Please help me

thanks


Solution

  • You do not render (->render()).

    Short example:

    $html = ''; // Your html.
    $size = 'A4';
    $orientation = 'portrait';
    $options = new Options(
        [
            'isHtml5ParserEnabled'    => true, // little faster
        ]
    );
    $domPdf = new Dompdf($options);
    $domPdf->loadHtml($html);
    $domPdf->setPaper($size, $orientation);
    $domPdf->render();
    $pdf = $domPdf->output();
    

    And here i post all that i do with dompdf -
    i searched a lot, lots of learn by doing ... perhaps it helps:

    Following is for version v0.8.6

    /**
     * Returns pdf from html.
     *
     * @param string $html
     * @param string $size
     * @param string $orientation
     *
     * @return string
     */
    public function htmlToPdf($html, $size = 'A4', $orientation = 'portrait')
    {
        $options = new Options(
            [
                //'logOutputFile'           => 'data/log.htm',
                'isPhpEnabled'            => false,
                'isRemoteEnabled'         => false,
                'isJavascriptEnabled'     => false,
                'isHtml5ParserEnabled'    => true, // little faster
                'isFontSubsettingEnabled' => false,
                'debugPng'                => false,
                'debugKeepTemp'           => false,
                'debugCss'                => false,
                'debugLayout'             => false,
                'debugLayoutLines'        => false,
                'debugLayoutBlocks'       => false,
                'debugLayoutInline'       => false,
                'debugLayoutPaddingBox'   => false,
                //'pdfBackend'              => 'CPDF',
            ]
        );
        $domPdf = new Dompdf($options);
        $domPdf->loadHtml($this->minimizeHtml($html));
        $domPdf->setPaper($size, $orientation);
        $domPdf->render();
        return $domPdf->output();
    }
    
    /**
     * Minimizes the html source.
     *
     * @see http://stackoverflow.com/a/6225706/3411766
     *
     * @param string $html
     *
     * @return string
     */
    public function minimizeHtml($html)
    {
        return preg_replace(
            [
                '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
                '/[^\S ]+\</s',  // strip whitespaces before tags, except space
                '/(\s)+/s'       // shorten multiple whitespace sequences
            ],
            [
                '>',
                '<',
                '\\1'
            ],
            $html
        );
    }