繁体   English   中英

DomPDF:图像不可读或为空

[英]DomPDF: Image not readable or empty

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

PDF 图像丢失

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

HTML 图像存在

我查看了这些问题并确保将 DOMPDF_ENABLE_REMOTE 设置为 true 并验证文件权限:
dompdf 图像不是真实图像不可读或为空
ZF2 的 DOMPDF 中的图像错误

还有其他我应该检查的东西吗?

以下帮助我喜欢魅力,至少在本地,甚至

def("DOMPDF_ENABLE_REMOTE", false);

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

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

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

<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"] 是 C:/wamp/www/ZendSkeletonApplication/public

多亏了这个:迷失在代码中

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

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

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

这解决了我遇到的问题。

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

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

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

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

希望能帮助到你

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

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

您可以使用 base64 编码的图像

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

如果您的所有图像和其他图像都在执行脚本的同一台服务器上,您实际上并不需要打开isRemoteEnabled

为什么它不加载您的图像

DomPdf 保护您免受通过它的攻击。 根据文档,以下内容不起作用:

$dompdf = new Dompdf();
$dompdf->getOptions()->getChroot(); // something like 'C:\\laragon\\www\\your-local-website\\vendor\\dompdf\\dompdf'

$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
    <body>
        <img src="C:\\laragon\\www\\your-local-website\\public\\img\\logo.png">
    </body>
</html>
HTML;

$dompdf->loadHtml($html);

您应该将 CHROOT 更改为您想要的绝对路径

$dompdf->getOptions()->setChroot("C:\\laragon\\www\\your-local-website\\public");

然后您可以将任何带有src<img>/public文件夹内(可以嵌套)插入到 HTML 中。

安全说明

将 Chroot 设置为您的应用程序根文件夹似乎很容易,但不要. 它打开了一扇令人讨厌的门,你想关上它。 假设/public中没有关键脚本,只有图像、公共文档、路由等。

使用说明

请注意,我使用的目录分隔符与文档中使用的目录分隔符不同。 相信最好的做法是按照以下方式做一些事情:

define('DS', DIRECTORY_SEPARATOR);

$public = ABSPATH . DS . 'public'; // ABSPATH is defined to be __DIR__ in root folder of your app
$image = $public . DS . 'logo' . DS . 'logo-md.png';

/* Optional */
$saveLocation = ABSPATH . DS . '..' . DS . 'private' . DS . 'invoices'; // Note that save location doesn't have to be in '/public' (or even in 'www')

$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <style type="text/css">
            * {
                font-family: DejaVu Sans !important;
            }
            @page {
                margin: 0;
                padding: 0;
            }
            html, body {
                margin: 0;
                min-height: 100%;
                padding: 0;
            }          
        </style>
    </head>
    <body>
        <img src="{$image}">
    </body>
</html>
HTML;

$dompdf = new Dompdf();
$dompdf->getOptions()->setChroot($public);
$domPdf->loadHtml($html, 'UTF-8');
$domPdf->setPaper('A4', 'portrait');
$domPdf->render();

/* either */
$domPdf->stream($filename); // filename is optional

/* or */
$outputString = $domPdf->output();
$pdfFile = fopen($saveLocation, 'w');
fwrite($pdfFile, $outputString);
fclose($pdfFile);

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

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

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

并处理图像。

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

在路径:

供应商/dino/dompdf-module/config/module.config.php

更改设置

enable_remote' => 假,

这是真的。

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

这是代码:


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

我在加载本地图像时遇到了这个问题并发现

$dompdf->getOptions()->setChroot($path_to_images);

还不够,我还得设置

chdir($path_to_images);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM