DomPDF: Image not readable or empty

后端 未结 9 1236
梦如初夏
梦如初夏 2020-12-15 16:41

For some reason, DomPDF won\'t render an image included in the html that is being parsed:

\"PDF

9条回答
  •  春和景丽
    2020-12-15 17:31

    For our use case we had to convert all the images on the page to base64 since the pdf should be usable offline. Our code lives inside a controller class but you can modify that to your needs.

    Here's the code:

    
        /**
         * Convert images to Base64 so it's included in the PDF.
         *
         * @param $html  Full html render of the page.
         */
        public function convertReportImagesToURI($html): string
        {
            $doc = new DOMDocument();
            libxml_use_internal_errors(true);
            $doc->loadHTML($html);
            $tags = $doc->getElementsByTagName('img');
            $imgArr = array();
    
            // Iterate over all image tags.
            foreach ($tags as $tag) {
                // Get the src attribute.
                $imgSrc = $tag->getAttribute('src');
    
                // Convert to base64.
                $base64src = self::getImageDataURI($imgSrc);
                $tag->setAttribute('src', $base64src);
            }
            return $doc->saveHTML();
        }
    
        /**
         * This does the actual encoding.
         */
        public static function getImageDataURI($image, $mime = ''): string
        {
            // Director::absoluteURL('/') gets the base url of the site.
            // We had to remove the leading slash of the image hence the use of substr.
            // If your images have absolute urls you will need to modify this.
            $imageLocation = Director::absoluteURL('/') . substr($image, 1);
            // Get the image location. remove leading slash on image url.
            return 'data:' . self::get_image_mime_type($imageLocation) . ';base64,' . base64_encode(file_get_contents($imageLocation));
        }
    
        /**
         * https://stackoverflow.com/a/45054843
         * @param $image_path
         * @return string
         */
        public static function get_image_mime_type($image_path): string
        {
            $mimes  = array(
                IMAGETYPE_GIF => "image/gif",
                IMAGETYPE_JPEG => "image/jpg",
                IMAGETYPE_PNG => "image/png",
                IMAGETYPE_SWF => "image/swf",
                IMAGETYPE_PSD => "image/psd",
                IMAGETYPE_BMP => "image/bmp",
                IMAGETYPE_TIFF_II => "image/tiff",
                IMAGETYPE_TIFF_MM => "image/tiff",
                IMAGETYPE_JPC => "image/jpc",
                IMAGETYPE_JP2 => "image/jp2",
                IMAGETYPE_JPX => "image/jpx",
                IMAGETYPE_JB2 => "image/jb2",
                IMAGETYPE_SWC => "image/swc",
                IMAGETYPE_IFF => "image/iff",
                IMAGETYPE_WBMP => "image/wbmp",
                IMAGETYPE_XBM => "image/xbm",
                IMAGETYPE_ICO => "image/ico");
    
            if (($image_type = exif_imagetype($image_path))
                && (array_key_exists($image_type, $mimes))) {
                return $mimes[$image_type];
            } else {
                return '';
            }
        }
    

提交回复
热议问题