domPDF create an empty PDF with custom font - php

I using domPDF and I' trying to add a custom font (neosans).
I use dompdf/www/fonts.php to add my font into domPDF. .ufm and .ttf files were created. I used a online convert to create .afm file and I put it in the fonts folder.
The font name is added into dompdf_font_family_cache.dist.php and dompdf_font_family_cache.php.
In CSS (<style> tag in the document) I set :
font-family: neosans;
but when I display the PDF the font is blank.
If I set Arial it's ok.

Related

Create pdf which content html and gujarati text in Laravel

I have try to create pdf in Laravel.
Usebarryvdh/laravel-dompdf vendor package class.
My pdf content English and Gujarati text.
but gujarati content print as ?????? in pdf.
I have create pdf succesfully use this type of code
pdfview is my pdf php, html content file view
view()->share('data',$data);
$pdf = PDF::loadView('pdfview');
return $pdf->stream('pdfview.pdf');
// return $pdf->download('pdfview.pdf');
I have expected to pdf content as English and Gujarati text content.
Actual result in pdf Gujarati content print like ????
use wkhtmltopdf, make sure to install all unicode library related to Gujarati language. I tried it for sinhala in ubuntu and centos.
"????" is because the font in not there by default in barryvdh/laravel-dompdf package.
1.You need to download the fonts that supports gujrati.
2.After downloading, just put the the .ttf file to your font assets folder.
3.Then use the css #font-face to explicitly declare your font supporting the gujrati characters.
Just for example (using simhei.ttf). FYI, this is in my template.blade.php file
<style type="text/css">
#font-face {
font-family: SimHei;
src: url('{{base_path().'/public/report_assets/'}}fonts/simhei.ttf') format('truetype');
}
* {
font-family: SimHei;
}
</style>
<body>
//gujrati characters
.......
Thats it

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

How to add TTF font to html2pdf PHP program

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

binding font files with PDF document in TCPDF

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

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