php DomPDF:图像不可读或为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25558449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:53:15  来源:igfitidea点击:

DomPDF: Image not readable or empty

phphtmlpdflaraveldompdf

提问by David P. P.

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

出于某种原因,DomPDF 不会渲染包含在正在解析的 html 中的图像:

PDF Image missing

PDF 图像丢失

However, the image is rendered on the page when it is returned as html:

但是,当图像作为 html 返回时,图像会呈现在页面上:

HTML Image exists

HTML 图像存在

I've looked at these issues and have make sure that DOMPDF_ENABLE_REMOTE is set to true and verified file permissions:
dompdf image not real image not readable or empty
Image error in DOMPDF for ZF2

我查看了这些问题,并确保 DOMPDF_ENABLE_REMOTE 设置为 true 并验证文件权限:
dompdf image not real image not readable or empty
Image error in DOMPDF for ZF2

Are there any other things that I should be checking for?

还有什么我应该检查的东西吗?

回答by David P. P.

Following helped me like charm, at least localy, and even with

跟随帮助我喜欢魅力,至少在本地,甚至

def("DOMPDF_ENABLE_REMOTE", false);

The solution is to change the image SRC to the absolute path on the server, like this:

解决办法是把图片src改成服务器上的绝对路径,像这样:

<img src="/var/www/domain/images/myimage.jpg" />

All of the following worked for me:

以下所有内容都对我有用:

<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'/placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'\placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'./placeholder.jpg';?>"/>

$_SERVER["DOCUMENT_ROOT"] is C:/wamp/www/ZendSkeletonApplication/public

$_SERVER["DOCUMENT_ROOT"] 是 C:/wamp/www/ZendSkeletonApplication/public

Thanks to this: lost in code

感谢这个:迷失在代码中

回答by Patrice Flao

Ok I had the same problem with image using :

好的,我在使用图像时遇到了同样的问题:

<img id="logo" src="/images/flags/fr.png" width="50" alt="Logo">

But if I add a . before /images, without changing anything in dompdf_config.custom.inc, it works

但是如果我添加一个 . 在 /images 之前,无需更改 dompdf_config.custom.inc 中的任何内容,它就可以工作

<img id="logo" src="./images/flags/fr.png" width="50" alt="Logo">

Hope it helps

希望能帮助到你

回答by Jon

As there was another answer that suggests enabling the remote option in module.config.phpand I can't yet add comments, I thought it would be best to answer that this file does not exist in newer versions of DomPDF.

由于有另一个答案建议启用远程选项module.config.php并且我还不能添加评论,我认为最好回答这个文件在较新版本的 DomPDF 中不存在。

If you need to include remotely stored images in a newer version you have to pass it as an option to the constructor:

如果您需要在较新版本中包含远程存储的图像,则必须将其作为选项传递给构造函数:

$dompdf = new Dompdf(array('enable_remote' => true));

This fixed the issue I had.

这解决了我遇到的问题。

回答by André DLC

Now (May 2018) the correct way is :

现在(2018 年 5 月)正确的方法是:

$options = new Options();
$options->set('isRemoteEnabled',true);      
$dompdf = new Dompdf( $options );

回答by Owen Davey

None of the solutions here worked for me. Instead I just base64 encoded the image and then it worked. You can use this tool.

这里没有一个解决方案对我有用。相反,我只是对图像进行了 base64 编码,然后它就可以工作了。您可以使用此工具

回答by BlackPearl

You can use base64 encoded image

您可以使用 base64 编码的图像

<img src="{{'data:image/png;base64,' . base64_encode(file_get_contents(@$image))}}" alt="image" >

回答by Nasser Hekmati

I solve this problem by using external CSS's full path. This one worked on my linux ubuntu server :

我通过使用外部 CSS 的完整路径解决了这个问题。这个在我的 linux ubuntu 服务器上工作:

<link href="{{ public_path('css/style.css') }}" />

<link href="{{ public_path('css/style.css') }}" />

<img src="{{ public_path('images/image.jpg') }}" />

<img src="{{ public_path('images/image.jpg') }}" />

and work on image.

并在图像上工作。

回答by egorychmaster

In path :

在路径中:

vendor/dino/dompdf-module/config/module.config.php

vendor/dino/dompdf-module/config/module.config.php

change settings

更改设置

enable_remote' => false,

enable_remote' => 假,

то true.

是真的。

回答by Japo Domingo

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.

对于我们的用例,我们必须将页面上的所有图像转换为 base64,因为 pdf 应该可以离线使用。我们的代码位于控制器类中,但您可以根据需要对其进行修改。

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 '';
        }
    }