簡體   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