I have read all the tutorials there are to read about this topic, I simply can't make this work, neither do the example codes and online tools...
When I try to import most .ttf files into TCPDF fonts, the resulting php file goes something like this:
<?php
$type='TrueType';
$name='FontAwesome5Free-Solid';
$desc=array('Ascent'=>875,'Descent'=>-125,'CapHeight'=>875,'Flags'=>96,'FontBBox'=>'[5000 5000 -5000 -5000]','ItalicAngle'=>-50,'StemV'=>70,'MissingWidth'=>1000);
$up=-123;
$ut=49;
$dw=1000;
$cw=array(
0=>1000,1=>1000,2=>1000,3=>1000,4=>1000,5=>1000,6=>1000,7=>1000,8=>1000,9=>1000,
10=>1000,11=>1000,12=>1000,13=>1000,14=>1000,15=>1000,16=>1000,17=>1000,18=>1000,19=>1000,
20=>1000,21=>1000,22=>1000,23=>1000,24=>1000,25=>1000,26=>1000,27=>1000,28=>1000,29=>1000,
30=>1000,31=>1000,32=>1000,33=>1000,34=>1000,35=>1000,36=>1000,37=>1000,38=>1000,39=>1000,
40=>1000,41=>1000,42=>1000,43=>1000,44=>1000,45=>1000,46=>1000,47=>1000,48=>1000,49=>1000,
50=>1000,51=>1000,52=>1000,53=>1000,54=>1000,55=>1000,56=>1000,57=>1000,58=>1000,59=>1000,
60=>1000,61=>1000,62=>1000,63=>1000,64=>1000,65=>1000,66=>1000,67=>1000,68=>1000,69=>1000,
70=>1000,71=>1000,72=>1000,73=>1000,74=>1000,75=>1000,76=>1000,77=>1000,78=>1000,79=>1000,
80=>1000,81=>1000,82=>1000,83=>1000,84=>1000,85=>1000,86=>1000,87=>1000,88=>1000,89=>1000,
90=>1000,91=>1000,92=>1000,93=>1000,94=>1000,95=>1000,96=>1000,97=>1000,98=>1000,99=>1000,
100=>1000,101=>1000,102=>1000,103=>1000,104=>1000,105=>1000,106=>1000,107=>1000,108=>1000,109=>1000,
110=>1000,111=>1000,112=>1000,113=>1000,114=>1000,115=>1000,116=>1000,117=>1000,118=>1000,119=>1000,
120=>1000,121=>1000,122=>1000,123=>1000,124=>1000,125=>1000,126=>1000,127=>1000,128=>1000,129=>1000,
130=>1000,131=>1000,132=>1000,133=>1000,134=>1000,135=>1000,136=>1000,137=>1000,138=>1000,139=>1000,
140=>1000,141=>1000,142=>1000,143=>1000,144=>1000,145=>1000,146=>1000,147=>1000,148=>1000,149=>1000,
150=>1000,151=>1000,152=>1000,153=>1000,154=>1000,155=>1000,156=>1000,157=>1000,158=>1000,159=>1000,
160=>1000,161=>1000,162=>1000,163=>1000,164=>1000,165=>1000,166=>1000,167=>1000,168=>1000,169=>1000,
170=>1000,171=>1000,172=>1000,173=>1000,174=>1000,175=>1000,176=>1000,177=>1000,178=>1000,179=>1000,
180=>1000,181=>1000,182=>1000,183=>1000,184=>1000,185=>1000,186=>1000,187=>1000,188=>1000,189=>1000,
190=>1000,191=>1000,192=>1000,193=>1000,194=>1000,195=>1000,196=>1000,197=>1000,198=>1000,199=>1000,
200=>1000,201=>1000,202=>1000,203=>1000,204=>1000,205=>1000,206=>1000,207=>1000,208=>1000,209=>1000,
210=>1000,211=>1000,212=>1000,213=>1000,214=>1000,215=>1000,216=>1000,217=>1000,218=>1000,219=>1000,
220=>1000,221=>1000,222=>1000,223=>1000,224=>1000,225=>1000,226=>1000,227=>1000,228=>1000,229=>1000,
230=>1000,231=>1000,232=>1000,233=>1000,234=>1000,235=>1000,236=>1000,237=>1000,238=>1000,239=>1000,
240=>1000,241=>1000,242=>1000,243=>1000,244=>1000,245=>1000,246=>1000,247=>1000,248=>1000,249=>1000,
250=>1000,251=>1000,252=>1000,253=>1000,254=>1000,255=>1000);
$enc='cp1252';
$diff='';
$file='lol.z';
$originalsize=191836;
// --- EOF ---
Look that all characters are invalid, basically.
This is the result I get using my PHP source, or using online converters made for TCPDF (eg.: http://www.xml-convert.com/en/convert-tff-font-to-afm-pfa-fpdf-tcpdf or http://fonts.snm-portal.com/).
I am trying to add font-awesome's TTF files (eg. fa-solid-900.ttf) to my TCPDF, but I get this error.
I have no idea what to do now.
You need to import FontAwesome as a Unicode font. [Also noting: Characters 0-255 will have character widths of 1000 in your example because they don't exist in this font and 1000 is the width defined for your missing glyph width.] For your other fonts that are failing, try importing them as Unicode fonts as well or with a different encoding table.
//Ignore the warnings about undefined indexes on first import.
//There's no H or x glyphs in the font, so the warning is expected.
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4);
//Note: using empty string for $enc since this is a Unicode font
//Note: using 4 for $flags to signify a symbol font. 32 for text fonts.
$pdf->setFont($fa);
$pdf->writeHTMLCell(100,120,40,10,"");
//An example inlined with normal text:
$pdf->setFont('helvetica');
$pdf->writeHTMLCell(100,120,40,20,"Should be a gamepad: <span style=\"font-family:$fa;\"></span>");
Attached screenshot of test output results:
Test script:
<?php
//Update to your TCPDF path
include 'TCPDF/tcpdf.php';
$pdf = new TCPDF('P', 'mm','Letter', true, 'UTF-8', false, true);
$pdf->addPage();
$pdf->setFontSubsetting(true);
$fa = TCPDF_FONTS::addTTFfont('fa-solid-900.ttf', 'TrueTypeUnicode', '', 4);
$testtext = "Font name: {$fa}<br>".
"Should be a gamepad: <span style=\"font-family:$fa;\"></span>";
$pdf->writeHTMLCell(100,10,10,10,$testtext);
$pdf->Output(dirname(__FILE__).'/fasolid-test.pdf', 'F');
We have an application that takes text from a form (it's ultimately a spreadsheet) and pulls it from the database and creates a pdf document. Using fdpf and everything is working well. But there is a header portion of the doc that is currently plain text posting from a <texarea>, and we want to upgrade that portion to rich text using tinymce. I can easily use https://github.com/spipu/html2pdf to create a nice, full doc from the text, but it's only meant to be a small portion of the doc. This heading is not the "Header", which is taken from a jpg image.
I've tried several approaches including creating a separate doc with the rich text (html) portion and attempting to merge the two, which works great if the two are separate pages, but not so well when trying position one inside another when the one is of varying sizes, shapes, lines. Another thing I've tried is using imagemagick to convert the pdf taken from the rich text portion to a jpg and then inserting the image into the other pdf. This works great unless the heading contains mostly text(as it will).Images come through well but text becomes unreadable with my lack if image manipulation prowess.
using fpdf
class foo extends FPDF
{
public function fooBody ($bar) {
`````````````````````````````````
$data is obj containing db entities
current lines for the heading (having to remove html rn)
$data->heading = preg_replace("/ /",'',$data->heading);
$this->MultiCell(190, 3, trim(strip_tags($data->heading)), 0, 'L', 0);
$foo= new foo();
$foo->fooBody(bar);
add page...
output()..
etc.
wish I could just do something like below
$html2pdf->writeHTML($data34->heading);
$richTextHeading = $html2pdf->output('file.pdf', 'S');
foo->MultiCell(190, 3,$richTextHeading, 0, 'L', 0);
I want to use the same doc I have now with fpdf but somehow include some converted html for a small portion of it, but am currently only capable of having one (current doc) or the other (html doc).
If anyone comes across this thread, I ended up just using TCPDF, and re-writing my old code to work with TCPDF. Suprisingly I only need to change font names in a few places (Ariel->helvetica) and explicitly state cell height and margins. The old code worked mostly as is, and this
$this->MultiCell(190, 3, trim(strip_tags($data->heading)), 0, 'L', 0);
Is now simply this
$this->writeHTML( $data->heading);
I have a PHP project where I am opening up a premade PDF, and filling it out with data via PHP. The problem I am having is that one of the text elements isn't showing up. I am positioning it towards the bottom right of the PDF page. If I move it to the left a little, it shows up. It's as if there is some clipping or something.
I am using TCPDF, and since I am needing to modify an existing PDF, I am also having to use the FPDI class. It appears to me that FPDI normally is integrated with FPDF, so I've been using the FPDF methods to build out my PDF. OK, so here is some of my code (or the relevant parts)...
$pdf = new PDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetXY(261,200);
$pdf->Write(5, 'test');
There is at least a centimeter of whitespace to the right of the text when I position the text with a value of 260. If I move it just one more unit to 261, like in the code above, it just disappears. I'm able to position the text so far on the bottom of the page, that only the top half of the letters show, however, I can't even approach the right side of the page, or the text will completely disappear. I've set the SetAutoPageBreak to false, so new pages aren't created, and I've also flirted with zeroing out the margins.
Might be a bit late in the game...
I had a look at the fpdf.php file and it looks like it adds a margin of 1cm.
If you lower it then you can get text closer to the edge of the page. Below is the original line:
// Page margins (1 cm)
$margin = 28.35/$this->k;
But if you change it to something like
$margin = 10/$this->k;
This gets you closer to the edge of your document.
I've had the best luck using cells to position text, for some reason they have shown to be more accurate and easier to work with than simply writing text onto the document:
$pdf->SetXY(261,200);
$pdf->Cell(0,10,'My text',0,1, 'C');
Docs: http://www.fpdf.org/en/doc/cell.htm
Write() is used for flowing text (internally it uses several Cell() calls). If it reaches the right margin an automatic line break is done and the next word/character will start a new line at the left margin.
The word will not disappear but will be shown at the lower left area.
You can see the characters flowing with this simple script:
$pdf = new FPDF();
$pdf->AddPage( 'L', 'Letter' );
$pdf->SetAutoPageBreak(false);
$pdf->SetFont('Helvetica');
$pdf->SetXY(261,200);
$pdf->Write(5, 't e s t');
$pdf->Output();
Try the following:
$pdf->SetAutoPageBreak('auto',0);