I'm trying to use HTML2PDF 4.03 with this code:
<?php
$content = "..."; # my HTML code
require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en', true, 'utf-8', array(15,20,15,20) );
# here I'm trying to add my arial.ttf
$html2pdf->pdf->AddTTFFont('arial.ttf');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>
Now the program die with this:
PHP Fatal error: Call to undefined method HTML2PDF_myPdf::AddTTFFont()
How can I add TTF font to my PDF file?
I have managed to add 1 custom font to my setup using the following method.
First convert the .ttf file to 3 separate files (.php .z and .ufm) using the following
font converter
Place the 3 files that are generated by this system into the fonts folder in TCPDF.
Now you can set the default font for your PDF using the following command
$html2pdf->setDefaultFont("the_name_you_called_your_font");
This was fairly simple to get working, I am having issues using 2 seperate fonts though via this method. I'll figure it out though
To expand on the selected answer (by o11y_75) when you convert your fonts, you need to use a specific name to include also the bold and italic variants.
That way, you only add one font definition like this
$html2pdf->AddFont('opensans', 'normal', 'opensans.php');
$html2pdf->setDefaultFont('opensans');
When you convert the fonts, name them, for example, like these:
default: opensans
bold: opensansb
italic: opensansi
bold italic: opensansbi
notice that behind the original name, you add b, i and bi on each case.
I have found no documentation on this issue, but I followed the nomenclature found on the fonts that already came with TCPDF and it worked.
If you want to add multiple fonts, just use :
$html2pdf->addFont('opensansregular', '', 'opensansregular');
$html2pdf->addFont('opensansbold', '', 'opensansbold');
I would suggest that you don't use special chars with the font converter specified above.
Then in your CSS simply type :
<style type="text/css">
<!--
.uppercase {
text-transform: uppercase;
}
* {
font-family: opensansregular;
}
h1, h2, h3, strong {
font-family: opensansbold;
}
-->
</style>
HTML2PDF works internally with TCPDF.
TCPDF has its own object since version 6.2.6 to create the fonts needed for HTML2PDF: TCPDF_FONTS
I have solved this as follows:
I searched for TCPDF in the vendor directory and found the Fonts directory there.
Then I created my own separate PDF script and used it once to create the necessary font files from the ttf.
usage: $fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);
For more details see: https://stackoverflow.com/a/70337995/2320007
Related
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
Hy there, I'm using html2pdf v3 to create a PDF.
That works pretty good. But now I wanted to use Google Fonts inside the PDF. So I downloaded Roboto-Regular.ttf and renamed it to roboto.ttf. Then I converted it to .php and .z (Encoding: cp1252): http://www.fpdf.org/makefont/index.php
Then I copied those two files inside the "fonts" folder and included the font:
$html2pdf = new HTML2PDF('P', 'A4', 'de', true, 'UTF-8', array(30,18,30,18));
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->AddFont('roboto', 'normal', 'roboto.php');
$html2pdf->writeHTML($css.$content);
$html2pdf->Output($path.$pdf_name, 'F');
Unfortunately the Roboto Text has some pretty weird widths:
HTML:
<p>This is written with default Font Family</p>
<p style="font-family:roboto">This is written with Roboto</p>
Output:
The same happens when I set Roboto as default font $html2pdf->setDefaultFont('roboto');
Is there any solution for this?
I used now a different encoder: fonts.snm-portal.com Now it works.
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.
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
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).