Image is not shown in PDF file using dompdf - php

I am using DOMPDF to convert the html into PDF and after converting I'm sending that PDF file to user mail id.
Everything is working perfectly but in PDF file I am not able to see the image of logo of my site. I also searched in stackoverflow for previous question such as :- error in pdf image using dompdf ,
dompdf and img tag, image wont show
I also set DOMPDF_ENABLE_REMOTE to TRUE and DOMPDF_PDF_BACKEND to CPDF
my image tag is :-
<img src="http://www.example.com/clients/myprojects/images/logo.png" alt="" /></a>
I'm giving full path of my website but still it does not show image in my PDF file.
Thanks in advance.

use full directory path with .jpg image

It works for me.
<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';?>"/>

Don't put full path of URL, just put foldername/filename.jpg
Example:
<img src="uploads/kuruvi.jpg">

I my case i spend 2 hour finally i got answer
img src not working with url or path you should covert it base64 format
<?php
$data = file_get_contents('https://teafloor.com/wp-content/themes/teafloor2-0/assets/images/logo.png');
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
?>
<?php echo '<div class="company-logo">
<img src="'.$base64.'" alt="base" />
</div>';
?>
or
<img src="<?php echo $base64; ?>" alt="base" />

if I recall it correctly you can put there a full system path to that image - absolute or relative to your script

Remove the alt attribute and changing it from a self-closing tag (/>) to a normal one (>). It worked for me.
Note: dompdf seems to have a bad response to any inline styles on img tags, so I suggest removing them if you´re using any.

//use full directory path something like this
$currentsite = getcwd();
$html = <<<HTML
<html>
<img src="{$currentsite}/sites/all/modules/certificate_handler/image002.png" alt="" /></a>
</html>
HTML;

I did this:
$imgurl = $graficas[$i];
$path_parts = pathinfo($imgurl);
$graficas[$i] = $path_parts['filename'].".jpg";
And works. I hope this would help you too

DOMPDF
working now with png,jpg.
here what I used.
I have index.php file in route folder and image in subfolder namely images.
$image ="images/".$_FILES['image_index_name']['name'];
$htm= '';

You just have to change the line def("DOMPDF_ENABLE_REMOTE", FALSE); to true in the file DOMPDF_CONFIG.INC

Now latest DOMPDF version another, but have the same problem on PHP v5.3
Upgrade to v5.4 or higher
The problem has solved.

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 it's very simple

Related

DOMPDF with laravel - cannot load images in html file

I'm trying to generate pdf file with laravel and dompdf lib. Here is my code from controller:
public function create(Request $request) {
$pdf = new Dompdf();
$html = Storage::disk('local')->get('public/pdf/template.html');
$pdf->loadHtml($html, 'UTF-8');
$pdf->setPaper('A4', 'portrait');
$pdf->render();
$filename = "Hi!";
return $pdf->stream($filename, ["Attachment" => false]);
}
And in template.html I got some html, somewhere there I got images like this:
<img src="img/logo.png" alt="BTS">
But dompdf cant write this image: Image not found or type unknown. My file system is:
.pdf
...img
......here is images
...template.html
...*some_fonts_files*
But at the same time, fonts are loading fine. What should I do to render images in template.html?
I believe that for dompdf you need to pass a properly accessable url rather than relative paths.
Try
<img src="{{ asset('img/logo.png')}}" alt="BTS">
Alternatively if asset doesn't work please try
<img src="{{ public_path('img/logo.png')}}" alt="BTS">
Also please note that asset uses the public directory as the base_url to calculate the 'asset' from.
Me too i struggled for the same error for 3 days. Also when i tried tricks mentioned above, none of them worked too. But when i changed the image file to .jpg file(extension) everything worked well.
<img src="{{ public_path('img/logo.jpg') }}" >
Laravel version: 5.8.38
PHP version: 7.3
barryvdh/laravel-dompdf version: 0.8.6
You can just print the image path as the src attribute of the <img> tag.
Suppose you have the path to the location of your image in a variable:
$img = asset($imgen);
Yust echo it out like this:
<img src="#php echo $img #endphp">
There seems to be a need to set the protocol and add an additional option:
$pdf->setOptions(['isRemoteEnabled' => true]);
$pdf->getDomPDF()->setProtocol($_SERVER['DOCUMENT_ROOT'])
Or:
$pdf->setProtocol($_SERVER['DOCUMENT_ROOT']);
After this src="/storage/myimage.png" will work.
I tried all tips!! The only one solution that works for me is:
header {
background-image: url("https://example.org/logo.jpg");
}
and
$pdf->setOptions(['isRemoteEnabled' => true]);
$pdf->getDomPDF()->setProtocol($_SERVER['DOCUMENT_ROOT'])

Display image from outside the web root ( public_html )

Background:
I have a folder with images called uploads or images located outside the website root. ( outside of public_html )
I am trying to print the image inside let's say image.php.
Rules for doing it:
I am trying to do it without using alias mod_rewrite in .htaccess.
Without showing a black background and image in the middle ( I don't want it like when browsing domain.com/image.png. like the example of the picture I mentioned below.
Without using another page and pass it as get.
What I tried:
I checked many questions, one of them is this and another is
this.
From following the current questions asked above and other tutorials, I came up with
this:
<?php
$location = dirname($_SERVER['DOCUMENT_ROOT']);
$image = $location . '/public_html/images/banned.png';
header('Content-Type:image/png');
header('Content-Length: ' . filesize($image));
echo file_get_contents($image);
?>
<img src=" <? echo $image; ?> "/>
It works fine, and here is an example:
Gyazo
However, this is not what I am looking for, or trying to do. Again, I am trying to view it as a normal image src as it will be used for a profile picture or other usages.
Any help will be much appreciated. Thanks in advance!
Change your image.php to
<?php
function image() {
$location = dirname($_SERVER['DOCUMENT_ROOT']);
$image = $location . '/public_html/images/banned.png';
return base64_encode(file_get_contents($image));
}
?>
In another file where you want to display that image, let's say test.php:
<?php
include("image.php");
?>
<img src='data:image/png;base64,<?= image(); ?>' >
This:
echo file_get_contents($image);
?>
<img src=" <? echo $image; ?> "/>
is wrong for a few reasons. First, you cannot mix raw image content with HTML markup that way. Also src specifies the URL of the image not the raw content.
You can either move your echo file_get_contents($image); and related code to separate file, i.e. image.php, then reference it in your src, passing image name as argument (i.e. image.php?img=foo.jpg) - note that if you do this wrong, you will be open to directory traversal attack). Alternatively you can try to use rectory traversal attackdata URI scheme as src argument and pass raw content directly that way.

Displaying image from different disk

Hi i want to show images from different disk.
Here is my code.
is it possible to do that ?
$a = "D:/img/1.jpg";
<img src="<?php echo $a; ?>" alt="...">
Im using this on my localhost.This is not for the web.
i add screenshot here.
when i come above the img it shows the link. but not show img. and i use lightbox.
but without lightbox its not show again.
example
The sample you provided/attached is only doable if you load the html file directly to your browser. It is not possible if you load it via localhost. In your case, you will have to do something like this:
<?php
$image = file_get_contents('D:/img/1.jpg');
$image_codes = base64_encode($image);
?>
<image src="data:image/jpg;charset=utf-8;base64,<?php echo $image_codes; ?>" />
Reference: How to retrieve and show images from another drive using src attribute in <img> tag?
I'd suppose to use such URLs with protocol: $a = 'file:///D:/img/1.jpg';

Load relative path of images instead of <?php echo $img ?>

The timthumb function is not working after the security setting of web server is raised.
Images with "http://...." in URL are blocked.
I have found that <?php echo $img ?> was used to load the image.
Original code:
<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?h=100&w=100&zc=1&src=<?php echo $img ?>" alt="<?php the_title_attribute(); ?>" width="100" height="100" />
Would someone please tell me how can I modify the code so that instead of
http://domain.com/wp-content/themes/a/scripts/timthumb.php?h=100&w=100&zc=1&src=http://domain.com/wp-content/uploads/thumbnail.jpg
the image URL will be
http://domain.com/wp-content/themes/a/scripts/timthumb.php?h=100&w=100&zc=1&src=../wp-content/uploads/2013/10/thumbnail.jpg
?
The code inside timthumb.php is super long. I will post the code if it's needed.
I know almost nothing about php code and only use plugins to build Wordpress websites.
You would have save my life. Thank you very very much!
It depends on where it is getting $img value from. If this is just a field on your article (through the extra parameters on the bottom), you could simply alter the url in your post. Alternatively, you could process the path, stripping out the http://domain.com/ and replace it with ../,
a la:
$img = str_replace("http://domain.com/", "../", $img);
just before echoing it out on to the page.

how calling the images in a folder with php

I want to ask about calling the images in a folder with php
The first I've made ​​a file to declare the image url
$url_folder_gambar = 'http://localhost/mysite/assets/img/';
I then call the php but the picture did not come out. what's wrong?
<img src="<?php echo $url_folder_gambar . people.png?>"
please help me
It looks like a syntax error. Try this:
<img src="<?php echo $url_folder_gambar;?>people.png" />
$url_folder_gambar path of the image file, some thing like http://domain/path/imagefolder/
<img src="<?php echo $url_folder_gambar;?>people.png" >
OR
<img src="<?php echo $url_folder_gambar.'people.png';?>" >
Check browser code view source using ctrl+u, check the file path, and copy the source url and pasted into the borwser, whether it is rendered or not, If not there is no file in that location.

Categories