I'm playing with Mpdf for generating some html as a PDF but HTML tags don't seem to be working
I'm using Mpdf 7.1.6 and PHP 7.2
I can include stylesheets or not with no difference
$pdf = new \Mpdf\Mpdf(["format" => "A4"]);
$pdf->WriteHTML('<h1>Heading 1</h1>');
$pdf->WriteHTML('<h2>Heading 2</h2>');
$pdf->WriteHTML('<h3>Heading 3</h3>');
$pdf->WriteHTML('<h4>Heading 4</h4>');
$pdf->WriteHTML('<b>Bold</b>');
$pdf->WriteHTML('<center>Center</center>');
$pdf->WriteHTML('<table><tr><td>Table</td><td>Table</td></tr><tr><td>Table</td><td>Table</td></tr></table>');
$pdf->Output();
and I get the following
Not sure what the issue is.
Had to install Mpdf manually as I'm on a shared server and don't have command access nor access to composer so it could be a missing library of config file.
Any suggestions would be greatly appreciated as it's doing my head in.
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
I was running wkhtmltopdf on my Windows local machine which was working fine. But now I hosted my website on aws(CentOS server) and tried to run, here it's not creating the .pdf file using wkhtmltppdf.
So Does it make a difference in installing wkhtmltopdf on windows and aws (linux I suppose) with same windows flavored version.
It is not able to pick up the link for the .exe file in cpanel as far as I understand, and shows http error 500 .
My code is
require DIR . '/vendor/autoload.php';
$html1 = $doc1->saveHTML();
//echo $html1;
use mikehaertl\wkhtmlto\Pdf;
$pdf = new Pdf;
$pdf->binary = '/wkhtmltopdf/bin/wkhtmltopdf.exe';
$pdf->addPage($html1);
if (!$pdf->send()) {
throw new Exception('Could not create PDF: '.$pdf->getError());
}
mysqli_free_result($result);
exit();
I am trying to convert $html1 to pdf. The code is working fine when I uncomment "echo $html1", it shows editable html.So, I guess autoload. php is loading correctly, but when I comment "echo $html1" and want to convert this to pdf, it shows http 500 error.strong text
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");
I am in windows 7. I installed WAMPServer. Now, I can not load spatialite libraries. It is showing warning..
Warning: SQLite3::loadExtension() [sqlite3.loadextension]: Not
supported in multithreaded Web servers
Here is my total configuration procedure, what I have done...
From this link I got spatialite lib. I copied "libspatialite-1.dll" and paste it to "D:\wamp\bin\php\php5.3.8\ext" which contains php extention dlls.
http://www.gaia-gis.it/spatialite-2.3.1/libspatialite-win-x86-2.3.1.zip
Then I edited php.ini file. I changed the following configuration.
.
sqlite3.extension_dir = C:\libspatialite-win-x86-2.3.1\bin
.
extension=libspatialite-1.dll
.
enable_dl = On
And lastly, I copy and paste all the libraries from my downloaded libspatialite-win-x86-2.3.1 to my project folder(libspatialite.a, libspatialite.dll.a, libspatialite.la) in my php code i write the script as follows...
<?php
$db = new SQLite3('sixcommunes.sqlite');
$db->loadExtension('libspatialite.a');
$rs = $db->query('SELECT spatialite_version()');
while($row = $rs->fetchArray()){
print "<h3>SQLite version: $row[0]</h3>";
}
?>
I do not know what I have done wrong or how to solve this problem?
Haven't seen any answer, so giving my own. I have used spatialite in C#, I think for using Spatialite Extensions you don't have to load libspatialite like a usual PHP extension, instead something similar to this should be done. (this is c# code, just trying to give you an idea)
SQLiteCommand sqliteCommand = new SQLiteCommand(String.Format("SELECT
load_extension('{0}');", "libspatialite-2.dll"), connection);
sqliteCommand.ExecuteNonQuery();
You have to execute Select load_extension("libspatialite-2.dll") on sqlite to use Spatialite.
Hope this helps