FPDF error: Could not include font metric file - php

I have a app which was done by someone else and now i am asked to look into one issue.
When a pdf report is generated it throws an error. This app uses FPDF to generate the PDF
FPDF error: Could not include font metric file
Earlier it was throwing the following error
Warning: FPDF::include(helveticab.php) [function.FPDF-include]: failed to open stream: No such file or directory
Warning: FPDF::include() [function.include]: Failed opening 'helveticab.php' for inclusion
FPDF error: Could not include font metric file
This was resolved by including a font folder with helveticab.php and other php files related to other fonts
But the Error FPDF error: Could not include font metric file is still there.
On searching the net the possible reasons were
font directory missing
Doesnt have access permissions for the font files.
I am not sure what permission need to given to the font folder or files in the folder.
Any help in this regard would be of great help.

I had the same issue. The issue was the path was incorrect to the folder with all of the fonts. So, I added updated the following line in the PHP file to reflect the correct path to the folder with all of the fonts.
define('FPDF_FONTPATH','class/fpdf_font/');
So, double check the path that this line defines, and it should work fine.

I belive you have already extracted fpdf zip file onto your localhost or system
Once, zip file is extracted you see directory structure as in below image
and insert the below code in test.php
<?php
define('FPDF_FONTPATH','font/');
//above line is import to define, otherwise it gives an error : Could not include font metric file
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Now enjoy

My problem was, because of downloading the fpdf library from the page, some of the scripts they have there uses Arial font but, that font specially, was not included in the fonts directory. I just added define('FPDF_FONTPATH','fpdf/font/'); with relative path to the fpdf dir and changed font to Courier and ready!

In my case I use Linux (Debian), I had the same issue and the directories was right. I solved adding 777 permissions to the /font directory. And now it works like charm =)))

if you are using a external class that extends FPDF
like file name Custom_pdf.php
in that file you wrote your cutom codes
require_once('fpdf.php')
class Custom_PDF extends FPDF{
...
}
then you included in to your coding like
require_once('custom_pdf.php');
$pdf = new Custom_pdf();
$pdf->Write();
....
$pdf->output();
this is problem...
So you directly place the code in the file you need
require_once('fpdf.php')
class Custom_PDF extends FPDF{
....
}
$pdf = new Custom_pdf();
$pdf->Write();
...
$pdf->output();
it will works fine...
Thanks
sorry for the english mistakes...

Related

How to set font path in FPDF

I'm getting the following error:
include(/gravity/school/pdf/font/helveticab.php): failed to open stream: No such file or directory in C:\xamp\htdocs\gravity\school\pdf\fpdf.php on line 1144
Can anyone tell me how to set the fontpath?
You can place font file anywhere in your project folder, the best way to place font file is fpdf/fonts, Where fpdf is your library folder which you are using to generate PDFs. Then after set the fonts as below:
define('FPDF_FONTPATH','/Path/to/font');
require('fpdf.php');
// Arial bold 14
$pdf->SetFont('Arial','B',14);
You can refer official doc for more information: SetFont FPDF

tPDF error after reuploading ttf "Undefined font: dejavu"

I am using the tFPDF package
$pdf=new tFPDF();
$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('DejaVu', '', 12);
and it used to work. I had to reupload the font folder and since then I get the error message
FPDF error: Undefined font: dejavu
but the file DejaVuSansCondensed.ttf is definitely in the fonts folder. I even changed the mod to 777 because I thought maybe access rights are the problem, but it is still not working. What else could cause this error?
okay I just found the solution sorry for the post.
https://stackoverflow.com/a/38520012/2311074
Delete all *.php and *.dat files in font/unifont folder except
ttfonts.php. All this files will be re-created (with right path).

How to add fonts using tFPDF

I try to create pdf file using tFPDF with custom font.
I put arial font inside font/unifont folder.
I use this code to create pdf file:
$pdf->AddFont('arialbd','','arialbd.ttf',true);
$pdf->SetFont('arialbd','',16);
$pdf->Cell(40,6, 'OÜ Kersti Võlu Koolituskeskus', 0, 1);
$pdf->SetFont('arialbd','',10);
$pdf->Cell(40,12,'REGISTREERIMISLEHT', 0, 1);
$pdf->Output('test.pdf', 'I');
It works well on localhost, but when I uploaded this file on server, it shows me an error:
Warning: fopen(C:\xampp\htdocs\koolitus\wp-content\plugins\koolitus\fpdf/font/unifont/arialbd.ttf)
I see that library tries to load a font from my localhost, but I don't even set a path, just use font name
How to add fonts properly?
Delete all *.php and *.dat files in font/unifont folder except ttfonts.php.
All this files will be re-created (with right path).
Another option: open every mtx.php file.
Replace the line (absolute path):
$ttffile='D:\xampp8\htdocs\test\include\tfpdf/font/unifont/DejaVuSansCondensed.ttf';
With (relative path):
$ttffile='include/tfpdf/font/unifont/DejaVuSansCondensed.ttf';

FPDF error "Unable to create output file"

I'm working with fpdf libray for providing pdf files. A part of my project consists of using this library to generate pdf files for consumers.
We are working with a server test under "ovh". The arborescence of my space in "ovh" is : /www/betatest.
A folder named upload which contains factures's folder where all the facture's pdf files will be there.
So, when i try to generate a pdf file inside the factures folder, in a web browser it displays me :
Warning: fopen(upload/factures/facture_98.pdf) [function.fopen]: failed to open stream: Success in /homez.742/coplayer/www/betatest/library/fpdf/fpdf.php on line 1025
FPDF error: Unable to create output file: upload/factures/facture_98.pdf.
I tried a lot of things that i found in this web site but does not work.
Please help me. Thank's a lot! :)
Make sure the directory have at least a 755. Also, use $_SERVER['DOCUMENT_ROOT'] with your path to target the good directory :
$nomFacture = $_SERVER['DOCUMENT_ROOT']."upload/factures/facture_".$idFacture.".pdf";
That will produce something like
/homez.742/coplayer/www/betatest/upload/factures/facture_12.pdf
Make sure you have not sending to output something else (like echo, var_dump, etc.) before generating PDF with Output("path_file", "F"). It is mandatory.
make sure that file upload/factures/facture_98.pdf is not open anywhere when you run script. if it's open somewhere else then TCPDF can't open it.

how can get the FPDF library

i have found some pdf file geneartion program in php. When i run it i get the following error Warning:
Warning: include(helveticab.php) [function.include]: failed to open stream: No such file or directory in E:\wamp\www\raj19\fpdf.php on line 789
Warning: include() [function.include]: Failed opening 'helveticab.php' for inclusion (include_path='.;C:\php5\pear') in E:\wamp\www\raj19\fpdf.php on line 789
FPDF error : Could not include font metric file
I think the fpdf.php is missing .I have searched on their site but i could not find the english version. can anyone provide me full link of english version?
helveticab.php is a font definition file, which is shipped with FPDF. Get it here:
http://www.fpdf.org/
Since your code is not able to locate font library, add this line to your code.
define('FPDF_FONTPATH','fpdf/font/');
All you have to do is to copy the entire folder, and check for "fonts" one. Go inside and search for the file you're looking for.
If nothing works. Try with $font = 'fonts/arial.ttf'; and in the "fonts" folder put the TTF file. If you don't know where to get it. Search in your explorer "fonts" and get the TTF file by dragging it to the fonts folder or copying it.
If doens't works, copy the code line where you're trying to call the font file.
Depends on where the library is in the root directory. if Is in the same place just use
$str = 'fpdf.php';
require($str);
if your output is in a directory called fpdf and your php output is in another directory i.e. mydoc, you need to go up one step
$str = '../fpdf.php';
if your php output is down 2 steps i.e. root/myfolder/mydoc/output.php then you need to up two steps $str = '../../fpdf/fpdf.php'; i'm assuming that you have your fpdf library in the folder root/fpdf/fpdf.php

Categories