DOMPDF with laravel - cannot load images in html file - php

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'])

Related

could not load image in html2pdf in codeigniter

I am trying to create a pdf file from html in my codeigniter project. The html file contains an image but it will not load in the pdf. I am using html2pdf library and here is my code.
example.php :
<img src="<?php echo base_url()?>assets/images/logo.jpg" alt="BulkSMS">
I am pasting only the <img> tag here, because it is a large file.
and here is my controller function
public function htmlToPdf($data) {
$this->html2pdf->folder(PURCHASE_INVOICE_PATH);
$fileName = $data['invoiceNumber'].'.pdf';
$this->html2pdf->filename($fileName);
$this->html2pdf->paper('a4', 'portrait');
$content = $this->load->view('templates/example', '', true);
//Load html view
$this->html2pdf->html($content);
$this->html2pdf->create('save');
return $fileName;
}
I tried to change path of image to full path and type of image from png to jpg. But still it not showing
This issue is happening becuase of CORS. Uncomment The following line from the dompdf_config.inc.php file located in application/libraries/dompdf folder.
define("DOMPDF_ENABLE_REMOTE", true);

Html2Pdf -Codeigniter -Image not loading

I am using HTML2PDF Library for codeigniter
See:https://github.com/aiwmedia/HTML2PDF-CI
My problem is i am unable to load images using tag. Instead of image it always shows alt attribute. Tried giving the full path and relative path.
View:
<img src="<?php echo base_url(); ?>/uploads/pdf_images/1.jpg" alt="<?php echo base_url(); ?>uploads/pdf_images/1.jpg"/>
Controller:
$this->load->library('html2pdf');
$this->html2pdf->folder(APPPATH."third_party/samples/");
$file_name=$this->random_string(7);
$this->html2pdf->filename($file_name.'.pdf');
$this->html2pdf->paper('a4', 'portrait');
//$this->html2pdf->html($this->load->view('pdfcouple',$data,true));
if($user_details_arr[0]->is_married)
{
$content=$this->load->view('pdfcouple', $data, true);
}
$this->html2pdf->html($content);
$this->html2pdf->create('save');
I solved the issue by editing dompdf_config.custom.inc.php
uncommented
define("DOMPDF_ENABLE_REMOTE", true);
Now the images are loading inside pdf. Thanks for the support.

mPDF: image in CSS

I'm using mPDF to generate PDF's on my website.
In my CSS I use to have a image as background:
.page{
height:297mm;
width:210mm;
background:url(../bookletFiles/exam_header.png) no-repeat scroll;
padding:400px 1.6cm 1.7cm 1.6cm;
}
This has been working for a long time. But lately I noticed that, when I declare $mPDF->debug to true, I receive the following error:
<B>mPDF error: </B>IMAGE Error (http://topografieindeklas.nl/wp-content/themes/topografieindeklas/bookletFiles/exam_header.png): Could not find image file
Strangley, when I remove the CSS declaration and use the exact same image in tag's, it is displayed.
I already tried to regenerate the .png file, replace it with a .jpg file but this hasn't had any result.
The only thing I can image that has changed lately, is an upgrade from PHP 5.3 to 5.4 The allow_url_fopen settings is set to true though.
Does anyone has any thoughts on why this image won't load through CSS and how that could be fixed?
Try this in the view of your pdf file.
<?php $dir= $this->webroot."img/cake.icon.png"; ?>
<?php echo $dir; ?>
<img src="<?php echo $dir; ?>">

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

Categories