I have to generate a PDF from php page contains html which includes unicode fonts (Telugu). Its showing perfectly when I print html code and while rendering to PDF using TCPDF, the unicode characters are distorting of letter formations.
I have copied the telugu font from google translators and added a telugu font into tcpdf lib.
$message = '<h2 align="center">ధన్యవాదములు -- శుభోదయం</h2>';
$fontname = $pdf->addTTFfont('E:\xampp\htdocs\ncs\svdn\flowers\tcpdf\fonts\mandali-regular.ttf', 'TrueTypeUnicode');
$pdf->SetFont($fontname, "",30, false);
$pdf->writeHTML($message, true, 0, true, 0);
$pdf->Output("Test.pdf", 'I');
The output in html is like below. And the same has to be printed in PDF too.
ధన్యవాదములు -- శుభోదయం
But in current issue, the above consonants are disjointed:
TCPDF doesn't support the OpenType layout features needed for Indic scripts to work.
mPDF supports several indic scripts - including Telugu specifically - however, so if you're not too far along in your project to switch, that would be my recommendation.
This code should work for instance (though I have not tested it) after installing mPDF with composer:
include 'vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
//I'm being a little lazy here and letting mPDF select the appropriate
//appropriate font.
$mpdf->autoScriptToLang = true;
$mpdf->baseScript = 1; // Use values in classes/ucdn.php 1 = LATIN
$mpdf->autoLangToFont = true;
$mpdf->WriteHTML('<h2 style="text-align: center;">'.
'ధన్యవాదములు -- శుభోదయం</h2>');
$mpdf->Output('test_mpdf_lang.pdf');
Related
I would like to add a custom font to a pdf I'm generating using TCPDF. I might be missing something but the docs seem to be out dated. They are referencing the addTTFfont() function but I think it's been deprecated and no longer exists in the latest version of TCPDF.
I read that I need to convert the ttf file and put it in the fonts folder so I ran:
php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf
and it generated these files which are now in the fonts folder:
rumpelstiltskinwebfont.ctg.z
rumpelstiltskinwebfont.z
rumpelstiltskinwebfont.php
Then I tried to add the font:
$pdf->addFont('rumpelstiltskin');
$pdf->SetFont('rumpelstiltskin', '', 14, '', false);
but I'm getting an error:
TCPDF ERROR: Could not include font definition file: rumpelstiltskin
I figured out my issue, I was almost there.
Here is a step by step:
First convert your font using the tcpdf_addfont.php tool font in the TCPDF tools folder:
php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf
This will generate the required files and put them in the TCPDF fonts folder. Check the fonts folder and copy the name of the font, in my case it was rumpelstiltskinwebfont.
In your code set the font using the font file's name and write a line of text:
$pdf->SetFont('rumpelstiltskinwebfont', '', 14, '', false);
$pdf->Write(0, 'Fill text', '', 0, '', true, 0, false, false, 0);
That's it. Hope this helps someone. :)
Got this answer in another question and resolved for me. You just need to use the first parameter, the path of the font file. Worked with TTF and OTF fonts.
It generates a name string to use with $pdf->SetFont($fontname, '', $font_size);
Hope it helps.
The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:
// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);
// use the font
$pdf->SetFont($fontname, '', 14, '', false);
For further information and examples, please check the TCPDF Fonts documentation page.
NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!
You can convert ttf fonts with http://fonts.palettize.me and then put uncompressed result in fonts folder into tcpdf class. Then you can add it with $pdf->SetFont('rumpelstiltskinwebfont', '', 14); using the name of the file
Since TCPDF (version 6.2.6) you can use TCPDF_FONTS::addTTFfont directly instead of AddFont.
TCPDF makes three files out of the ttf and puts them into the directory "font" of TCPDF. Once these have been created, they can be used for all future PDFs.
Example:
First (for simplicity) I created a folder in the font folder of TCPDF with the name of the new font, in my example "arialuni" and copied my ttf of this font into it.
If I want to create my font files in a single separate file that does not use Composer autoload, I have to include TCPDF once and also specify the correct path to my ttf.
require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);
I'm using this extension in Yii:
http://www.yiiframework.com/extension/tcpdf/
I would like to write to a pdf file with an external font, for example this:
http://www.1001fonts.com/open-sans-font.html
Here is my code:
$pdf = Yii::createComponent('application.extensions.tcpdf.ETcPdf', 'P', 'cm', 'A4', true, 'UTF-8');
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("Some Body");
$pdf->SetTitle("TCPDF Example 002");
$pdf->SetSubject("TCPDF Tutorial");
$pdf->SetKeywords("TCPDF, PDF, example, test, guide");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$fontRegular = $pdf->addTTFfont(Yii::getPathOfAlias('webroot') . '/OpenSans-Regular.ttf');
$pdf->SetFont($fontRegular, "", 30);
$pdf->Cell(0,10,"Example",1,1,'C');
$pdf->Output("example_002.pdf", "I");
It works, I can read the pdf many programs, except Corel Draw 6X. When I'm opening the pdf it starts with a message of "missing fonts" and I can't replace them any other kind of fonts. Can anyone tell me how should I add the ttf font, or embed it into the pdf?
Thanks in advance!
So the problem is for Corel Draw . You have to Install the font.
1)Download your ttf font from here
2)Extract it and copy the font to C:\Windows\Fonts
3) Restart Corel Draw and make sure the new font is available by creating a text field and selecting the font.
Reference
I have a doubt about using TCPDF and FPDI together.
I am working on a project where I need to modify existing PDF file and generate new PDF, actually existing PDF are Greeting card template and I have to print certain data at certain pages (such as Image on first page, message on 3rd page, artwork on 4th page) to generate final PDF.
I googled and found with TCPDF, it is not possible to manipulate existing PDF, they suggested using FPDI for opening and manipulating existing PDF.
That's where i am stuck. I need TCPDF (it methods to print images, transparent images, utf text, embed font etc) to do what I want to do, but, I need FPDI to start with. :(.
Please help me: is it possible to use both FPDI and TCPDF together? so that I can use features offered by both APIs ?
Thanks in advance...
http://www.setasign.de/products/pdf-php-solutions/fpdi/about/
"As of version 1.2.1 FPDI can be used with TCPDF - a derivate of FPDF."
libraries_load('tcpdf');
libraries_load('fpdi');
$pdf = new FPDI();
$pdf->setSourceFile("%local_file_path%");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage('L', array(3.5, 2), FALSE);
$pdf->useTemplate($tplIdx, 0, 0, 3.5, 2, false);
I was using Drupal at the time so I used libraries_load but require_once should work.
I use PHP and TCPDF to generate online invoices encoded in utf8. I've created font definition files following the tutorial on the tcpdf website using PT-sans .ttf file.
Generated pdf files look fine (accents are displayed correctly) when opened with Adobe Reader. But as soon as you open the pdf file with Adobe Illustrator, you'll see a mix of normal ascii characters and missing characters signs. When opened as attachment in the gmail preview, no text is displayed at all, just images will show up.
I also get unconsistent behavior with Foxit reader. It was ok on one computer (with font installed), but corrupted on another (without the font installed).
Illustrator (on the left) and Foxit reader printscreen: http://dl.dropbox.com/u/14647415/illustrator%20and%20foxit.jpg
Here is a copy of the corrupted file: http://dl.dropbox.com/u/14647415/2011040-3.pdf
PDF's size is ±200kB, so font seems to be embeded correctly.
This seems to happen with any utf8 font, embeded with procedure described on the tcpdf website. When I tried the core cid fonts, that came with the tcpdf library, accented characters were missing, or have been replaced with ?, but the overal layout and text were ok in Illustrator, too. Attached utf-8 fonts like Dejavusans are showing up corrupted, too.
I use 'UTF-8' as a param passed into tcpdf constructor.
class XTCPDF extends TCPDF
{
function __construct(){
parent::__construct('P', 'mm', 'A4', true, 'UTF-8', false);
...
}
...
}
Do you have any ideas on what could be causing this?
Thank you very much in advance.
p.
Problem was obviously caused by TCPDF automatically subsetting the font. This caused problems with charset (characters were "shifted", '0' interpreted as 'A' etc.) in aplications other than Acrobat.
Solution to this is setting:
$pdf->setFontSubsetting(false);
and subsetting your font manually to reduce the resulting file size, based on charset you want to use.
I have several PDF templates that I would like to load and modify and output using tcpdf.
Is it possible to load an existing PDF and use it as a starting point in tcpdf?
You want to use FPDI.
There's some example code here.
I have tried the free version of FPDI but does not support PDF version 1.5 or higher.
If someone else is looking for a free solution I have used TCPDI. You can find it on github https://github.com/pauln/tcpdi
If you are using composer, you can find some fork for composer too. Just search tcpdi on github.
Once you add it to your project, the code is quite simple. It is an extension of TCPDF so all your previous code keep working
This is a snippet from my code. I used it to save a copy of the privacy policy (a static pdf) with the user name and agreement date on each page footer.
// Create new PDF document
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
...
// Add the pages from the source file.
$pagecount = $pdf->setSourceFile($policyPdfPath);
for ($i = 1; $i <= $pagecount; $i++) {
$tplidx = $pdf->importPage($i);
$pdf->AddPage();
$pdf->useTemplate($tplidx);
// Add agreement text in document footer
$pdf->SetXY(15,282);
$pdf->Cell(180, 5, "Documento approvato da {$fullName} il {$date}", 0, 0, 'C');
}
// Send PDF on output
$pdf->Output(FOLDER_PATH . DIRECTORY_SEPARATOR . "{$userId}.pdf", 'F');
For anyone else finding this, it does appear a PARSER and import class were built for TCPDF (https://tcpdf.org/docs/srcdoc/TCPDF/source-class-TCPDF_IMPORT/#50-100) but as of 2018 was still under development.
Its also worth noting that the solutions above do not allow the contents of the PDF pages to be edited. In other words you import the page as a whole, you cannot edit text content or images.
You can use fpdf with fpdi. You can’t modify directly a template, but you can add a text cell with a white background to cache the old content (note that the old content can be read by using some tools). Then save your new pdf. This tools are relatively easy to use. To resolve the fact that fpdi not read 1.5 pdf version in the free version, you can convert 1.5 version in 1.4 version by using ghost script with the exec command. I use this and work fine.