3

For report generation in PHP I am using HTML2PDF.

It works fine with English language but not giving proper output for Japanese language.

How can I set utg8 character in hHTML2PDF library.

Is there a way to achieve this in HTML2PDF library. I am gettign output like "???????????????" instead of Japanese text

In /var/www/html/html2pdf/locale folder following files I found en.csv, fr.cvs, cs.csv, da.csv

Can we get such file for Japanese too.

Below is my code

<?php

$content = ob_get_clean();

// convert to PDF
require_once('Classes/library/html2pdf.class.php');
try {
    $html2pdf = new HTML2PDF('P', 'A4', 'en');
    $html2pdf->pdf->SetDisplayMode('fullpage');
//      $html2pdf->pdf->SetProtection(array('print'), 'spipu');
    $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
    $filename = $filename .'_'.date('Ymd');
    $html2pdf->Output($filename.'.pdf','D');//,'D'
}
catch(HTML2PDF_exception $e) {
    echo $e;
    exit;
}
  • whats your db and table collation? – user1844933 Feb 10 '14 at 10:01
  • 1
    I am able to see same report on my web page in japanese , problem comes when I download in pdf format. –  Feb 10 '14 at 10:03
  • Its just an html page, no databse. –  Feb 10 '14 at 10:03
  • I had face same issue with tcpdf, you can refer [solution1](http://stackoverflow.com/questions/14037887/tcpdf-font-helvetica-not-work-for-japanese-language) and [solution2](http://stackoverflow.com/questions/14379119/how-to-generate-multilingual-content-pdf-in-php) – Roopendra Feb 10 '14 at 10:19
  • I updated question with .csv file , please check and in that way we could fine solution. –  Feb 10 '14 at 11:13

5 Answers5

5

Try to use a specify fonts instead of the default, try this

<?php
    $html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8');
    $html2pdf->setDefaultFont('arialunicid0'); //add this line
    $html2pdf->pdf->SetDisplayMode('fullpage');
    $html2pdf->writeHTML($content, false);
    $html2pdf->Output('japan.pdf');
?>

Reference: http://community.impresscms.org/modules/newbb/viewtopic.php?post_id=43474#forumpost43474

Allen Chak
  • 1,802
  • 1
  • 10
  • 21
  • I added this line but still same issue. –  Feb 11 '14 at 09:12
  • Please take a look about the file "_tcpdf_5.0.002/fonts/arialunicid0.php" exists or not?? Also, please try using simple HTML without font style (HTML / CSS). – Allen Chak Feb 11 '14 at 09:47
  • 1
    It works now, All the content are now in Japanese language. But If I pass Japanese text for file name it gives blank name. –  Feb 26 '14 at 04:50
  • I think you need urlencode('the_japanese_name.pdf') or rawurlencode('the_japanese_name.pdf'). – Allen Chak Feb 26 '14 at 16:14
  • I used as $html2pdf->Output(urlencode('注意.pdf'),'D'); but it download file as named E6B3A8E6848F.pdf . it should be 注意.pdf. What I am missing her. I tried with urlencode and rawurlencode but getting same result. –  Feb 27 '14 at 05:21
1

For polish signs helped this line:

$html2pdf->setDefaultFont('arialunicid0'); //add this line

Look it in Word, for me Helvetica showed squares. After font change problem disapered.

AbC
  • 11
  • 1
1

I solved this issue with this function:

$tpl_data = array_map('utf8_decode',$datas);

Ebarroso
  • 17
  • 3
1
$html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8', []);
$html2pdf->setDefaultFont('cid0jp'); //using this line
$html2pdf->writeHTML($html);
$html2pdf->pdf->SetTitle('PDFダウンロード');
$html2pdf->output('download.pdf');

I'm using this code and it working!

gralias
  • 31
  • 4
0

Set encoding to UTF-8

 $html2pdf = new HTML2PDF('P', 'A4', 'en', true, 'UTF-8');
  • I updated code as per you but still I am getting same output. ????? instead of Japanese text. –  Feb 10 '14 at 09:51
  • @user3165155 run `$content = utf8_encode($your_insert_content)` at the top –  Feb 10 '14 at 09:59
  • I tried it gives me output インターフット使用状æ, this is not Japanese character. –  Feb 10 '14 at 10:04
  • Have you tried above solutions by yourself? Are those work for you? –  Feb 10 '14 at 10:11
  • no, i haven't tried, maybe contact the creators of it –  Feb 10 '14 at 10:13