DomPDF generation for chinese characters - php

I am trying to generate a PDF that will contain Chinese characters using dompdf.
Here is my code:
require('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF();
mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);
$html = ' <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
*{ font-family: DejaVu Sans, font-size: 12px;}
</style> </head> <body>
忠烈祠
</body>
</html>';
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
$filename = 'a.pdf';
$path = $filename;
file_put_contents($path, $output);
The problem is the the generated PDF show only squares when i open it with chrome or adobe reader, but it looks ok in Mozilla firefox.
Any sugestions?

First, move def("DOMPDF_UNICODE_ENABLED", true); above the require since the def function included with dompdf only defines the constant if it doesn't exist. When you include dompdf_config.inc.php that constant will be set at that time. Also, use define instead.
Second, you need a different font. The DejaVu fonts included with dompdf do not support CJK. For an overview read my answer to Dompdf and set different font-family.
If you do not currently have a font you might try looking up Firefly Sung. Whatever font you decide to use it should be TTF in order for dompdf to use it.
Here's an example using Firefly Sung via the #font-face CSS rule, no installation required:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#font-face {
font-family: 'Firefly Sung';
font-style: normal;
font-weight: 400;
src: url(http://eclecticgeek.com/dompdf/fonts/cjk/fireflysung.ttf) format('truetype');
}
* {
font-family: Firefly Sung, DejaVu Sans, sans-serif;
}
</style>
</head>
<body>
<div>忠烈祠</div>
</body>
</html>

Related

Dompdf Unicode display as Question marks

I use dompdf for generate PDF. Unicode characters I use inside the document. In my Generated PDF document Unicode characters display as ???. My code are below.
$html='<!DOCTYPE html><html>';
$html.='<head><meta charset="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style>
#font-face {
font-family: latha;
font-style: normal;
font-weight: 400;
src: url(http://eclecticgeek.com/dompdf/fonts/latha.ttf) format("true-type");
}body {
font-family: latha;
}
</style></head><body><h3>வணக்கம்</h3></body></html>';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream();

Dompdf not supporting chinese languages

Chinese characters work fine in the HTML page, but when i try to covert HTML to PDF with below code, all Chinese characters got converted to question marks.
I tried to use all types of chinese font libraries but still no success. Please help me fix it.
$html = '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type: application/pdf; charset=utf-8"/>
<link rel="stylesheet" href="style.css">
<div style="width:800px; height:970px; padding:20px; border: 10px solid #787878">
<style>
body {
body {font-family: "simsun"}
}
</style>
<div style="width:750px; height:915px; padding:20px; border: 5px solid #787878">
</br>
<div><img class="imgA1" src="logo.png" ></div>
<!--<img class="imgB1" src="logo.png">-->
</br></br></br></br>
<h6 style="font-size:16px !important; text-align:center;"> <b>'.$goal.'</b></h6><br/><br/>
<b>立約人</b></br></br>
甲方 : <span style="font-size:18px;"><b>'.$user[0]['user_name'].'<b> </span>
<br><br>
乙方 : <span style="font-size:18px;"><b>'.$judge_name.'</b></span><br/><br/>
<b>四. 本合約壹式貳份,雙方各執壹份為憑。</b><br/><br/>
裁判簽名___________________<br/><br/>s中華民國 月 日
</div>
</div></html>';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
/* Render the HTML as PDF */
$dompdf->render();
header('Content-Type: application/pdf; charset=utf-8');
header('Content-disposition: inline; filename="' . $no . '.pdf"', true);
/* Output the generated PDF to Browser */
$dompdf->stream();
#font-face {
font-family: 'Firefly Sung';
font-style: normal;
font-weight: 500;
src: url(http://eclecticgeek.com/dompdf/fonts/cjk/fireflysung.ttf)
format('truetype');
}
* {
font-family: Firefly Sung, DejaVu Sans, Verdana, Arial, sans-serif;
}

Why float not working in laravel dompdf?

I use Laravel 5.3
I use :
"barryvdh/laravel-debugbar": "^2.3",
"barryvdh/laravel-dompdf": "^0.7.0",
"dompdf/dompdf": "0.7.x#dev",
I get from here : https://github.com/barryvdh/laravel-dompdf
In config\dompdf.php, I set :
"DOMPDF_ENABLE_CSS_FLOAT" => true,
In my html pdf, I try :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Test</title>
<style type="text/css">
body {
font-family: "Arial", Helvetica, sans-serif !important;
font-size: 9px;
margin-bottom: 100px;
}
.container {
background:#fff;
overflow:auto;
}
.left {float:left;}
.right {float:right;}
</style>
</head>
<body>
<div class="container">
<div class="left">Float left</div>
<div class="right">Float right</div>
</div>
</body>
</html>
The float not working
How can I solve it?
Update :
The result of pdf is like this :
Use
display: inline-block;
instead of float.
I use the older version of dompdf in my project. I enable DOMPDF_ENABLE_CSS_FLOAT true in dompdf_config.custom.inc file. But the float result is overlapping and distorted.
The inline-block is the new and better way than using float left every time. Visit the following w3school link to more info. CSS Layout - inline-block
You should use the following.
.container{
...
display : block;
}

css sheet not effecting php echo

I am using PHP to check a form. If the content is not filled in the PHP will echo an error message. I want that text to be effected by my style sheet. It is not. The CSS effects the rest of the page, just not that error text. I tried adding the span and class in with the echo and I tried adding the span and class to the html and just echo the text. No effect.
the HTML
<meta charset="utf-8">
<link rel="stylesheet" href="run.css" type="text/css">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
the PHP
<span class="errormsg2">
<?php
if(isset($notset)){
echo $notset;
}
?>
</span>
and the CSS
.pushme:hover{
background-color: #eb5300;
color: black;
text-shadow: none;
}
.errormsg2{
text-align: center;
color: red;
font-weight: bold;
}
The PHP example I provided was from my attempt to put the span and class into the HTML and just echo out the text content.
I believe that the problem is that your file extension should be filename.php instead of filename.html
and because php is a server side language you will have to run your code in a localhost mode to see the change.
if you try to run php code in a filename.html the browser will comment your your php code.
// filename.php file, this will work for you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
<style>
.errormsg2{
text-align: center;
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<span class="errormsg2">
<?php
$notset = "test";
if(isset($notset)){
echo $notset;
}
?>
</span>
</body>
</html>

header not working online but works perfect in localhost

I am creating a doc in php. My code works fine in localhost but it doesn't work when i put it to online. I guess the header is not working properly. How to resolve this issue .The problem is with the adding content-type and content-disposition in header. Any way to add those in a different way ? My code is given below.
<?php
error_reporting(0);
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=Productname.doc");
$str = mb_convert_encoding($str, "EUC-JP", "auto");
$conn = oci_connect("NPM_OPL", "NPM_OPL", "192.168.10.35:1521/pdbopl");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
#font-face { font-family: Solaimanlipi; src: url('SolaimanLipi.ttf'); }
table {
border-collapse: collapse;
font-size-adjust:inherit;
font-size:12px;
}
table, td, th {
border: 1px solid black;
}
th {
font-weight:normal !important;
}
</style>
</head>
<body>
<?php
for($i=0;$i<=$_POST['max_item'];$i++){
$check=$_POST['check_'.$i];
if(!empty($check)){
$STATUS=$_POST['STATUS_'.$i];
$srlno=$_POST['SLNO_'.$i];
if($STATUS=='M'){
?>
<?php
?>

Categories