Laravel Framework HTML ->PDF 변환 

* Laravel 5.6 , PHP 7.2  사용


1. DOMPDF

- barryvdh/laravel-dompdf 를 composer.json 의 require 안에 추가한다.

- composer update

- composer.json[require] 안에 barryvdh/laravel-dompdf:0.8.* 이 추가됬는지 확인
(2018-07-20) 기준

- vendor/barryvdh/laravel-dompdf 생성확인

-confing/app.php 

provider

Barryvdh\DomPDF\ServiceProvider::class,

alias 

'PDF' => Barryvdh\DomPDF\Facade::class,

항목추가

- vendor/barryvdh/laravel-dompdf/config 안에 domdpf.php 설정확인
*font 경로
"font_dir" => storage_path('/fonts'),
* 외부 CSS 허용
"enable_remote" => true,

Controller 생성
php artisan make:controller PDFController

*PDFController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;use PDF;
class PDFController extends Controller {
   public function pdf_download(){      
        $pdf = PDF::loadView('PDFDownload');      
        return $pdf->download('example.pdf');
   }
   public function pdf_stream(){
      $pdf = PDF::loadView('PDFDownload');
      return $pdf->stream('example.pdf');
   }
   public function show(){
      return view('PDFDownload');
   }
}
cs
Route
*web.php
Route::get('download','PDFController@download');
Route::get('stream', 'PDFController@pdf_stream');
Route::get('show', 'PDFController@show');

View
*PDFDownload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * Created by PhpStorm.
 * User: dongeon
 * Date: 18. 7. 12
 * Time: 오후 3:12
 */
?>
        <!doctype html>
<html lang="ko">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Document</title>
    <style>
        *{
            font-family:'NanumGothic', '나눔고딕', 'dotum', '돋움';
        }
    </style>
</head>
<body>
<h1>PDF 다운로드 예제</h1>
<hr>
테이블에 있는것만 변환되니
<a href="www.naver.com">호로론ㅁㅇ러만아러먼ㅇㄹㄴㅁㅇ리ㅏㅓㅏㅣ</a>
<svg width="100" height="100">
    <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
    하차차차차차카카카카
</svg>
<table>
    <colgroup>
        <col style="width:5%">
        <col style="width:55%">
        <col style="width:20%">
        <col style="width:10%">
        <col style="width:10%">
    </colgroup>
    <thead>
    <tr>
        <td>no</td>
        <td>제목</td>
        <td>작성자</td>
        <td>등록일</td>
        <td>조회수</td>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>7</td>
        <td>야래야래</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>71</td>
    </tr>
    <tr>
        <td>6</td>
        <td>머리도 BAMMMmmmm</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>25</td>
    </tr>
    <tr>
        <td>5</td>
        <td>똥꾸멍 BAmmmm</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>56</td>
    </tr>
    <tr>
        <td>4</td>
        <td>설사도 하고요</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>14</td>
    </tr>
    <tr>
        <td>3</td>
        <td>오환이 와요</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>24</td>
    </tr>
    <tr>
        <td>2</td>
        <td>장염 조심하세요 여러분</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>27</td>
    </tr>
    <tr>
        <td>1</td>
        <td>DOMPDF와 함께하는 즐거운 PDF 다운로드 예제</td>
        <td>관리자</td>
        <td>2018-07-10</td>
        <td>21</td>
    </tr>
    </tbody>
</table>
<img src="images/back_y.jpg" alt="img" width="400px" height="200">
</body>
</html>
cs

view 에서 유의사항
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    /* meta 데이터에 꼭 utf-8을 설정 해줄것"*/

    <style>
        *{
           font-family:'NanumGothic', '나눔고딕', 'dotum', '돋움';
         }
    </style>

가이드에는
@font-face{
   /...... /
}
로 해결하라고 나와있지만 난어떻하는지 몰라서 일단 style에 하드코딩을 함
추후 문제가 해결되면 업데이트 예정


결과

PDF 로 컨버팅후 stream 한결과임


문제점

개인적인 자료수집에 의거하면 현재(2018-07-12)를 기준으로 Laravel framework 에 dompdf를 적용한 laravel-dompdf 의 0.8.* 버전 한글 컨버팅에 대한자료는 극히드물다.
대부분은 dompdf-0.6.* 버전을 윈도우 기반으로 프레임워크를 적용하지 않고 작업한 사례들이 많다.

  위 결과물에 최 상단 PDF??? ?? 은 h1 태그 를 사용했으며 table에는 tr과 td 태그로만 이루어져있고 th 태그안의 한글이 컨버티이 안되는 현상이 지속되었다.

댓글