Dompdf远程图片在PDF中未显示

30

在我的服务器上,通过dompdf生成的PDF文件无法显示远程URL图片(本地图片可以正常工作)。后来我知道它需要一些设置才能渲染远程图片。

allow_url_fopen = true => i can not as server control is not in my hand.(and no one will suggest to do this due to security reasons)
read/write access to the DOMPDF_TEMP_DIR (already have this)
DOMPDF_ENABLE_REMOTE = true (already have this)

为了解决allow_url_fopen的问题,我在本地设置为false,现在输出与服务器相同。

现在的问题是,我想在allow_url_fopen = false的情况下显示远程图片到PDF中

  • 我尝试了5-10种不同的方法。
  • 我尝试通过设置头文件来在php文件中显示图像,然后在pdf中显示php链接。
  • 我也尝试通过绝对路径来显示图像,但都没有效果。
  • 我尝试通过一个函数获取图像,然后在php文件中显示,但没有成功。

请问有谁能建议我如何在pdf中显示图像。我一直得到的错误是...

Image not found
http://localhost/dompdf/image.php

并且

Image not found
http://localhost/dompdf/image.jpg

你找到任何解决方案了吗?我也遇到了同样的问题,图片在本地主机上显示正常,但在生产服务器上却不行,路径没问题,图片已经存在于服务器上。 - Irfan Ahmed
还没有找到它。 - TechCare99
27个回答

61

尝试

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

8
我必须使用Options类,代码如下:use Dompdf\Options; - Gavin
15
在我的情况下,$dompdf = new DOMPDF(); $dompdf->set_option('isRemoteEnabled', TRUE);就足够了(即不需要额外的类初始化)。 - ᴍᴇʜᴏᴠ
2
set_option()已被弃用。 - Alex
1
@aexl...我们真的不需要其他的代码行。给你点赞。谢谢。 - Vivek
谢谢,$options->set('isRemoteEnabled', true); 运行得很顺利 :) - sohail amar
显示剩余4条评论

51

我遇到了同样的问题,dompdf 在线服务器上找不到图片

我找到了它的解决方案,您只需要仔细检查图像路径即可,

考虑您在线服务器上的图像路径

<img src="http://www.example.com/public/images/thumb.png">

你只需要将其更改为:

<img src="public/images/thumb.png">

注意:确保所有设置与您已经进行的设置相同。

我希望这可以帮助您。


+1 搜了半个小时的问题,这个真的帮了很大的忙。 - Karan Thakkar
@irfan Ahmed 谢谢你的解决方案 - Mukhila Asokan
如果图像名称中有空格,则会出现问题。 - Rajesh Vishwakarma
1
谢谢。这对我很有帮助。顺便说一句,我不得不去掉public。"<img src="images/thumb.png">"希望这能帮到其他人。 - Manuja Jayawardana

15

有两个需要注意的事项。

  1. 如果使用来自相同服务器的图像,则使用完整的目录路径,例如:/var/www/html/project_folder/images/logo.jpg

  2. 列表项 使用 JPEG 图像而不是 png 或其他类型。


4
对于任何遇到加载图片问题的人。上述解决方案提到了使用绝对路径。但是,除非您设置chroot选项,否则这种方法是行不通的。超出chroot目录的任何文件都将无法加载。例如:将chroot设置为从WordPress的wp content目录中读取:
$options = new Options();    
$options->set( 'chroot', WP_CONTENT_DIR );
$dompdf = new Dompdf( $options );

4

我曾经遇到过与图片相关的问题,我的解决方案是:

getcwd().'path/to/image'

这对我有效!


4

在进行了大量研究后,我遇到了相同的问题,最终找到了解决方案。首先,您需要获取dompdf的选项,然后将 isRemoteEnabled 设置为 true。

    $options = $dompdf->getOptions(); 
    $options->set(array('isRemoteEnabled' => true));
    $dompdf->setOptions($options);

3

进入 dompdf/vendor/dompdf/dompdf/src/ 目录并打开 Options.php 文件。

找到并将 private $isRemoteEnabled = false; 更改为 private $isRemoteEnabled = true;


这个选项对于我的CI项目有效。 - Sreejith vs

2
尝试使用以下解决方案来解决您的问题。图像网址是样例。我正在使用用于生成PDF的Laravel“barryvdh/laravel-dompdf”包(版本为“^2.0”)。
 <img src="data:image/png;base64,{{ base64_encode(file_get_contents( "https://example.com/image/domo.png" )) }}"> 

enter image description here


2

DOMPDF版本1.1.1

我尝试了上述所有选项,唯一有效的方法是:

对于绝对路径(例如:https://website/image.jpg),我必须执行以下操作:

#1 - 使用这些选项:

$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$options->set('tempDir', '/tmp'); //folder name and location can be changed
$dompdf = new Dompdf($options);

#2 - 在添加上述代码的php代码同级别处创建tmp文件夹。

对于相对路径(例如:/contents/test_image.jpg),我需要:

#1 - 使用以下选项:

$options = new Options();
$options->set('tempDir', '/tmp');
$options->set('chroot', __DIR__);
$dompdf = new Dompdf($options);

#2 - 在添加了上述代码的同一级别的php代码中创建此tmp文件夹(如绝对路径所示)

最终,为了使用绝对路径和相对路径,我的代码如下:

<?php

use Dompdf\Dompdf;
use Dompdf\Options;

require __DIR__ . "/vendor/autoload.php";

$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$options->set('tempDir', '/tmp');
$options->set('chroot', __DIR__);

$dompdf = new Dompdf($options);

ob_start();
require __DIR__ . "/contents/my_html.php";
$dompdf->loadHtml(ob_get_clean());


$dompdf->setPaper("A4", "portrait");
$dompdf->render();
$dompdf->stream("file.pdf", ['Attachment' => false]);

我的项目结构如下:

pdftest/
  |__contents/
  |  |__my_html.php
  |  |__jpg_test.jpg
  |__tmp/
  |__vendor/* //composer stuff
  |__index.php

我的HTML看起来像index.php这样

<html>
<header>
    <style>
        body { display:flex; flex-direction:column; align-items: center; justify-content: center; }
        table { border: 1px solid #a1a1a1; }
        table th { border-bottom: 1px solid #a1a1a1; }
    </style>
</header>
<body>
    <table>
        <tr>
            <th>column 1</th>
            <th>column 2</th>
            <th>column 3</th>
            <th>column 4</th>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>
                <img width="40" src="contents/jpg_test.jpg" alt=""/>
                <img width="40" src="https://cdn.pixabay.com/photo/2021/10/14/15/11/cathedral-6709412_960_720.jpg" alt=""/>
            </td>
            <td>Data 3</td>
            <td>Data 4</td>
        </tr>
    </table>
</body>
</html>

2

我认为你可以添加这个

private function change_url_image($data, $url) {    
    $str = $url; //for example "http://localhost/yoursite/";
    $str2 = str_replace($str, "", $data);
    return $str2;
}

更改图像的URL


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接