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.
Related
Hi I am trying to proxy a woff font file using PHP
this is the code I am using $path is the path to file on the harddisk. But I get an error in the console
Failed to decode downloaded font: http://localhost/font/fontawesome-webfont.woff?v=3.2.1
fakemboard.com/:1 OTS parsing error: invalid version tag
If I use PHPStorm default http server it works fine.
I have attached two images:
1) The first is the problematic response with my PHP proxy
2) the second one is the OK one using PHPStorm default server
Can you help me find out what is missing with my proxy? I believe it may be headers buy I am weak at that. So it will be really helpful if you provide the missing code. Thanks
header('content-type: application/font-woff');
$file = fopen($path, 'rb');
if ($file) {
fpassthru($file);
exit;
}
I flowed Mike 'Pomax' Kamermans advice and compered the good file font file with the bad font file. Indeed they were different. The bad file has two extra blank lines in the beginning.
It happened because that in one of the PHP files that I have included I accidentally added blank lines before the <?php tag and php rendered them. Hope this helps someone else
This is driving me crazy. I can download a file from internet through browser and its working fine. However if I write a script that does this for me using PHP, the file ends up being completely different (as per online diff tools).
$link = "https://torcache.net/torrent/9AE1726935FF9C08DF422CCE3C4445FC9484478B.torrent?title=[kat.cr]the.big.bang.theory.s08e24.720p.hdtv.x264.dimension.rartv";
file_put_contents($file_name, fopen( $link, 'r'));
First I tried to play around with encoding, but that should not matter as the file is binary, right? Also tried file_get_contents instead of fopen first, the same problem.
My PHP app is running on UTF-8 w/o BOM files. Can someone help? What am I doing wrong?
I think I got what you mean. The responses from torcache.net are all compressed with gzip. If you download the file from your browser, the file is automatically decoded, but if you do the same with php you get the same file but still encoded. You can use gzdecode to decodes it.
file_put_contents($file_name, gzdecode(file_get_contents($link)));
You have some errors in your code.
First you try to add a string to a variable without parentheses.
$link = "https://torcache.net/torrent/9AE1726935FF9C08DF422CCE3C4445FC9484478B.torrent?title=[kat.cr]the.big.bang.theory.s08e24.720p.hdtv.x264.dimension.rartv"
The next one you try to download that file over https. Check if your openssl is enabled and if allow_url_fopen is set correctly in your php.ini
https://secure.php.net/manual/en/features.remote-files.php
There are 3 ways to solve your problem:
enable openssl for https calls
Use curl and set the ssl verifier to 0
Replace the https with http to make a normal call.
Option 3 is the easiest one.
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 trying to connect to my printer which is on another machine through this code
$handle = printer_open("\\\\xxx.xxx.xxx.xxx\\Canon MF4320-4350");
if($handle)
echo "connected";
else
echo "not connected";
and getting the error
Fatal error: Call to undefined function printer_open() in C:\wamp\www\print\index.php on line 3
I have installed the php_printer.dll but it is not showing it under phpinfo(). Using PHP version 5.3.0
how can i connect to the printer and make my printer_open() method work??
If you are using PHP 5.3/ 5.4/ 5.5 in WAMP, here is how you setup printer.
Go to this link:http://windows.php.net/downloads/pecl/snaps/printer/0.1.0-dev/
Download the zip for your version. First try with ts. If it does not work, try nts.
Unzip and copy the php_printer.dll file. Paste here: C:\wamp\bin\php(phpyourversion)\ext . Note: (phpyourversion) will be like php5.5.12 depending on your version.
Find the loaded php.ini file. Use phpinfo or Wamp menu to find it. Look online for more help.
Paste this at the end of the file: extension=php_printer.dll
Restart server.
Create a file with the following code and test it:
<?php
$printer_name = "Your Printer Name exactly as it is";
$handle = printer_open($printer_name);
printer_start_doc($handle, "My Document");
printer_start_page($handle);
$font = printer_create_font("Arial", 100, 100, 400, false, false, false, 0);
printer_select_font($handle, $font);
printer_draw_text($handle, 'This sentence should be printed.', 100, 400);
printer_delete_font($font);
printer_end_page($handle);
printer_end_doc($handle);
printer_close($handle);
If the above does not work, try with nts version of the php_printer.dll file as mentioned in step two. remember to restart server after deleting the previous file, pasting the new file.
I have a similar problem. This is what I have figured out so far.
Make sure that you have a php_printer.dll matching your php compile version in your extension folder ( php/ext/ ). Use this link for downloading the file.
add the following in your php.ini file:
printer.default_printer=PHP_INI_ALL
extension=php_printer.dll
The errors have disappeared but the file is still not printing.
some of the fonts like ArialMT, Arial-boldMT, PTSans-bold..
when i am using this font like
$pdf->SetFont("ArialMT", "", 22);
then i got error like..
TCPDF ERROR: Could not include font definition file
when i saw the font folder of TCPDF library then i could not see this type of font..
how to install this font in TCPDF to solve the error and get effect of this font??
please help it is urgent..
thank you in advance..
i found one solution that in "fonts/utils/ttf2ufm.exe" then run following to command 1)$ ttf2ufm -a -F myfont.ttf and 2)$ php -q makefont.php myfont.ttf myfont.ufm first command is successfully executed but 2nd is not executing in windows command promt so tell me what i have to do for php file as you specify in "comici.php"..
Try this
$pdf->AddFont('Comic','I');
// is equivalent to:
$pdf->AddFont('Comic','I','comici.php');
You will find tutorial here.
http://api.joomla.org/com-tecnick-tcpdf/TCPDF.html#AddFont
Have you tried adding the font first?
$pdf->AddFont("ArialMT", "", 22);
If that does not work you can include the font in the library by using the command
$fontname = TCPDF_FONTS::addTTFfont(FCPATH.'/assets/css/fonts/ArialMT.otf');
For that you need to have the "otf" or "ttf" version of the font and set the correct directory.
For example I am using:
$fontname = TCPDF_FONTS::addTTFfont(FCPATH.'/assets/css/fonts/arialunicode050418/ArialUnicodeMS.otf');
// This should be ran only once to include the font. You can comment it afterwards.
$pdf->AddFont('ariaunicodems', '', 10, '',false);
$pdf->SetFont('ariaunicodems', '', 10, '',false);
You can print the "fontname" variable to see the correct name of the font to be added or set. It does not respond to the name of the font file.
TCPDF have a tool(tcpdf_addfont.php) to install any new font which is located in 'your_path_to_tcpdf/tools/ '
You can use the following command to install a new font to TCPDF.
path_to_tcpdf/tcpdf/tools/tcpdf_addfont.php -i path_to_ttf/ArialMT.ttf
The code above is pretty much self explanatory.
You can download the True Type (ttf) for any font easily using Google Search.
After running the above command you will get a output similar to:
>>> Converting fonts for TCPDF:
*** Output dir set to /path_to_tcpdf/tecnickcom/tcpdf/fonts/
+++ OK : path_to_ttf/ArialMT.ttf added as arial
>>> Process successfully completed!
Now use this font in TCPDF like any other font:
$pdf->SetFont("arial", "", 22);
You only have to create the font file once with TCPDF. This can be done, for example, with a separate PDF script.
It is important to know the exact path of the original ttf and, if necessary, to place it in the appropriate folder on the server.
TCPDF_FONTS::addTTFfont('File path to the ttf file.', 'TrueTypeUnicode', '', 32);
For all commands and details see:
https://stackoverflow.com/a/70337995/2320007