php Dompdf 远程图像未在 pdf 中显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15153139/
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 08:40:47  来源:igfitidea点击:

Dompdf remote image is not displaying in pdf

phpimagedompdf

提问by TechCare99

On my server pdf generated via dompdf was not displaying images. Because they are remote url images.(local images are working fine) then I come to know that it needs some settings to render remote images.

在我的服务器上,通过 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)

So to sure the issue of allow_url_fopen, I set false in my localhost which is now giving the same output as server.

所以为了确定allow_url_fopen的问题,我在我的本地主机中设置了false,它现在提供与服务器相同的输出。

So, now the issue is now I want to display remote images in PDF with allow_url_fopen = false

所以,现在的问题是现在我想使用 allow_url_fopen = false 以 PDF 显示远程图像

  • I tried almost 5-10 unique ways to do this.
  • I tried to display image in php file (via setting headers) and then displaying the php link in pdf
  • I tried to display image via absolute path to php too but nothing worked.
  • I tried via getting image via curl in a function and then displaying it in a php file ...but no luck.
  • 我尝试了近 5-10 种独特的方法来做到这一点。
  • 我试图在 php 文件中显示图像(通过设置标题),然后在 pdf 中显示 php 链接
  • 我也尝试通过 php 的绝对路径显示图像,但没有任何效果。
  • 我尝试通过函数中的 curl 获取图像,然后将其显示在 php 文件中……但没有运气。

Is there anyone can suggest me how can I display image in pdf please. The error I am always getting is ...

有没有人可以建议我如何以pdf格式显示图像。我总是遇到的错误是......

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

and

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

回答by theTypan

Try

尝试

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

回答by Irfan Ahmed

I had the same problem, dompdf image not found on live server

我遇到了同样的问题, 在实时服务器上找不到 dompdf 图像

I found its solution, you just need to double check the image path,

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

Considering your live server image path

考虑您的实时服务器图像路径

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

You just need to change it to,

你只需要把它改成,

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

Note: Make sure that, all settings are same as you have already made.

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

I hope this will help you.

我希望这能帮到您。

回答by daudichya

There are two things to take care of.

有两件事需要注意。

  1. If using image from same server then use full directory path e.g. /var/www/html/project_folder/images/logo.jpg

  2. List item Use JPEGimage instead of pngor other types.

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

  2. 列表项使用JPEG图像代替png或其他类型。

回答by Stephen Schwink

Can you reach those URLs in your web browser on the machine that you're using to open the PDF? If not, the PDF reader won't be able to either.

您能否在用于打开 PDF 的机器上的 Web 浏览器中访问这些 URL?如果没有,PDF 阅读器也将无法使用。

I suspect that the "localhost" domain means that those URLs are only visible from the web server that generated the PDF. You need to output a url like http://example.com/dompdf/image.jpg

我怀疑“localhost”域意味着这些 URL 只能从生成 PDF 的 Web 服务器中看到。您需要输出像http://example.com/dompdf/image.jpg这样的网址

(To step around this issue, bear in mind that there are good reasons not to use remote images. The document will look bad if the viewer is not connected to the internet, for example. Is it possible to just embed the images directly in the document?)

(要解决这个问题,请记住有充分的理由不使用远程图像。例如,如果查看器没有连接到互联网,文档会看起来很糟糕。是否可以直接将图像嵌入到文档?)

回答by Sumit Kumar Gupta

I am giving you an example with function

我给你一个函数的例子

  $html = $this->output->get_output();
        $this->load->library('pdf');
        $this->dompdf->loadHtml($html);
        $this->dompdf->set_option('isRemoteEnabled', true);
        $this->dompdf->setPaper('A4', 'portrait');
        $this->dompdf->render();
        $this->dompdf->stream("studentidcard.pdf", array("Attachment"=>0));

Set isremoteenabled true to show remote images

设置 isremoteenabled true 以显示远程图像

回答by Lego

Hi my solution to this problem was adding the enabling the allow_url_fopen in my php.ini file

嗨,我对这个问题的解决方案是在我的 php.ini 文件中添加启用 allow_url_fopen

回答by user2649610

I think you could add this

我想你可以添加这个

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

to change url for image

更改图片的网址

回答by karthikeyan ganesan

$data = {{any html content with src & href links}}

Sometimes it may happen based on the unsecure url like httpswith invalid ssl certificate. so the renderer of dompdf cannot be accessed the url for fetching the content. check your content has any unsecure url. If you're using that url with process as unsafeoption in browser, please use the following method. It replace the url to abspath

有时它可能会基于不安全的 url 发生,例如https使用无效的 ssl 证书。因此无法访问 dompdf 的渲染器以获取内容的 url。检查您的内容是否有任何不安全的网址。如果您process as unsafe在浏览器中使用带有选项的网址,请使用以下方法。它将 url 替换为 abspath

$url = "https://example.com";
$abspath="/home/public_html/example" || getcwd();
$data =  preg_replace('#(src=\"[\s\r\n]{0,})('.$url.')#',''.$abspath, $data);
$data =  preg_replace('#(href=\"[\s\r\n]{0,})('.$url.')#',''.$abspath, $data);

回答by Tangzy Isemin

Go to library/dompdf/scr/option

去图书馆/dompdf/scr/option

change private $isRemoteEnabled = false; to private $isRemoteEnabled = true;

更改私有 $isRemoteEnabled = false; 私人 $isRemoteEnabled = true;

回答by Soubhagya Kumar Barik

I had the same issue after doing a lot of R&D finally got the solutions.First you need to get the options of dompdf then set isRemoteEnabledwith true.

我做了很多研发后遇到了同样的问题,终于得到了解决方案。首先你需要得到dompdf的选项,然后设置isRemoteEnabled为true。

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