binding font files with PDF document in TCPDF - php

Im scripting to generate a PDF file using TCPDF library, all I want now is to add font files into the document, I could add them using "SetFont" like
$pdf->SetFont('myfont', '', 10);
and its when I execute this script, the document in browser is rendering fonts perfectly, if I save it and open in another machine then fonts are working with, default fallback font is applying.
I did check it by going to File->Properties->Fonts in Adobe reader, font file is listed there, but text is missing the font.
Any idea will be helpful.

You will probably want to embed the font using $pdf->AddFont().
A longer description of the differences between SetFont(), AddFont() and AddTTFFont() here:
TCPDF, "Could not include font definition file" with OpenType fonts

Related

PHP ImageMagick - Draw Indic text using font file, not from system fonts

I am trying to draw Devanagari text on image using PHP ImageMagick.
ImageMagick renders text correctly if I use font available in the system, but not when loading a font from a file.
For exmaple if use
$imagick->setFont('Lohit Marathi');// installed font
works correctly but if I use
$imagick->setFont("fonts/lohit_mr.ttf");//load from file
Font does not loads and it renders using default font.
code I am trying is as follows
<?php
$imagick = new Imagick();
$imagick->setFont('ANY_SYSTEM_FONT') // works;
$imagick->setFont('somefont.ttf') // does not work;
?>
Can someone point out anything I am missing?

How can I omit Helvetica with TCPDF?

I'm making a PDF with TCPDF, and I'm trying to make the file as small as possible. The font I'm using is Open Sans. I'm not (intentionally, at least) using Helvetica anywhere in the PDF. When I view the included fonts with Adobe Reader in my outputted PDF file, both Open Sans and Helvetica are listed. I have noticed that if I AddFont() other fonts, the outputted PDF gets bigger.
To save space, how can I tell TCPDF to not include Helvetica in the file?
The Helvetica font is added by TCPDF for two reasons:
On initialization the TCPDF class sets the default font to Helvetica (in the constructor) and therefore adds this font to the fonts list of the document.
For older versions:
To prevent this, you can edit the file config/tcpdf_config.php and change the constant PDF_FONT_NAME_MAIN to your desired default font name (should be around line 155). Note that you must not use any core font because they will never be embedded.
For newer versions:
Define PDF_FONT_NAME_MAIN with your desired default font name before including the TCPDF files. Example:
define('PDF_FONT_NAME_MAIN', 'freesans');
include_once 'path/to/tcpdf.php';
TCPDF adds an invisible link "Powered by www.tcpdf.org" at the bottom of the page.
To prevent this you have to use an override class like this:
class MyPdf extends TCPDF {
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
// call parent constructor
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
// disable the tcpdf link
$this->setTcpdfLink(false);
}
/**
* Allows to disable the invisible "Powered by www.tcpdf.org" link at the bottom of the page.
* #param type $tcpdflink
*/
public function setTcpdfLink($tcpdflink = true) {
$this->tcpdflink = $tcpdflink ? true : false;
}
}
The Helvetica font is one of the standard 14 core PDF fonts, so it is not embedded in the PDF when it is used. If you look in the TCPDF fonts directory you will notice that the Helvetica file only contains a description of the font and not a copy of the font. Therefore it shouldn't be significantly increasing the file size.
Solution
The Helvetica font is set as the default font in the TCPDF config files. From my testing, it appears that this causes it to be set as a font in the generated PDF files even when it is not used. Changing the default fonts in your TCPDF configuration files should prevent this from happening.
I have to face the same issue. I have tried the JOR solution. its correct but it still shows the Helvetica font family in my pdf.
for my pdf, I am using SVG image.so it displays the Helvetica . in the tcpdf.php protected property called $svgstyles has the SVG font family as Helvetica.
Just find $tcpdflink in tcpdf.php and make that variable false. This works for me

PHP - Dynamically creating a svg with embedded fonts

I want to dynamically create an svg that I can use in an <img/> tag. This in itself is easy; create an svg, set the header and echo the generated parts in their correct place.
The problem is, I want to be able to embed fonts in the svg.
I've tried using the #font-face rule in the css of the svg, but that didn't work (MDN says that it only works on Android and Safari).
Is there any cross-browser way to do this?
Solutions I've Considered:
Possible Solution #01:
The solution:
In my main file, create an svg file which uses the #font-face css rule, and then use exec() to use inkscape to convert that svg into another svg, which converts all letters into paths. I then could use echo file_get_contents($inkscape_file) with the correct headers to output it as a svg which can be used with an <img/> tag.
The problem with this:
This creates 2 additional files, so seems very inefficient. Furthermore, since each user will end up generating several images, the space it takes up would grow phenomenally.
Possible Solution #02:
The solution:
Make a template in illustrator, then save it as svg, and tick the embed all glyphs option. Then replace the text & the styles with the options from the PHP script. Use the correct header and output this.
The problem with this:
This severely limits the amount of fonts that can be used, as it is limited to only those which I create a template for. My desired behaviour was to add the option for users to upload their own fonts and use them. This solution does not allow for that.
Additional information that may be of some relevance:
My development server runs fedora, and the production server uses redhat.
The #font-face rule I am currently using is as follows:
#font-face {
font-family: Potato;
src: url("/fonts/potato.otf");
}
You can't load any external resources declared in the svg from the <img> tag.
The only solutions would be some crappy ways to append the glyphs or the fonts into the svg file itself.
Actually there is a not so crappy way to do it as you found in this answer by lèse-majesté.
The best way is then still IMO to not use an <img> tag to display the svg documents, but rather use an <iframe> or an <object> tag, with the #font-face declared inside the svg file, or even directly include an inline version into the document. These methods do allow the loading of external resources such as fonts.
Then you just have to save the fonts on your server or just an url to the font in the #font-face declaration.

Including custom font with mpdf does not show fonts in Adobe Acrobat on Windows but does on Okular

When I include my custom fonts (as specified below) with mPDF, the custom fonts are correctly used in the PDF when viewed in Okular (a Linux PDF reader) and Chrome's built in PDF viewer, but on Windows, Adobe Acrobat falls back to Helvetica, not my custom font.
Could it be that mPDF is writing the font instructions in a non-standard manner? Or am I doing something wrong?
I'm including them according to these instructions: http://mpdf1.com/manual/index.php?tid=501
Assumes you have 2 font files "Frutiger-Normal.ttf" and "FrutigerObl-Normal.ttf" which you want to be available in mPDF, and you will refer to them in HTML/CSS as "Frutiger".
1. Upload the 2 files to the fonts directory (/ttfonts)
2. In the configuration file (config_fonts.php) add this to the array $this->fontdata:
"frutiger" => array(
'R' => "Frutiger-Normal.ttf",
'I' => "FrutigerObl-Normal.ttf",
),
3. In your HTML or CSS code use something like this:
<p style="font-family: Frutiger">....</p>

Making text bold or larger in dompdf

I just started using dompdf v0.6.0beta3 and the dompdf Codeigniter helper with Codeigniter. I have copied the Arial font family tff files from Windows 7 font folder to dompdf's /lib/fonts folder.
Problem: When I select a text using CSS and apply the font-weight: bold property, on the HTML output it is indeed bold, but after converting to PDF via dompdf, the text is no longer in bold!
font-size: 24px does not work, all text in the pdf are the same sizes. And the only font being used in the pdf appears to be Times New Roman!
How can I make my text bold and change its size and font in the pdf?
PHP (Controller)
function pdf() {
$this->load->model('resume_model');
$results = $this->resume_model->get_resume_details($user_id);
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view('resume_pdf', $results, true);
pdf_create($html, 'filename');
}
Is your stylesheet external to your HTML content? If so you may just have a path problem. The plugin appears to use $dompdf->load_html() to load the document. DOMPDF has no knowledge of your website when used in this way and will work off the local file system. What this means for you is that file paths are relative to the currently-executing file. If the path is absolute (e.g. /css/main.css) then DOMPDF will look for this file off the root of the file system. Instead of looking for the file at /wwwroot/content/css/main.css it will look for /css/main.css.
The quickest fix, if this is your problem, would be to add a full URL, including domain, to your file references (e.g. http://example.com/css/main.css).

Categories