mPDF - Image not appearing in PDF when inserted via HTML - php

I'm using mPDF to create a PDF with PHP. When trying to insert an image with HTML it is not appearing in the generated PDF.
I'm trying to insert the image like this:
$pdf_html = '<div style="font-family: sans-serif;">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Test-Logo.svg/783px-Test-Logo.svg.png" width="100%" height="100%" />
<br>
<p>Testing testing.</p>
</div>';
$pdf->WriteHTML($pdf_html);
The rest of the content works fine but the image isn't appearing. Where am I going wrong?
UPDATE
I've found that I can embed image fine if I convert it to it's data URI (e.g. using https://ezgif.com/image-to-datauri/). This is fine for static images but I need to embed some of them dynamically using the URL. I tried using the following function to convert images to their data URI but this doesn't seem to work either:
function image_data_encode($image){
$data = file_get_contents($image);
$base64 = base64_encode($data);
$data_uri = "data:image/png;base64,".$base64;
return $data_uri;
}
Then in my PDF HTML code I would do something like:
$pdf_html = '<div style="font-family: sans-serif;">
<img src="' . image_data_encode("https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Test-Logo.svg/783px-Test-Logo.svg.png") . '" width="100%" height="100%" />
<br>
<p>Testing testing.</p>
</div>';
$pdf->WriteHTML($pdf_html);
As per comments and answer, I've:
Checked that curl is enabled
Tried escaping the double quotes
Checked error logs (no errors)
Tried using an absolute path to the image file
I'm developing this locally on my machine and can access the image files via my browser so I assume they should be accessible from my dev environment

Related

PHP: View PDF file inmy site

I need to display a PDF on a website and have it embedded inline.
The following code works if the data attribute is local, but not remote. I just get a large grey box in place of the PDF. I'm open to any solution as long as it works in all browsers. We are using Rails 3 and jQuery.
<object data='https://remotesite.com/uploads/pdf/5/mypdf.pdf' type='application/pdf' width='100%' height='100%'>
<p>
It appears your Web browser is not configured to display PDF files.
No worries, just <a href='https://remotesite.com/uploads/pdf/5/mypdf.pdf'>click here to download the PDF file.</a>
</p>
</object>
And i try this code
<iframe src="http://people.seas.harvard.edu/~chong/pubs/oopsla04.pdf" width="100%" height="300">
<p>Your browser does not support iframes.</p>
</iframe>
But not work with Arabic file like " ملف المتسابقين.pdf"
Thanks,

Can I inject posted image files into PDF using dompdf?

I have a form where users can fill input boxes. In some cases, they're allowed to select images too.
The problem is, I don't want to save the image file somewhere on our server and give PDF the URL like this:
//Put uploaded file on public/uploads/ folder
<img src="{{ $imagePath }}" style="height: 50px; width: 50px;">
Instead, I'd like to inject uploaded image directly into PDF file. Something like this:
PDF::load($data, 'A4', 'portrait')->inject('image', Input::file('resim'))->output();
//So I can reference it as {{ $image }} on PDF template, but image will be injected into PDF file
I'm beginner on PDF files and I'm not even sure if this is possible.
If it's possible, can anyone give me some advices?
Ps. I use DOMPDF package for Laravel 4. https://github.com/thujohn/pdf-l4
You will need to be able to reference the image somehow in your document. There are a few ways you could do this.
Access the temporary file from the uploads.
In your HTML file just reference the temporary file created by the upload as your image source, e.g.
<img src="<?php echo $_FILES['userfile']['tmp_name']; ?>">
Capture the content of the image and write it into the HTML as a data-uri value
dompdf understands data-uris and so you could take the uploaded file, convert it to a data-uri, and insert that into your HTML.
You do have to detect the image format, which can require a bit more work depending on your version of PHP. With v5.3+ you can use the finfo_file method. If that extension isn't available you can perform some basic content-type detection by parsing the file extension.
In my example I'm just specifying PNG as the image type.
<?php
// detect mime type, but I'm forcing PNG
$mime = 'image/png';
?>
<img src="<?php echo 'data:' , $mime , ';base64,' , base64_encode( file_get_contents( $_FILES['userfile']['tmp_name'] ) ); ?>">
I have this in my HTML...
<body>
<img src="{{ URL::asset('img/my-image.png') }}" />
my route where i have my img
and this in my controller
public function getPdf($id){
$html = View::make("backend.PDF.batch")->with('batchs',$batchs)->with('ad',$ad)->render();
return PDF::load($html, 'A4', 'portrait')->show();
}
and in the view don't show me the img they say
DOMPDF_ENABLE_REMOTE is set to FALSE

Image is not shown in PDF file using dompdf

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

Imagick is displaying blank images with php

I installed imagick and checked to make sure the class exists etc. I can get it to count images in tiffs and other things. However, I cant get it to display an image. I am using the following code :
header('Content-type: image/JPG');
$image = new Imagick(' ur to my jpg here ');
$image->thumbnailImage(100, 100);
echo $image;
But it displalys a blank image. When I look at the HTML code produced I get this:
<html>
<body style="margin: 0px;">
<img style="-webkit-user-select: none" src=" url to this page imagechange.php">
</body>
</html>
Can anyone explain why the image is not being displayed? Above the HTML appears the image src as the PHP page that I am using and not the image.

Can I embed a .png image into an HTML page?

How can I embed a .png file into a blank "file.html" so that when you open that file in any browser you see that image?
In this scenario, the image file is not linked to from the HTML, but rather the image data is embedded in the HTML itself.
There are a few Base64 encoders online to help you with this, and this is probably the best I've seen:
http://www.greywyvern.com/code/php/binary2base64
As that page shows your main options for this are CSS:
div.image {
width:100px;
height:100px;
background-image:url(data:image/png;base64,iVBORwA<MoreBase64SringHere>);
}
Or the <img> tag itself, like this:
<img alt="My Image" src="data:image/png;base64,iVBORwA<MoreBase64SringHere>" />
The 64base method works for large images as well. I use that method to embed all the images into my website, and it works every time. I've done it with files up to 2 MB size, JPEG and PNG.
I stumbled upon similar problem now and the solution is:
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use GD::Graph::pie;
use MIME::Base64;
my #data = (['A','O','S','I'],[3,16,12,47]);
my $mygraph = GD::Graph::pie->new(200, 200);
my $myimage = $mygraph->plot(\#data)->png;
print <<end_html;
<html><head><title>Current Stats</title></head>
<body>
<p align="center">
<img src="data:image/png;base64,
end_html
print encode_base64($myimage);
print <<end_html;
" style="width: 888px; height: 598px; border-width: 2px; border-style: solid;" /></p>
</body>
</html>
end_html
You can embed images using the methods listed in other answers, such as in HTML
<img alt="My Image" src="data:image/png;base64,base64-data-goes-here" />
or CSS
div#my-image {
width:150px;
height:100px;
background-image:url(data:image/png;base64,base64-data-goes-here);
}
For Base64 encoding, on Linux I use the standard base64 tool with wrapping turned off, like this:
base64 -w 0 my-image.png
and copy/paste the output text verbatim.
A quick Google search says you can embed it like this:
<img src="data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7"
width="16" height="14" alt="embedded folder icon">
But you need a different implementation in Internet Explorer.
http://www.websiteoptimization.com/speed/tweak/inline-images/
Use mod_rewrite to redirect the call to file.html to image.png without the URL changing for the user.
Have you tried just renaming the image.png file to file.html? I think most browser take mime header over file extension :)
You can embed a png image like you can embed jpg images or any type of images in html from your device or from the web .
Be sure that the type of the image is png when you are saving it on your device.This is the same way but I embed it as jpg.
<embed type="image/jpg" src="https://s3-us-west-2.amazonaws.com/textimgs/music/250px-Antonio_Vivaldi.jpg" width="500" height="500">
</EMBED>
I want to say thanks to Stack Overflow that let us ask and answer!!!!!!!!!!!!!!!!

Categories