How to insert Image in pdf using PHP (Generating a PDF)? - php

I want to generate a PDF that will contain an image.
so i already tried this line of codes:
$pdf->Image('b.png',10,8,33);
the other is:
$pdf->Image('Project_ITCPH\images.jpg', 1, 10, 5.8, 1.5);
the problem here is this Error:
"Deprecated: Function set_magic_quotes_runtime() is deprecated in C:\xampp\htdocs\Project_ITCPH\reports\fpdf.php on line 931
FPDF error: Alpha channel not supported: b.png"
is there a problem with the fpdf.php? i only use it as include.
You have my regards.

This error simply means that FPDF is using a function call that is deprecated and outdated. This is shown because your PHP displays errors and warnings and deprecation notes. You should try turning off errors before generating the PDF or writing # before function calls (like here) when using FPDF.
By the way, I highly recommend you use mPDF for PDF generation with PDF, it's better supported in my opinion.

Upgrade your FPDF to 1.7. It now supports alpha channel in PNGs.

Check out this class it lets you add images to PDF's with PHP it has a lot of demo's
https://github.com/dompdf/dompdf

Try
$pdf->Image('..\Project_ITCPH\images.jpg', 1, 10, 5.8, 1.5);
this is some time happen cause server could not find the folder path hope this work for u
also try this
require('fpdf.php');

Related

Get PDF-page-size in points with php

I'd like to add an item on the last page of an existing PDF. The Item is added by an commercial non modifiable API.
The positon and size is definied relative to the Page. To support diffrent page formats, I have to get the page size of the existing PDF.
I've tried it with FPDI/FPDF but there are warnings. I'm not allowed to update the version.
Next try with the TCPDF doesn't throw an exception but after the import the function getNumPages returns 0;
Is there a possibility to get the page size of a PDF without using a library or a command line tool?
I have solved the problem by fixing the existing fpdi library. There were only small problems with the compatibility with newer PHP Versions. I've solved them. So I could use the following snippet:
$oPDF = new fpdi();
$iPageCount = $oPDF->setSourceFile($sFileLocation);
$mTemplateId = $oPDF->importPage($iPageCount);
$aTemplateSize = $oPDF->getTemplateSize($mTemplateId);

Laravel 5: Output tFPDF

I want to use tFPDF in Laravel 5.5. I added the package with composer and then I tried to output it the according to the docs but nothing worked.
I tried to output it directly to the browser:
Route::get('/test',function(){
$pdfLibrary = new tFPDF\PDF();
$pdfLibrary->AddPage();
$pdfLibrary->AddFont('DejaVuSansCondensed', '', 'DejaVuSansCondensed.ttf', true);
$pdfLibrary->SetFont('DejaVuSansCondensed', '', 14);
$pdfLibrary->Write(8, 'Hallo!');
$pdfLibrary->output();
});
but the sceen was just blank. When I tried return $pdfLibrary->output();
then it just returned
%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�u�� �#S�)>+�p�.......
I also tried to simply save the pdf to a file, but even
$pdfLibrary->output('F', storage_path() . '/test.pdf');
did not work.
How can I save & output the pdfs created by tFPDF ?
I found another package from SetASign which is just want I wanted. Simple tFPDF packge with namespace support. This works out of the box and is amazing.
I would strongly recommend to use the SetASing package over the DocNetUk package, because the DocNetUK package has just to many quirks. Here are some of the issues that I had using the DocNetUK package:
Output returns raw binarys, see below how to work with it
Renaming font names does not work.
method tFPDF was changed to __construct, so you have to call parent::construct in your constructor each time, which was necessary because of the namespacing(see What's difference between __construct and function with same name as class has?)
The variable names have been changed randomly and there are no docs for it:
$this->w =>$this->flt_current_width
$this->h =>$this->flt_current_height
These changes make the version from DocNetUK incompatible with basically all add-ons.
*Outdated answer
I finally realized that the unofficial composer version of tFPDF changed slightly the behavior of the output function. The output functions just returns the pdf content, but cannot save it to a file nor render it directly in the browser.
This is how to display raw binarys as a pdf in Laravel
\Response::make($this->output(), 200, ['content-type'=>'application/pdf', 'Content-Disposition' => 'inline; temp.pdf']);
Saving raw binaries goes like this:
Storage::put('file.pdf', $pdfLibrary->output());
or file_put_contents if one is not using Laravel.

TCPDF, "Could not include font definition file" with OpenType fonts

I am a web programmer with no in-depth knowledge of fonts and am struggling to get TCPDF to include our custom OpenType font. We have bought OpenType font files (.oft), which are not protected by any kind of DRM.
A lot of questions regarding this error message end up getting the same advice. I've set the correct file permissions for the folders used by TCPDF (755) and I have no trouble using the addTTFfont() to including .ttf TrueType fonts like so:
$pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);
So I've made up the following code to include our OpenFont type. The addTTFfont() documentation seems to indicate support of OpenTypeUnicode and OpenType parameters.
$pdf->addTTFfont('/path-to-font/customfont.otf', 'OpenTypeUnicode', '', 96);
Which results in:
TCPDF ERROR: Could not include font definition file:
We're using TCPDF v6.0.020 and I've been reading the TCPDF Fonts information page with no luck so far. I've noticed TCPDF also has the addFont() function (documentation here) which seems more obvious to use as it does not include any reference to any font type. However, I was unable to get this function to work with the little documentation it has.
Any help would be greatly appreciated.
Are you sure that you are getting that error when calling addTTFfont()? The reason I ask is because I checked the TCPDF code (just did a global search on "Could not include font definition file") and that message only appears in the addFont() method.
These two methods can be a bit confusing, but I wrote myself some notes a few months ago, which I will partially reproduce below in the hope that it helps you somehow:
addTTFfont() - The primary function of this method is to convert a font from TTF (or OTF) to the "raw" version that TCPDF needs. The way this function is implemented you could, in theory, use it as your primary method of adding fonts to a document. It will check the tcpdf font folder first and if the converted files aren't there it will go ahead and do the conversion. It is only a little bit more overhead, but still not my preferred method of adding fonts to files, as you need to know what style of font you are converting for the process to even work successfully. IMO, it is better to use this method to pre-convert any fonts that you plan on using and simply use addFont() to add the "raw" versions to the document.
AddFont() - This adds a "raw" (ie. already converted) font to the document, which means it is then available for writing text.
SetFont() - This sets the font for the next chunk of text that you write.
So I would use addTTFfont() to pre-convert the font to the "raw" version, then use addFont() and setFont() in the code that actually creates the PDF.
If addFont() is failing with the error message above, it means it cannot find the font definition file. Keep in mind that if you call addFont() with style set ('i', 'b', 'bi', etc), all it does is append this to the file name (before the extension).
Most importantly, you need to make sure that your call to addTTFFont() is producing the "raw" font files and saving them into your fonts folder. There should be three files per style, with extensions of .php, .z and .ctg.z. So if you converted a font called blah.ttf you will end up with blah.php, blah.z and blah.ctg.z. If you convert blah bold.ttf, TCPDF will figure out that it is a bold font and append the 'b' to the end of the file names: blahb.php, blahb.z and blahb.ctg.z.
Hopefully there will be some nugget in here that will help! Good luck!
TCPDF New Font add and turkish char problem solved method.
Use Converter Link:
http://fonts.snm-portal.com
Download 3 files,
Copy TCPDF-master/fonts
Useage: $pdf->SetFont('roboto', '', 14);
To solve this problem with generating font.
Make sure all path are correct.
Use this link
Remember to put folder make_font inside folder of html2pdf
Remember about correct path to: html2pdf.class.php
inside folder make_font in index.php
or
If you have other problem - font: Could not include font definition file:
It's a problem of correct name of your new font.
For example using when generating pdf:
Correct: $html2pdf->addFont('lato i', '', 'latoi.php');
Incorrect: $html2pdf->addFont('lato', '', 'latoi.php');
Remember there are only 2 files generated:
latoi.php
latoi.z
You don't need any ctg file.
I hope it will help
In my case the problem was, that the fonts path wasn't writable. I fixed it by changing permissions of the folder: var_dump(K_PATH_FONTS);
In my case, it was simply a wrong path to the (no more found) .ttf font file.
For a test I used the drive D:/ successfully, but I forgot to change it back to the prod drive C:/ in my php file in the code line where I use the addTTFfont('C:/...') command.
Unfortunately, the TCPDF error message doesn't output anything about the source file and the code line producing the problem.
add the dejavusans.php file into the font folder into the tcpdf library into the codeingiter library folder.
"cid0jp" this font supports Japanese and Chinese.

FPDF Image from Dynamic Source. php/png/pdf. Can't get image to work

http://babymoments.co/preview/highres%20preview/5_357/
According to the FPDF documentation here: http://www.fpdf.org/en/doc/image.htm
You are supposed to be able to use an Image from a dynamic source.. however, as per the first link, i'm getting an fopen error.
Any suggestions?
Code Snippet:
// Overlay Text & Images
$pdf->Image($conf['rbase'].'page_maker/image_hr.php?id=5&side=1&bg=cover_pink&lo=0_1&imgtxt=0|0|u5_1310329746.jpg##1|1|Elina\'s Puppies 9/2/2010|15|arial_bi.ttf|db0ddb|fedfe4&applet_type=cover',$sx,$sh,(0-$dpi), 0, 'png');
You are trying to open a local php file with get parameters - try instead to open the image file as a url. For example :
http://domain.com/image.php?id=5
Or in your case...
http://babymoments.co/preview/page_maker/image_hr.php?id=5&side=1&bg=cover_pink&lo=0_1&imgtxt=0|0|u5_1310329746.jpg##1|1|Elina's%20Puppies%209/2/2010|15|arial_bi.ttf|db0ddb|fedfe4&applet_type=cover
I wouldn't fetch the preview over HTTP but would include the required code and generate the image in the same script that generates the PDF. That way there is no problems with setups that have disabled fopen()-usage with URLs.
I was implementing a barcode for a label printer. So I was using the 'barcodegen' library and 'fpdf' library for this project, but I was having problems including dynamically the image generated with barcodegen, giving me the following error:
FPDF error: Not a PNG file: ./misc/barcodegen/mostrar-codigo-bcgcode39.php
Afeter that, I use one of the answers described here, and I solved the problem using the full URL of the image, like this:
$pdf->Image("http://localhost/caaf/misc/barcodegen/mostrar-codigo-bcgcode39.php",0,0,20,0,'PNG');
And it worked for me.

eps image (from inkscape) not showing up in tcpdf

Using php and TCPDF to generate a pdf file. Everything works great except when I try to write an EPS image to the pdf using ImageEPS(). Nothing shows up. No errors (it can definitely find the file). It just shows up as white space.
Raster images (like PNG/JPG) work just fine.
I'm using Inkscape to save the .eps file. When I open the file up in any other program, it opens just fine. Its only TCPDF that its not showing up with.
I had open my *.ai file in Adobe Illustrator and save the file as "Illustrator 3" version to overcome that issue. Any more current version produced the results you describe (except "Illustrator 8," which gave me the B&W version of my *.ai file).
A bit late, but I had the same problem.
For me, the workaround was to export as PDF and reuse this PDF in TCPDF/FPDI with:
$num_pages = $pdf->setSourceFile(path_to_file);
$template_id = $pdf->importPage(1); //if the grafic is on page 1
$pdf->useTemplate($template_id,$x,$y,$width,$height);
The ImageEPS function in TCPDF (6.0.004) is not fully implemented and the documentation states the following:
/**
* Embed vector-based Adobe Illustrator (AI) or AI-compatible EPS files.
* NOTE: EPS is not yet fully implemented, use the
* setRasterizeVectorImages() method to enable/disable rasterization of
* vector images using ImageMagick library.
* ...
*/
public function ImageEps(...){/*...*/}
TCPDF (6.0.004) checks an eps meta-data for its creator. If the creator is Adobe Illustrator, a version check is made and if the version is above 8 an error is generated.
Creators other than Adobe Illustrator are not checked and the function is allowed to continue. It does not seems like TCPDF parses the PS prolog and this is probably one reason why not all AI versions are supported. Here is what PostScript Language Reference says about the prolog section:
The prolog is a set of application-specific procedure definitions that an applica-
tion may use in the execution of its script. It is included as the first part of every
PostScript file generated by the application. It contains definitions that match
the output functions of the application with the capabilities supported by the
PostScript language.
Since the prolog is not parsed, it is troublesome to interpret the file correctly.
Inkscape (0.48.3.1 r9886) creates epses with cairo and no error will occur and the function will continue. TCPDF will partly interpret the eps, but since it does not output anything, the output is probably removed by some error handling. But that is just a guess.
I had more success with exporting my eps to a svg with
inkscape -D --file=filename.eps --export-plain-svg=filename.svg
and using ImageSVG instead. Note: this function is not fully implemented either, so I can't guarantee that it will work. I have only tested a pretty basic eps.

Categories