How to generate multilingual content Pdf in PHP - php

I am using TCPDF to generate multilingual content PDF in PHP. I am using TCPDF and using font 'cid0jp' but It is forcing user to download language pack for adobe reader.
Is there any way to generate multilingual pdf without enforcing user to download any langauge pack?

Yes, there's a way. I just had the same issue. Why Adobe Reader is asking for the language pack i don't know.
But i know that the best way to create multilingual PDFs is to use the Arial Unicode MS-Font which is included in windows. That's the font with the most characters according to wikipedia!! But there's no Bold or Italics. You can buy a Arial Unicode Bold from http://www.linotype.com/en/817674/ArialUnicode-family.html#.
I use the Arial MS Unicode-Font with chinese, japanese, cyrilic. For western languages i use the normal Arial so i have italics and bold.
An other possibility is to use for each language a own font which is made for this language.
To make the Arial MS Unicode to work with tcpdf follow the steps on the answer of this Question: Creating PDFs using TCPDF that supports all languages especially CJK
I hope i could help...

Use html2pdf for multilingual.
I tried with this and it worked for me.I developed site for (English/Japanese)
Used font arialunicid0 for multilingual.
require_once('Classes/library/html2pdf.class.php');
//$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8');
$html2pdf->setDefaultFont('arialunicid0');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$filename = $filename .'_'.date('Ymd');
$html2pdf->Output($filename.'.pdf','D');
It download file with Japanese text.
Only problem I facing here with file name.
IF I pass Japanese character for file name it downloads file with blank name.

Related

How to export emoji to PDF document using PHP?

I am trying to export to PDF using FPDF and TCPDF php library. I found that the emojis like 😁 😀 💃🏻 ❤️ 🥳 where not converted. Only ️️some rectangle box there in generated pdf. I also tried tfpdf.
$text = "There is my text 😁 , 😀 and emojis 💃🏻 ❤️ 🥳";
require('tfpdf/tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
//Add a Unicode font (uses UTF-8)
$pdf->AddFont('Segoe UI Symbol','','seguisym.ttf',true); // DejaVuSans.ttf
$pdf->SetFont('Segoe UI Symbol','',12);
$pdf->Write(8,$text);
$pdf->Output();
I also tried different font. But didn't work for me. Can any one help me in this regard?
Sadly fPDF, TCPDF nor tFPDF can't print those characters. Issue is, these characters are not part of BMP, they are expressed with surrogate pairs, meaning they behave like multiple characters in UTF-16 (because of that one emoticon is printed as 2 rectangle boxes, not one) and also they have codepoint above 65535. However all mentioned PDF libraries relies on codepoint index being <= 65535 as well as TFontFile class reading TTF files.
You would also need to add TTF file having complete set of Unicode charset, or at least emoticons. Most fonts does not have it. This brings another issue for PDF library, which would probably need to have support for fallback font, which will be used when codepoint is not found in main font (for example you want to print text in Gotham, but since that does not include emoji, use other font for them). Btw for example emoji font "Noto Color Emoji" has 23 MB TTF file. So it gets big easily.
Anyway, all of the above can be added to PDF libraries, but it will require some effort. I am planning to do it for my needs as well sometimes. I think it will take roughly 1 man day.
Alternativelly, you might try something more robust like mPDF, but that library is huge, slow and require complete rewrite of your fPDF code. Also can't guarantee it can print emojis as well.

How spipu html2pdf to support variety of languages

I have a project to use spipu/html2pdf package to generate pdf documents, this pdf need to support all variety of languages
I am trying to use freeserif font as default font, the freeserif font support most western languages, including Hindi and Arabic. But seems not support Japanese/Korea/Chinese. Does anyone know which font under TCPDF support all
all variety of languages? or how to can support all variety of languages using spipu/html2pdf package. Thanks.
Here is my test code:
$html2pdf = new Html2Pdf('P', 'A4', 'fr', true, "UTF-8");
$html2pdf->setDefaultFont('freeserif');
$content = nl2br($PageTag);
$html2pdf->writeHTML($content);

Issues with Chinese characters in FPDF

I'm generating some order confirmations using FPDF library, which works great. But now I need some Chinese characters in the document. I have searching for many hours by now and have found no workable solution. Is it possible supporting Chinese characters using FPDF or is there an alternative to FPDF which will support it?
All the characters are mixed with regular text and are stored in a MySQL database. The PDF document only show unicode when printing the Chinese characters.
There exist Chinese encoding formats like Big5 or GBK. However, more likely than not, the text you are trying to input is in Unicode. There exists tFPDF, which provides Unicode support. I will test printing the following traditional hanzi: 中國. This means China, for those reading who do not know.
// Remember to copy msjh.ttf to [path to tFPDF]/font/unifont/ directory
// Initialize tFPDF
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();
// Add a Unicode font like MSJH
$pdf->AddFont('MSJH','','msjh.ttf',true);
$pdf->SetFont('MSJH','',42);
// Output Chinese string to PDF
$pdf->Text(12,42,"中國");
// Output PDF document
$pdf->Output();
Another alternative is the Code200365k TTF
$pdf->AddFont('Code200365k','','Code200365k.ttf',true);
I use it for Chinese characters only as follows:
if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $my_value)){
$pdf->SetFont('Code200365k','',12);
}
$pdf->Cell(130,9,$my_value,1,1,'L');
$pdf->SetFont('Arial','',11);

can't display ¥ with TCPDF, but other kanji are okay

I'm using TCPDF to create PDFs that include Japanese characters. Using the TrueType font ArialUni, most characters are displayed correctly, except the yen symbol shows up as a square box instead of ¥.
Here's a snippet of the resulting PDF using ArialUni:
So I tried another font. Here's the same section of the resulting PDF using GT200001:
And here's the same section using Helvetica:
Here's the same section using GNU's FreeSans:
I would like that second line to show up as "(渋谷猿, ¥8,000)"
I'm not surprised that Helvetica and Freesans cannot render the kanji correctly, but I cannot fathom why the other two fonts can render the kanji, but not the yen symbol, which is much more common.
The web server creating the PDFs is LAMP running Ubuntu. I'm viewing the PDFs on OS X with Chrome (using its in-browser view). I've also tried downloading the PDFs with Firefox and displaying in Preview. I get essentially the same results: ArialUni and GT200001 don't display the yen symbol, while Helvetica and Freesans don't display the kanji (but do display the yen symbol).
I know I can use different fonts for different lines/cells of the PDF, but the kanji and yen symbol are on the same line.
How can I get the kanji and yen symbol to display in a single line using TCPDF?
Near the top of my PDF code, I load the font using TCPDF's addTTFfont();
$this->font = $this->addTTFfont(K_PATH_FONTS.'arialuni.ttf', 'TrueTypeUnicode', '', 32);
Here's the code I'm using to write the section of the PDF.
$pdf->SetFont('arialuni','',10);
$pdf->MultiCell(105, $remarks_height, $remarks, 'B', 'L', false, 0, '', '', true, 1, false, true, $remarks_height, 'T');
In this wikipedia article you can read some more about this character.
Basically, there are 2 different ways of writing this Japanese Kanji, as it happens with some other symbols. From accepted answer "I was using ¥, not ¥", we can see that he was using the 'occidental' or Unicode ¥ symbol, when he actually wanted to use the double-width character ¥.
Oh dear, I figured it out.
I was using ¥, not ¥. Sorry for being confused!!
What I have noticed with TCPDF is, it's all about fonts.
I tried to use it for 15 totally different languages with different writing style too and only font I am using is Arial UNICODE MS.
TCPDF has function which will convert that font into TCPDF friendly files, which are arialuni.ctg.z(70k), arialuni.php(447k), and arialuni.z (14M).
the functions are,
http://www.tcpdf.org/fonts.php
$fontname = $pdf->addTTFfont('/path-to-font/ARIALUNICODE.TTF', 'TrueTypeUnicode', '', 32);

Creating PDFs using TCPDF that supports all languages especially CJK

Can someone put together a clear and concise example of how you can create a PDF using TCPDF that will support text strings from any language?
It appears there is not a single font that will support all languages. I'm guessing the font would be too large?
I assume the correct way would be to detect the language of the string and dynamically set the font type to a compatible font. If this is the case then it gets very complex in detecting the language for each string.
Most languages are supported if you use the "freeserif" font. However it does not support CJK fonts. I've tried many fonts (kozminproregular, cid0jp, cid0kr, cid0jp, stsongstdlight) to get support for Chinese, Japanse, and Korean, but none of them seem to support all three languages.
This worked out perfectly for me. Thank you!
To make sure, the generated PDF file will not get to big, use FontSubsetting - I have a 10 page PDF generated with only a few Lines of chinese (Names on Diplomas)
$pdf->setFontSubsetting(true); => PDF File slightly bigger 925kb vs 755kb without the chinese names
if you use
$pdf->setFontSubsetting(false); => PDF File size as about 17.5 MB ...
Managed this problem by making my own font from arial ms unicode with these steps:
In a temporal script put and execute this
1. put a copy of ARIALUNI.ttf in fonts folder under tcpdf installation (i've taken my copy from windows\fonts folder.
2. make a temporary script in examples folder of tcpdf and execute it with this line:
$fontname = $pdf->addTTFfont('../fonts/ARIALUNI.ttf', 'TrueTypeUnicode', '', 32);
3. set the new font in your pdf generator script:
$pdf->SetFont('arialuni', '', 20);
Now the pdf should be showing correctly CJK characters.
Hope this helps so many people.
I just tried Etiennez0r's solution, and it didn't work for me. Needed to make a minor modification as below:
$fontname = TCPDF_FONTS::addTTFfont('../fonts/ARIALUNI.TTF', 'TrueTypeUnicode', '', 96);
I setting:
$fontname = TCPDF_FONTS::addTTFfont(FCPATH . 'TCPDF/fonts/ARIALUNI.ttf', 'TrueTypeUnicode', '', 32);
.......
// set font
$pdf->SetFont('dejavusans', '', 14);
$pdf->SetFont('cid0cs', '', 14);
Export Japanese is working well

Categories