TCPDF html with image not working - php

I'm working on creating pdf with TCPDF library, i'm having issue creating pdf from html with image.
Pdf creation is worked beautifully in my localhost and staging server but when i moved it to live enviornment it fails to load and throwing error.
ErrorException in tcpdf.php line 7124:Illegal string offset 'masked'
I've applied googled answers but none worked for me.
When i'm removing the image from html, pdf is generating. But when i'm adding image, its throwing the above error.
I've checked the image url and its fine. Its been one whole day with no results.
It would be very helpful if someone can lead me to right way.
CODE
$pdf = new \TCPDF();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->writeHTML(view('pdfs.order',$data)->render());
$pdf->Output($filename, 'D');
HTML
<img width="320" height="68" src="{{ url('').'/img/report/large_logo.png' }}" />

So at the end i was able to solve it and i would like to share it with you guys.
Real reason was transparent png images.
At first i tried to find a solution that handle transparent images by TCPDF library, but didn't got anything working.
At the end i've replaced the transparent png images with non transparent images and it worked.
Hope it help someone.

Related

PHP mPDF base64 image error - could not find image file ()

According documentation I would like to use mpdf with base64 image to preview img tag in document. Everything should be ok, if I show the base64 image in html page it wortks fine. But mpdf throws me an error:
Could not find image file ()
The code looks like this and as I said in html it works but not in mpdf:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG8AAA....."
Can somebody tell me please what is wrong? Documentation is simple. Why mpdf does not recognize base64 data? Thanks for any help.

How to display GIF's in laravel

I'm using Firefox Browser, tried all PNG's, JPG's and all were working, except GIF.
Here's the thing, if I test http://localhost/pepsi.gif, the GIF is actually working and displaying. But when I tried it inside HTML img tag it does not displaying.
Here's the simple code below:
<img src="http://localhost/pepsi.gif" alt="No GIF">
Somebody could help me how to do it? I'm really stucked with this one.
pepsi.gif
This is most likely to be caused by an ad-blocker or extension.
Try disabling them 1 by 1 and see if the image loads.
Or try an incognito browser.
Your gif should work in an img-tag. If you upload your code to a server you have to change the path from localhost to anything other, maybe this causes the problem.
img {
height: 200px;
width:auto;
}
<img src="https://im3.ezgif.com/tmp/ezgif-3-0a7fd40adb02.gif" alt="test">

Calling a gif with createimagefromgif in PHP

I've been trying to display a gif image using PHP and have not found a solution to my problem with the research I've done. I know I can display image in HTML/CSS, but I need to use php in this case.
<html>
<?php
$img = imagecreatefromgif("http://www.mysite.com/images/timer.gif");
?>
<img src="<?= $img ?>" alt="timer" />
</html>
That code resides in a php doc on my server. I can tell the code is working because an icon of a torn image displays on my site, and when I attempt to save the torn icon image to desktop, the automatic file name appears as "Resource id.html"?
I read somewhere that creating gifs with Photoshop CS5 (as I did) uses a different frame separator sequence, \x00\x21, instead of the official standard \x00\x2C. The guy then said he uses pattern "#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s" to bypass that issue but I have no idea how/where to implement that or if that is even my issue (tried a different gif from internet and had same display problem). Thoughts? Thank you.
Imagecreatefromgif() returns an image resource, which you can't use in this way.
Have your PHP script output the image data, with the correct headers (see example here http://php.net/manual/en/function.imagecreatefromgif.php).
Then you can call your script just like you would any other image:
<img src="http://www.mysite.com/images/generate_image.php" alt="timer" />

DomPDF - Changing the point/pixel ratio

I'm trying to use DomPDF to generate a PDF out of some HTML. I need the page to be 20mm x 20mm, so I use the following:
CPDF_Adapter::$PAPER_SIZES['my_square_page'] = array(0, 0, 566.929134, 566.929134);
$dompdf->set_paper('my_square_page','portrait');
It works properly, if I check the PDF properties the size is ok. The HTML that will appear in the PDF has a container div of 490x490px. This size cannot be changed, as the elements inside that div are absolutely positioned.
The problem, then, is that in the generated PDF the div does not cover the entire page. I've tried setting the DPI, using different values in
def("DOMPDF_DPI", 150);
But it does not seem to make any difference at all. The output I get is this (gray borders are from the PDF reader):
I've tried setting the width and height of body and html in the CSS of the content, but it does not work.
You can check the source code of my sample case here.
Ok, I figured it out. Looks like the line
def("DOMPDF_DPI", 150);
does not actually do anything. I did change the dompdf_config.custom.inc file and then it worked. Added this:
define("DOMPDF_DPI", 62.230);
But now the images look too big :S

TCPDF don't display image with writeHTML

this question seems an evergreen on TCPDF...
I have an issue that's driving me crazy.
I have an html code that I use as "template" for my PDF, here I have my company logo.
Everything works fine on localhost (Windows), but when I move online, the image is not shown.
Pay attention: I don't get any error (ie the Unable to get image error) on my PDF the image is simple blank!
Infact if I click on the PDF on the position where the images it's supposed to be, I can select it, and Adobe enables the option "Copy image".
Obviously the image exists, is here, and permission are correct.
If I try to surf there, or view the generated HTML page, everything is fine.
This is the PHP code:
$pdf->SetMargins($params->get('pdfMarginLeft', 15), $params->get('pdfMarginTop', 27), $params->get('pdfMarginRight', 15));
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 8);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
Then this is my HTML code (I just skipped everything except the image):
<img alt="logo black" src="../images/logo_black.png" height="60" width="210" />
I've tried with the url (relative and absolute) and with the path (relative and absolute), the problem still occurs.
Any ideas?
This wasn't your problem, but it's a possible solution for people with a similar issue in the future. Please make sure the HTML attributes have double quotes.
$html = "<img src='...' />"; // This will not work
$html = '<img src="..." />'; // This will work
As things are working locally so you may try changing the image type from png to jpg and check after modifying your code accordingly and uploading the jpg on the server.
You can convert image type "jpg/png" to base64.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...."/>
this may help you !
In my case, I tried every solution above to no avail. It turned out I was missing width and height attributes. Adding those in, and using a root path ended up working for me:
<img src="/images/image.png" width="50" height="50"/>
In my case IMG tag not working until I write full path to file
Not working
<img src="/pdfrender/XXX.jpg" width="50" height="50">
Working (localhost example)
<img src="http://site.local/pdfrender/XXX.jpg" width="50" height="50">
It is also posible add image data inline as base64:
<img src="#Base64encodedImageFile" />
I implemented a str_replace for the image src, and that works ok now.
$html = str_replace("../images", $_SERVER["DOCUMENT_ROOT"] . '/images', $html);
I had to 'resave' the images:
$image = 'images/logo_example.png';
imagepng(imagecreatefrompng($image),$image);
Then it worked.
Not really an answer but as everybody is trying to add a "brick" here is a very strange detail I'm facing:
I have small "strips" of HTML that I put, one after other on a page. If I put the first one, which has 2 png and a small text, on the file, the png are visibles on the PDF.
If I put the second one, which has 2 png and a small text, on the file, the png are visible on the PDF.
Now, if I put the two, ONLY the first one as the images. The second one as blank picture. If I change the picture of the second strip, images became visible.
Detail: 1st and 2nd strips are using the same images!
Notice this seems to happened ONLY with PNG image. So it seems there is a bug and you can't have two time the same PNG image on a page. Using JPG solve the problem.
I had the same problem, locally it was working and on server blank image. I found out that htaccess password was the problem. I've put .htaccess file with the row Satisfy any to the folder with pictures and now it is working.

Categories