Dompdf does NOT render images well with remote urls

use Dompdf\Options;

use finfo

// function


        function encode_img_base64( $img_path = false, $img_type = ‘png’ ){

            if( $img_path ){

                //convert image into Binary data

                $img_data = fopen ( $img_path, ‘rb’ );

                $img_size = filesize ( $img_path );

                $binary_image = fread ( $img_data, $img_size );

                fclose ( $img_data );

                //Build the src string to place inside your img tag

                $img_src = “data:”.$img_type.”;base64,”.str_replace (“\n”, “”, base64_encode($binary_image));

                return $img_src;

            }

            return false;

        } 

// run 

$target = “/user/www/test.com/uploads/images/16a8f22beb11808dc84c17fba020a73a.jpg”;

$finfo = new finfo();

$fileMimeType = $finfo->file($target, FILEINFO_MIME_TYPE);

$result_base64_code = encode_img_base64($target,$fileMimeType);

$img = $result_base64_code ;

        $options = new Options();

        $options->setIsRemoteEnabled(true);

        $options->setIsHtml5ParserEnabled(true);

        $dompdf = new \Dompdf\Dompdf($options);

$dompdf->loadHtml($html);

// result html($html) to sample.pdf

123 <br> 222 <br> <img src=‘$img’>

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다