我创建了一个使用Laravel dompdf导出下载PDF文件的函数。我有一个日语的UTF-8 Unicode错误。 pdf文件显示日语不正确。当我将PDF中的文本复制到word文件时,它可以正确显示。这是pdf文件http://inofthing.com/element_list_pdf.pdf
我在控制器和视图中创建了一个函数来生成pdf并下载如下。
控制器:
public function exportPdf(Request $request)
{
$project_id = $request->project_id;
// Fetch all element from database
$data = DB::table('element_designs')
->join('elements', function($join){
$join->on('element_designs.id', '=', 'elements.element_design_id')
->where('elements.deleted_at', null);
})
->leftJoin('tag_mappings', function($join){
$join->on('elements.id' , '=', 'tag_mappings.element_id')
->where('tag_mappings.deleted_at', null);
})
->leftJoin('rfid_tags', function($join){
$join->on('tag_mappings.tag_id', '=', 'rfid_tags.id')
->where('rfid_tags.disable_flg', false);
})
->leftJoin('districts', function($join){
$join->on('element_designs.district_id', '=', 'districts.id')
->where('districts.disable_flg', false);
})
->leftJoin('statuses', function($join){
$join->on('elements.status_id', '=', 'statuses.id')
->where('statuses.disable_flg', false);
})
->leftJoin('locations', function($join){
$join->on('elements.location_id', '=', 'locations.id')
->where('locations.disable_flg', false);
})
->leftJoin('areas', function($join){
$join->on('elements.area_id', '=', 'areas.id')
->where('areas.disable_flg', false);
})
->leftJoin('delivery_date_mappings', function($join){
$join->on('elements.id', '=', 'delivery_date_mappings.element_id')
->where('delivery_date_mappings.deleted_at', null);
})
->leftJoin('delivery_dates', function($join){
$join->on('delivery_date_mappings.delivery_date_id', '=', 'delivery_dates.id')
->where('delivery_dates.disable_flg', false);
})
->where('element_designs.disable_flg', false)
->where('element_designs.project_id', $project_id)
->orderBy('element_designs.id')
->select('element_designs.id as element_design_id', 'element_designs.doc_no', 'elements.id as element_id', 'element_designs.project_id as project_id',
'tag_mappings.created_at as create_time', 'tag_mappings.id as tag_mappings_id', 'element_designs.element_name', 'elements.element_seq_id',
'districts.district_name', 'element_designs.material_name', 'element_designs.element_size', 'element_designs.element_length', 'element_designs.single_weight', 'element_designs.remarks',
'element_designs.total_weight', 'rfid_tags.device_id', 'statuses.status_name', 'locations.location_name', 'areas.area_name',
'elements.latitude', 'elements.longitude', 'delivery_date_mappings.delivery_date_id', 'delivery_dates.delivery_date',
'elements.updated_at', 'statuses.status_name', 'statuses.id as statuses_id')
->paginate(100);
// Send data to the view using loadView function of PDF facade
$view = view('element.pdf', ['items' => $data]);
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
$html_decode = html_entity_decode($html);
$pdf = \PDF::loadHTML($view)
->setPaper('a4', 'landscape')
->setWarnings(false)
->setOptions(['isFontSubsettingEnabled' => true]);
// Store pdf file in the server
return $pdf->download('element_list.pdf');
//return view('element.pdf', ['items' => $data]);
}
风景:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Export element list to PDF</title>
<style type="text/css">
/*@import url(https://fonts.googleapis.com/css?family=Kosugi);*/
/*Nunito font*/
/*@import url(//fonts.googleapis.com/earlyaccess/notosansjapanese.css);
@font-face {
!*font-family: "Noto Sans", "Noto Sans CJK JP", sans-serif;*!
font-family: 'Noto Sans Japanese', sans-serif;
src: url({{ storage_path('fonts/NotoSansJP-Regular.otf') }}) format("otf");
font-weight: normal;
font-style: normal;
font-size: 8px;
}*/
/*Nunito font
* @import url(https://fonts.googleapis.com/css?family=Nunito);
*/
/* vietnamese */
@font-face {
font-family: 'Nunito';
font-style: normal;
font-weight: 400;
src: local('Nunito Regular'), local('Nunito-Regular'), url({{ storage_path('fonts/XRXV3I6Li01BKofIOuaBXso.woff2')}}) format('woff2');
unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
}
/* latin-ext */
@font-face {
font-family: 'Nunito';
font-style: normal;
font-weight: 400;
src: local('Nunito Regular'), local('Nunito-Regular'), url({{ storage_path('fonts/XRXV3I6Li01BKofIO-aBXso.woff2')}}) format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Nunito';
font-style: normal;
font-weight: 400;
src: local('Nunito Regular'), local('Nunito-Regular'), url({{ storage_path('fonts/XRXV3I6Li01BKofINeaB.woff2')}}) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
/*End nunito fonts*/
*{
font-family: 'Nunito', sans-serif;
}
body {
font-family: 'Nunito', sans-serif;
font-size: 8px;
}
.table {
border-collapse: collapse;
width: 100%;
}
.table td, .table th {
border: 1px solid #000000;
padding: 8px;
}
.table tr:nth-child(even){}/*background-color: #f2f2f2;*/
.table tr:hover {}/*background-color: #ddd;*/
.table th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #fff6a1;
font-weight: normal;
}
</style>
</head>
<body>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>図面No</th>
<th>製品名</th>
<th>工区</th>
<th>鋼材</th>
<th>部材</th>
<th>長さ</th>
<th>単重</th>
<th>鉄板その他</th>
<th>重量</th>
<th>タグID</th>
<th>ステータス</th>
<th>ロケーション</th>
<th>エリア</th>
</tr>
</thead>
<tbody>
@foreach ($items as $item)
<tr>
<td>{{$item->element_design_id}}</td>ID
<td>{{$item->doc_no}}</td>図面No
<td>{{$item->element_name}}</td>製品名
<td>{{$item->district_name}}</td>
<td>{{$item->material_name}}</td>鋼材
<td>{{$item->element_size}}</td>部材
<td>{{$item->element_length}}</td>長さ
<td>{{$item->single_weight}}</td>単重
<td>{{$item->remarks}}</td>
<td>{{$item->total_weight}}</td>
<td>{{$item->device_id}}</td>
<td>{{$item->status_name}}</td>
<td>{{$item->location_name}}</td>
<td>{{$item->area_name}}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
</html>
我使用的是Laravel 5.7和PHP 7。
请参阅上面的代码并帮助我。非常感谢,BienHV
更改
// Send data to the view using loadView function of PDF facade
$view = view('element.pdf', ['items' => $data]);
$html = mb_convert_encoding($view, 'HTML-ENTITIES', 'UTF-8');
$html_decode = html_entity_decode($html);
$pdf = \PDF::loadHTML($view)
->setPaper('a4', 'landscape')
->setWarnings(false)
->setOptions(['isFontSubsettingEnabled' => true]);
// Store pdf file in the server
return $pdf->download('element_list.pdf');
//return view('element.pdf', ['items' => $data]);
成
$pdf = PDF::loadView( 'element.pdf', ['items' => $data] );
return $pdf->stream( 'element.pdf' )->header('Content-Type','application/pdf');