mPDF 5.7.1 - image displays as a broken [x] - php
I have a small issue with mPDF (version 5.7.1).
This code should generate PDF with image file:
$mpdf = new mPDF();
$html = '<img src="https://www.google.pl/images/srpr/logo11w.png"/>';
$mpdf->WriteHTML($html);
$mpdf->debug = true;
$output = $mpdf->Output();
exit();
Well there is no image but an [x] instead.
I've googled enough to get to the conclusion that it has to be done this way but I also tried realpath to the file. Still nothing.
The only thing I haven't tried is <img src="logo11w.png"> and copying the image into the folder because I don't know into which folder I should copy file logo11w.png.
Any suggestions?
I had the same problem with PNG images being displayed as [X] when to generate PDFs with mPDF.
I added: $mpdf->showImageErrors = true;
After: $mpdf = new Mpdf();
and got the error message:
GD library required for PNG image (alpha channel)#
So after running apt-get install php5-gd generating a PDF with a PNG worked like a charm!
Including images is kinda tricky in mPDF. I had some issues as well. I found more kind of problems.
At first you should turn on debug variable:
$mpdf = new mPDF();
$mpdf->showImageErrors = true;
Usualy people don't have installed GD module for PHP. On linux machine, execute:
sudo apt-get install php5-gd
sudo service apache2 restart
On Windows servers, php_gd2.dll is included in a standard PHP installation, but is not enabled by default. To enable it, uncomment the extension=php_gd2.dll line in your php.ini file (remove the # from the beginning of that line) and restart the PHP extension. [1]
If you get this error you probably see [x] image:
mPDF error: IMAGE Error (http://www.domain.com/directory/image.jpg): Could
not find image file
Check the url if your image exists and if the image is accessible. If yes then you can try to change absolute URL to relative. You can try both versions:
<img src="directory/image.jpg">
<img src="./directory/image.jpg">
Actually I also had a problem with PNG formats. Converted PNG image to JPG worked fine.
Linking in mPDF templates should be same like for whole your framework/system.
I have encountered the same problem after migrating the script.
The problem was, that the tmp directory inside the mpdf folder was not writable. I hope it helps someone.
For me, it is working as of now. Hope this will help someone.
Solution : Try relative path of image instead of URL. Image must be hosted on the same server.
Ex: /var/www/mysite/image/xyz.jpg
In my project i fix problem and solution is:
Set src absolute path on the server example:
src="/var/www/myproject/images/logo.png" if image is on the same server.
If image is from external server src is absolute path example:
src="https://www.google.bg/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png".
Hope this will help someone.
Just experienced this issue today with an old website.
If you have mPDF <= 6.0 and you are now experiencing this issue when it was previously working. The reason is that there is an SSL error (expired certificate, invalid certificate, etc). The solution is the fix the SSL certificate installation; however, if you want a quick fix you can edit the mpdf.php file to ignore SSL errors during CURL requests. Simply add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Inside the functions:
file_get_contents_by_curl
and
_get_file
Ok. After the last comment on question (14:30). Here the exact same code that IS working with mPDF 5.4. And it saves it's output on scripts directory as test.pdf. Firewall issues?
Edited
I have the following directories / files:
images
wallpaper01.jpg
wallpaper02.jpg
index.php (source code)
So these image files are relatively starting from the script directory as it looks like.
require_once __DIR__ . DIRECTORY_SEPARATOR . 'MPDF/mpdf.php';
try {
$mpdf = new mPDF();
$mpdf->WriteHTML('<img src="images/wallpaper01.jpg" alt="" width="480">');
$mpdf->WriteHTML('<img src="images/wallpaper02.jpg" alt="" width="480">');
$mpdf->Output(__DIR__ . DIRECTORY_SEPARATOR . 'test.pdf', 'F');
} catch(Exception $e) {
echo $e;
}
Fully working example (download)
It's enough
The solve is :
$mpdf->curlAllowUnsafeSslRequests = true;
It's all
In my case, the problem was just the usage of transparents PNG using the 6.0 version. This bug was fixed on the mpdf 6.1 version.
I successfully solved the problem downloading the 6.1 version from here and overriding the mpdf files on my project.
You can try this:
$mpdf->imageVars['myvariable'] = file_get_contents('alpha.png');
or
$html = '<img src="var:myvariable"/>';
$mpdf->WriteHTML($html);
after there, you should do:
$mpdf->Image('var:myvariable', 0, 0);
read more about this in documentation:
mPDF Load Image
I am using Code Ignitor and mpdf. After installing php-gd5 library, I had to re-install the php with configure command --with-gd to resolve the issue
I encountered a similar problem using MPDF 8.1.x where I could not use URL paths for my images.
Eventually I determined it was caused because the "curlUserAgent" setting was using a very old user agent.
The setting was found here:
/mpdf/src/Config/ConfigVariables.php
The curlUserAgent was set to:
'curlUserAgent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1',
But I updated this to:
'curlUserAgent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:108.0) Gecko/20100101 Firefox/108.0',
and the images successfully loaded from URL source.
change to
remove the second line before initializing $html
its undefined at that time.
new code will be as
$mpdf = new mPDF();
$html= "<img src='https://www.google.pl/images/srpr/logo11w.png' alt=''>";
$mpdf->WriteHTML($html);
$mpdf->debug = true;
$output = $mpdf->Output();
exit();
Another reason might be you're running your application in Docker (or another container) and you're using a domain that can't be resolved.
Adding an alias in my docker-compose file resolved the issue for me.
networks:
app-network:
aliases:
- your.local.domain
To find out if this is the reason, log into the running container and attempt to request the file via curl/wget/etc.
.htaccess with password also can blocks access
An exception can be added:
AuthType Basic
AuthUserFile "/home/domain.pl/public_html/.htpasswd"
require valid-user
AuthName "set password"
Order allow,deny
# payu
Allow from 185.68.14.10
Allow from 185.68.14.11
Allow from 185.68.14.12
Allow from 185.68.14.26
Allow from 185.68.14.27
Allow from 185.68.14.28
# serwer localhost
Allow from 185.185.185.185
satisfy any
Had the same issue today. Problem was that the file was in another folder on the same server of the file calling the "save pdf" function.
copy("holdpath.bmp","samepath.bmp");
$writer->save("someotherpath.pdf");
unlink("samepath.bmp");
Related
why dompdf inside laravel 4.2 failes to load html string?
Inside the laravel controller, When I do this $html = '<h1><a name="top"></a>mPDF</h1> <h2>Basic HTML Example</h2> This file demonstrates most of the HTML elements. <h3>Heading 3</h3> <h4>Heading 4</h4> <h5>Heading 5</h5> <h6>Heading 6</h6>'; $pdf = App::make('dompdf'); $pdf->loadHTML($html); $pdf->save('my_stored_invoice.pdf') It works perfectly. But when i do this $pdf = App::make('dompdf'); $pdf->loadView('pdf.invoice'); $pdf->save('my_stored_invoice.pdf'); My browser crashes with the calling URL, Also apache got slowed down. I have set INI limit 2048 MB. Please suggest me some way to get out of this problem. Thanks in advance.
Isnt really an answer but too big to fit in comments... Few things to try: Check installed: PHP version 5.3.0 or higher DOM extension GD extension MBString extension php-font-lib php-svg-lib Writeable folders - check to make sure that you are able to write any temporary files. Check your logs to see what crashed? Check top / htop / activity monitor etc to see if you are using all your ram
Hello for this is fixed by changing the file permission /vendor/thujohn/pdf To allow domPDF to write temporally data
DOMPDF Installation
I'd like to use DOMPDF, so I downloaded v0.6.2 on my PHP server (it meets the requests: php version, GD, mbstring...) I'm a newbie so I didn't use git or composer, just downloaded the package and put it in a folder (named DOMPDF) of my webserver (Apache 2.4 / Windows 2008 server). I wrote this very simple example and put it directly in the DOMPDF folder: <?php // c:\apache24\htdocs\dompdf\sample.php print ("I see this in HTML output"); require_once("dompdf_config.inc.php"); print("Can't see this... and nothing that follows of course!"); $html = '<html><body><p>Hello world!</p></body></html>'; $dompdf = new DOMPDF(); $dompdf->load_html($html); $dompdf->render(); $dompdf->stream("sample.pdf"); ?> Nothing works!!! It seems I haven't the correct require directive. Is it an installation problem? What files must I include? Thanks.
I had to manually install php-font-lib... Everything OK now.
how to convert with princeXML and PHP
I followed the directions on the readme file but I didn't get any errors nor any output or anything written to the log file. so, what is the right way of converting and HTML file to PDF with prince, thank you in advance. ini_set('display_errors', '1'); error_reporting(-1); require 'prince.php'; $exepath='/Users/agk/Desktop/prince/bin/prince'; $prince= new Prince($exepath); if(!$prince) die("Prince instantiation failed"); $prince->setHTML(TRUE); $prince->setLog('prince.log'); $xmlPath='/Applications/MAMP/htdocs/test/newhtml.html'; $msgs= array(); $convert=$prince->convert_file($xmlPath, $msgs); if(!$convert) { echo 'nothing converted'; var_dump($msgs); }
I notice you are using MAMP - and this is where I hit the same issue - and here's how I fixed it. I managed to find an answer elsewhere on SO relating to running shell commands via PHP on MAMP, so will share this. The problem is that the DYLD_LIBRARY_PATH is set in MAMP, and the shell/shell_exec command in PHP is looking elsewhere. As explained in the original post, this might not be the best fix but it has me up and running for now - and the alternative suggestions there didn't work for me. So, try this: Open the /Applications/MAMP/Library/bin/envvars file and comment the following lines as below: #DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH" #export DYLD_LIBRARY_PATH then restart Apache It might mess up other MAMP settings, but I can live with that for now, as I need this to work locally on my Mac for testing before I load it all on a live Linux server. Hope this helps.
Imagick not working
I am working on creating thumbnail image from the first page of pdf file. I almost tired every solution available on the web to make ImageMagick running but I still get the following error Fatal error: Class 'Imagick' not found in C:\wamp\www\mediabox\application\controllers\cron.php on line 153 what I have done so far. Installed GPL Ghostscript Installed ImageMagick-6.7.6-Q16 Downloaded and copied the php_imagick.dll file in ext folder of my php installation. Added extension=php_imagick.dll line to my php.ini file. Restarted the apache server and windows many times :) I am using codeigniter framework. Php 5.3.10, apache 2.2.21, mysql 5.5.20, wamp 2.2 and win 7 On my localhost page in the loaded extensions category 'Imagemagick is not listed' The code I have written is as $image = explode('.', $filename_new); $image_name = 'files/import/' . $ftp_upload["path"] ."/". $image[0] .".jpg"; //exec("convert -colorspace RGB -geometry 300x400! -density 200 " .$pdf_file."[0] ". $image_name); $img = $image[0].".jpg" ; $im = new Imagick($img); $im->pingImage($img); $im->readImage($img); $im->thumbnailImage(100, null); $im->writeImage( 'files/import/' . $ftp_upload["path"] ."/".$img); $im->destroy(); The exec command create images but it is very slow I want to create thumbnail images using Imagick class and I hope it will be faster as compared to exec and convert thingy. Any ideas what should I do to make the Imagick class available for my php ? Thanks
I am not sure where you got your binaries from, but it seemed like something I experienced about a month ago. Some of the binaries I tried would show up in phpinfo(), but refused to work, whereas others wouldn't even show in phpinfo(). I then just went ahead and compiled my own binaries as per my question here. I just went and compiled those binaries for you. They are for x86, compiled with VC9 and there are threadsafe and non-threadsafe versions. The version is 3.1.0RC1 (latest). You can download them here: http://min.us/mtg0Z66BI Let me know if it works for you :)
On my server the pecl-imagick extension only works with mod_mpm_prefork - it does not work with mod_mpm_worker.so or mod_mpm_event.so That may be your issue.
If you are only creating jpg add the jpg hint to the exec( ) command which will be faster than Imagick. Use -define jpeg:size=200x200 before reading the image in - 200x200 is the output image size. I suspect Imagick is not installed correctly
TCPDF ERROR: [Image] Unable to get image
I'm using TCPDF with Drupal's print module to generate PDF of articles, & ending up with following error message when I click the link to generate PDF: TCPDF ERROR: [Image] Unable to get image: http://localhost/pathToDrupal/themes/bartik/logo.png The Image exists in the location specified. I tried applying: allow_url_fopen = On; allow_url_include = On; in php.ini but that could not resolve the problem. Please care to help :(
Apparently the "fopen"-wrappers are not used by TCPDF. If you supply the URL of an image, TCPDF tries to download it with cURL into the "cache"-directory where your TCPDF-installation is stored (you have to check what the K_PATH_CACHE-constant contains to be sure). So I guess you have to have write permissions in this directory that the magic works. Also you need cURL enabled.
We had problems with the way connections were handled in our linux "example.com" server. So this lead us to try on the server: curl -v http://www.example.com/image.jpg Whenever TCPDF tried to download an image with curl, the image was not found, nevertheless, we could view the image by just opening it directly in the browser (e.g. http://www.example.com/image.jpg). We solved the problem by setting the example.com VirtualHost to accept 127.0.0.1 connections and adding the line "127.0.0.1 example.com" to /etc/hosts.
Just use the image path as "images/your_image.png" instead of "http://yourdomain.com/images/your_image.png" in the PDF html.
I've found that TCPDF would throw the Unable to get Image: error when the src was an absolute link. Just changing to a relative link would successfully create the PDF.
I had this problem on a staging server where the docroot of the site was protected by an .htaccess file (to prevent search engine indexing and other confusions) tcpdf uses curl to fetch the image and it gives this error if it cannot access the file. To solve it, I added a rule to .htaccess to allow requests from the webserver Allow from 123.45.6.7.8
Try to add path by current working dir. $img = getcwd().'/web/bundles/_bundlename_/img/logo.png';
$pdf->Image($base_url.'/'.$node->field_loc_images[0]['filepath'] ,30, 40, 75, 113, 'JPG', '', '', true, 300, '');
In your font unicode problems you need to put this syntax code: // set font $fontname = $pdf->addTTFfont('../your path here/sampletruetype.ttf', 'TrueTypeUnicode', '', 32); $pdf->SetFont($fontname, '', <font size value here>); put it before you add the page...
In drupal be sure to include the tcpdf library in your function and not at the top of your module file or you will get this error.
try this also foreach($node->field_loc_images as $key=> $s) { $pdf->Image($base_url.'/'.$s['filepath'], $x, $y, $w, $h, 'JPG', '', '', false, 300, '', false, false, 0, $fitbox, false, false); }
To expand on the error. It also seems to fail with base64 embedded images. This is a big problem for me right now.
After I upgraded PHP 5.5 to 5.6, I lost hours because of an image error, I found the solution here from #caligari (a comment on the accepted answer) and it fixed the problem for me: install CURL like this: apt-get install php5-curl && /etc/init.d/apache2 restart.
In my case I tried to curl -v http://www.example.com/image.jpg and the "curl: (60) server certificate verification failed." was shown. So it was simply a certificate issue.