【问题标题】:DomPDF: Image not readable or emptyDomPDF:图像不可读或为空
【发布时间】:2014-10-22 21:14:58
【问题描述】:

由于某种原因,DomPDF 不会呈现正在解析的 html 中包含的图像:

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

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

我还有什么需要检查的吗?

【问题讨论】:

  • PHP 设置allow_url_fopen 是否设置为true?如果您使用的是 0.6.x,您可以加载 dompdf/www/setup.php 以查看安装中是否有任何红色标记。

标签: php html pdf laravel dompdf


【解决方案1】:

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

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

Thanks to this: lost in code

【讨论】:

  • 将图像转换为 base64 对我有用$imageUrl = (string) Image::make(public_path($path)) -&gt;fit(80, 80) -&gt;encode('data-url');
  • 添加绝对路径对我有用,而不是相对路径。
【解决方案2】:

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

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

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

这解决了我遇到的问题。

【讨论】:

  • 在 Laravel 上为我工作,谢谢
  • 这对我来说适用于远程图像。谢谢分享!
【解决方案3】:

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

<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">

希望对你有帮助

【讨论】:

  • 有效的原因是因为您最初是在根目录中寻找图像目录。 “./”只是表示“查看此目录”。您实际上可以删除“./”,它仍然可以工作。
  • 你统治。我正要重新安排很多东西!
  • 经过所有这些尝试,我给出了...这就是解决方案!
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    可以使用base64编码的图片

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

    【讨论】:

    • 谢谢,伙计。我试图从 S3 获取图像以显示在 PDF 中。对我来说就像一个魅力?
    【解决方案6】:

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

    &lt;link href="{{ public_path('css/style.css') }}" /&gt;

    &lt;img src="{{ public_path('images/image.jpg') }}" /&gt;

    然后处理图像。

    【讨论】:

    • 在提供示例代码时,请作为示例提供给提问者解决问题。 (IE 示例代码具有图像的完整路径而不是样式表,因为这个问题是关于图像的。)
    【解决方案7】:

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

    为什么它不加载你的图片

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

    $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");
    

    然后您可以将任何&lt;img&gt;src/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);
    

    【讨论】:

      【解决方案8】:

      这里的解决方案都不适合我。相反,我只是对图像进行了 base64 编码,然后它就可以工作了。你可以使用这个tool

      【讨论】:

      • 与 .没有一个解决方案奏效。由于此评论,最后一次尝试 base64 并且它有效。
      【解决方案9】:

      在路径中:

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

      更改设置

      enable_remote' => 假,

      这是真的。

      【讨论】:

      【解决方案10】:

      对于我们的用例,我们必须将页面上的所有图像转换为 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 '';
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2014-12-24
        • 2017-03-16
        • 2021-12-17
        • 1970-01-01
        • 2014-06-08
        • 1970-01-01
        • 2021-09-15
        • 1970-01-01
        • 2022-06-29
        相关资源
        最近更新 更多