FPDF error: Could not include font definition file in PHP - php

I have bunch of images and I want to generate PDF of of all those images. I am using FPDF library (version 1.7) for achieving this. But I am getting the following error:
FPDF error: Could not include font definition file
I found some articles on google regarding this error and tried that but still problem persist.
What should be the problem here? Where I am going wrong?

I had a similar error and i share here because there is no documentation online.
"FPDF error: Font file not found"
If you convert a ttf font file for use it in FPDF with the online utility (http://fpdf.fruit-lab.de) once you have downloaded the files you need (file.php, file.afm, file.z)
you'll have to:
1) put in font folder (or any other folder, but you should use this instruction define('FPDF_FONTPATH','yourpath/yourfolder/');)
2) If you RENAME the files, you should open the file.php and search for "$file" which contains the name of the ".z" file and rename it properly.

Maybe a bit too late, but I used to have that same problem and the solution was to simply give permissions to the /font/ folder...
Rookie mistake... 755 permissions did the trick for me.

define('FPDF_FONTPATH','font/');
check in above line for correct path from local folder to server.....
exactly it runs well
define('FPDF_FONTPATH','font/');
require('fpdf.php');
class PDF extends FPDF
{
//Constructor (mandatory with PHP3)
function PDF()
{
self::__construct();
}
public function __construct()
{
$this->FPDF();
}
//Page header
function Header()
{
//Logo
//$this->Image('logo_pb.png',10,8,33);
//Arial bold 15
$this->SetFont('Arial','B',15);
//Move to the right
$this->Cell(80);
//Title
$this->Cell(30,10,'Title',1,0,'C');
//Line break
$this->Ln(20);
}
//Page footer
function Footer()
{
//Position at 1.5 cm from bottom
$this->SetY(-15);
//Arial italic 8
$this->SetFont('Arial','I',8);
//Page number
$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
}
}
echo "<br/>";
//Instanciation of inherited class
$pdf=new PDF();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->Image('logo_pb.png',40,20,33);
$url="demo/ivv/doc.pdf";
$dir='C:/xampp/htdocs/laravel/public/pdf/';
$filename= time().'.pdf';
$content=$pdf ->Output($dir.$filename);
?>

You must create the font definition files for the fonts you are using.
See here:
http://www.fpdf.de/tutorials/7/

Check your font path in AddFont function.
For example:
$fontInformation = pathinfo(WWW_ROOT . "files/files/font/file/" . $pdffile['font_file']);
$fontFileName = $fontInformation['filename'] . '.php';
//$pdf->fontpath = WWW_ROOT . "files/font/file/";
$pdf->AddFont($fontInformation['filename'], '', $fontFileName);
//set font for the entire document
$pdf->SetFont($fontInformation['filename'], '', 20);
This may solve your problem.

I had this kind of problem and my issue was that I was using a linux web server and I made the mistake of not using the correct case in my references to my font files.
After I changed "Arial" to "arial" the problem was resolved.

Related

Laravel 9: imagettftext(): Invalid font filename

I have a Controller method like this:
public function singleChar($text)
{
$font = 'SansBold.ttf';
...
imagettftext($img, $size, $angle + 5, $textX + 5, $textY + 10, $shadowgbColor, $font, $text);
...
}
But I'm afraid that's not right because I get this error:
imagettftext(): Invalid font filename
And I think this error occurs because I have wrongly called the font SansBold.ttf. So I wonder what the correct way of calling this font inside this Controller Method is? And also, where should I place the font?
Note that I'm using PHP v8 and also enabled GD Library by uncommenting ;extension=gd inside php.ini. I'm stuck with this and would appreciate any idea or suggestion.
you need to specify the full path to the font file.
it is better to use the public folder to store the font (if you need to have public access to it)
or the storage/app folder (if you only use it inside your laravel application)
In the first case, getting the path will look like this:
$font = public_path('fonts/SansBold.ttf'); // your file here: public/fonts/SansBold.ttf
In the second case:
$font = storage_path('app/fonts/SansBold.ttf'); // your file here: storage/app/fonts/SansBold.ttf

Error embedding pdf using fpdi in CodeIgniter 3

Thanks in advance...
I am trying to generate a pdf in CodeIgniter using the fpdi and tcpdf libraries using the following code...
<?php
use setasign\Fpdi;
require_once('tcpdf/tcpdf.php');
require_once('fpdi2/autoload.php');
class Pdf extends Fpdi\Tcpdf\Fpdi
{
/**
* "Remembers" the template id of the imported page
*/
protected $logo;
/**
* Draw an imported PDF logo on every page
*/
function Header()
{
if ($this->logo === null) {
$this->setSourceFile(base_url().'assets/pdf/logo.pdf'); //Will work if these 3 lines are commented
$this->logo = $this->importPage(1); //Will work if these 3 lines are commented
}
$size = $this->useImportedPage($this->logo, 130, 5, 60); //Will work if these 3 lines are commented
$this->SetFont('freesans', 'B', 20);
$this->SetTextColor(0);
$this->SetXY(PDF_MARGIN_LEFT, 5);
$this->Cell(0, 30, 'TCPDF and FPDI');
}
}
// initiate PDF
$pdf = new Pdf();
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);
// add a page
$pdf->AddPage();
$pdf->Write(5, "hello world");
$pdf->Output('generated.pdf', 'I');
?>
It runs and generates the pdf if I comment the lines I mention in the inline comments, but generates the error:
Type: InvalidArgumentException Message: Given stream is not seekable!
when I uncomment those lines.
The code works in my local server outside codeigniter.
I am able to embed the pdf in another view, so it is not a problem with the pdf nor with a restriction of accessing pdfs from my assets folder.
The same problem happens when I try to load an external font.
The view is being called by the controller this way...
class Projects extends CI_Controller{
public function fpdi(){
$this->load->view('projects/fpdi');
}
}
Thanks a lot!
(note that this is an oversimplification of the problem, I am not loading the libraries inside the view, this is just to make the problem as concise as possible for demonstration purposes)
edit-UPDATE tried the same thing with fpdf and I still get the same error "Given stream is not seekable".
UPDATE 2! Thanks to Jan Slabon's advice I kind of solved it, a replaced base_url() helper with CodeIgniter constant FCPATH so the path was relative and not a URL. Apparently this is required for fopen to function properly which is used by the fpdi library.
I don't know more and I feel this is inelegant solution but it works for now! If anybody has more info it would be greatly appreciated.
Changed the source from base_url() to relative path FCPATH. (More info at the bottom of question.)

Include frutiger font style in TCPDF

I am generating the PDF using TCPDF. The output I get is in Helvetica font but I want it in Frutiger. I have a frutiger.otf file.
I tried the following code
$fontname = $pdf->addTTFfont('Frutiger-45Light.otf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($fontname);
I am getting an error: could not include font definition file. And I have googled many websites which didn't help.
You cannot use a URL as fontfile. It must be a path in the local filesystem.
If you need a font on a different server you first must download it yourself. Since fonts are static you should do this manually.

tcpdf custom font is not being used

I'm using the TCPDF library to generate a PDF file with PHP. I want to use a custom font so I used the addTTFfont method to add my custom TrueType font files. The font I am trying to add is "Aller" from fontsquirrel.com
$aller = $pdf->addTTFfont($_SERVER['DOCUMENT_ROOT'].'/includes/fonts/Aller/Aller_Rg.ttf', 'TrueTypeUnicode', '', 32);
This created the files aller_rg.php, aller_rg.ctg.z, and aller_rg.z in my TCPDF fonts folder. The K_PATH_FONTS constant points to this directory. The addTTFfont method returns the string name of the font. It would return false if there was an error so the problem is probably not here..
I then tried to use the font
$pdf->SetFont($aller, '', 16); // or $pdf->SetFont('aller_rg', '', 16);
$pdf->Write(0,"abcdefg",'',0,'L',true,0,false,true,0);
The pdf is generated without any errors. When viewsed in the browser preview the font is clearly not aller but just a generic sans-serif.. When I open the pdf in Mac's Preview application the sections that are using the aller font are blank (no text displays).
Anyone know what I'm doing wrong?
Got it to work by using this tool
http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf
it generates a .php, .z, and .afm file that you put in the TCPDF fonts directory. I'm still not sure what the problem was. the addTTFfont() method doesn't create the .afm file so maybe that's it?
I had the same problem:
The TCPDF converted font did not display, but the one converted with the following link did:
http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf
So I compared the two and found out that xml-convert.com was using "TrueType" instead of "TrueTypeUnicode" as font type. For me it worked to change the argument to "TrueTypeUnicode". Try:
$aller = $pdf->addTTFfont($_SERVER['DOCUMENT_ROOT'].'/includes/fonts/Aller/Aller_Rg.ttf', 'TrueType', '', 32);

php include generated png in pdf via fpdf

I am trying to generate a PDF with an image that is also generated by php.
Sounds simple enough and I am sure I'm just screwing up the header but I can't seem to find a solution here.
first I generate a PDF:
define('FPDF_FONTPATH','fonts/');
require('scpt/fpdf.php');
class PDF extends FPDF {}
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->SetAutoPageBreak(false,0);
$pdf->SetTextColor(0,0,0);
$pdf->SetDrawColor(0,0,0);
$pdf->SetFont('Helvetica','',12);
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
then I generate the PNG in label.php:
if(isset($_GET["imgid"])) {
header("Content-Type: image/png");
$im = #imagecreate(110, 20)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
}
This will output: FPDF error: Not a PNG file:....
calling label.php?imgid=17 in the browser however will show me the image just fine...
What am I missing?
EDIT
In document:
Example
// Insert a logo in the top-left corner at 300 dpi
$pdf->Image('logo.png',10,10,-300);
// Insert a dynamic image from a URL
$pdf->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World',60,30,90,0,'PNG');
so it SHOULD be possible to include a dynamically generated image without saving it first?!
EDIT #2
I made it work with the library mem_image but the problem remains that this should not throw an error IMO?! So I leave this question open to see if there is indeed something wrong with my script of this turning out to be a bug.
You almost had it, you're missing only a couple things. First you need the full server address in the URL for your Image() call, so instead of
$pdf->Image('label.php?imgid=17',0,0,0,0,'PNG');
You need:
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
That will eliminate the FPDF error you were encountering. Then to get FPDF to render your output correctly, you need to add a call to AddPage(), so your PDF generation script would become:
require('scpt/fpdf.php');
$pdf = new FPDF('P','in',array(8.5,11));
$pdf->AddPage();
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
$pdf->Output('label.pdf','D');
Of course you don't need the SetTextColor(), SetFont(), etc if you're only including the single image. You didn't need the custom class definition either (I removed the unneeded lines).
Don't forget to substitute www.yourserver.com for the appropriate domain and path for your script.
The argument to the Image function needs to be a file, not a URL or another script. Since you already have your label script, the easiest work around would be to download the image to a temp location and then call $pdf->Image() with that temporary file.
This problem occurred With your file name,Make sure your passing file name is valid
function _parsepngstream($f, $file)
{
// Check signature
if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
$this->Error('Not a PNG file: '.$file);
...
}
View allow_url_fopen directive
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
You need to do only changes that
require('fpdf.php');
and do the
$pdf->Image('http://www.yourserver.com/label.php?imgid=17',0,0,0,0,'PNG');
and run your code...
First, try to check your url with this code
$handle = fopen("http://www.yourserver.com/label.php?imgid=17", "rb");
print_r(fread($handle, 8192));
you with see what the contain in your url
**) make sure your url is not redirect to the login page
much more easy, only put .png in the final of dinamic chain and its all
example:
$pdf->Image('http://www.yourserver.com/label.php? imgid=17.png',0,0,0,0,'PNG');
or
$pdf->Image('../label.php?imgid=17.png',0,0,0,0,'PNG');
the mime type is not recognized in the url why does not appear png, if it's a stupid mistake but it is proven, google does the same by adding the extension with the url & PNG

Categories