{"id":1061,"date":"2024-04-20T08:06:00","date_gmt":"2024-04-19T23:06:00","guid":{"rendered":"http:\/\/inwelly.com\/?p=1061"},"modified":"2024-04-21T05:28:00","modified_gmt":"2024-04-20T20:28:00","slug":"dompdf-does-not-render-images-well-with-remote-urls","status":"publish","type":"post","link":"https:\/\/inwelly.com\/?p=1061","title":{"rendered":"Dompdf\u00a0does\u00a0NOT\u00a0render images well with remote urls"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>use Dompdf\\Options;<\/p>\n\n\n\n<p>use finfo<\/p>\n<\/blockquote>\n\n\n\n<p>\/\/ function<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;function encode_img_base64( $img_path = false, $img_type = &#8216;png&#8217; ){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;if( $img_path ){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\/\/convert image into Binary data<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$img_data = fopen ( $img_path, &#8216;rb&#8217; );<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$img_size = filesize ( $img_path );<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$binary_image = fread ( $img_data, $img_size );<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;fclose ( $img_data );<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\/\/Build the src string to place inside your img tag<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$img_src = &#8220;data:&#8221;.$img_type.&#8221;;base64,&#8221;.str_replace (&#8220;\\n&#8221;, &#8220;&#8221;, base64_encode($binary_image));<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return $img_src;<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return false;<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}&nbsp;<\/p>\n<\/blockquote>\n\n\n\n<p>\/\/ run\u00a0<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>$target = \u201c\/user\/www\/<a href=\"http:\/\/test.com\/uploads\/images\/16a8f22beb11808dc84c17fba020a73a.jpg\">test.com\/uploads\/images\/16a8f22beb11808dc84c17fba020a73a.jpg<\/a>\u201d;<\/p>\n\n\n\n<p>$finfo = new finfo();<\/p>\n\n\n\n<p>$fileMimeType = $finfo-&gt;file($target, FILEINFO_MIME_TYPE);<\/p>\n\n\n\n<p>$result_base64_code = encode_img_base64($target,$fileMimeType);<\/p>\n\n\n\n<p>$img = $result_base64_code ;<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$options = new Options();<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$options-&gt;setIsRemoteEnabled(true);<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$options-&gt;setIsHtml5ParserEnabled(true);<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;$dompdf = new \\Dompdf\\Dompdf($options);<\/p>\n\n\n\n<p>\u2026<\/p>\n\n\n\n<p>\u2026<\/p>\n\n\n\n<p>$dompdf-&gt;loadHtml($html);<\/p>\n<\/blockquote>\n\n\n\n<p>\/\/ result html($html) to sample.pdf<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>123 &lt;br> 222 &lt;br> &lt;img src=\u2018$img\u2019><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>use Dompdf\\Options; use finfo \/\/ function &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;function encode_img_base64( $img_path = false, $img_type = &#8216;png&#8217; ){ &nbsp; &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1061"}],"version-history":[{"count":1,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1061\/revisions"}],"predecessor-version":[{"id":1062,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/posts\/1061\/revisions\/1062"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=\/wp\/v2\/media\/1251"}],"wp:attachment":[{"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inwelly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}