Created
May 30, 2014 05:02
-
-
Save localdisk/0eb905a4ad7da3d6a9d8 to your computer and use it in GitHub Desktop.
dompdf, mPDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('dompdf', function() | |
{ | |
// 前準備として load_font.php で描画に必要なフォントを読み込ませる。 | |
define("DOMPDF_ENABLE_AUTOLOAD", false); | |
require_once base_path() . "/vendor/dompdf/dompdf/dompdf_config.inc.php"; | |
$domPdf = new DOMPDF(); | |
$domPdf->load_html(View::make('pdf')); | |
$domPdf->set_paper('A4'); | |
$domPdf->render(); | |
$out = $domPdf->output(); | |
$path = storage_path() . '/file.pdf'; | |
File::put($path, $out); | |
return Response::download($path, 'file.pdf', ['Content-Type: application/pdf']); | |
}); | |
Route::get('mpdf', function() | |
{ | |
// 前準備として config.php の | |
// $this->useAdobeCJK = true; | |
// config_fonts.php の $this->fontdata に | |
// "ipaexgothic" => array( | |
// 'R' => "ipaexg.ttf", | |
// ), | |
// を追加。 | |
// config_cp.php に | |
// CASE "ja": | |
// if ($adobeCJK) { | |
// // 先ほど追加した ipaexgothic を追加 | |
// $unifonts = "ipaexgothic,sjis,sjisB,sjisI,sjisBI"; | |
// } | |
// が必要。 | |
$mpdf = new mPDF('ja', 'A4'); | |
// これがないと "mPDF error: HTML contains invalid UTF-8 character(s)" となる。なんでやねん | |
$mpdf->ignore_invalid_utf8 = true; | |
// 第2引数の 1 は headerCSS only とのこと | |
$mpdf->WriteHTML(File::get(public_path() . '/assets/h2/css/pdf.css'), 1); | |
// 第2引数の 0 は default とのこと(省略可能) | |
$mpdf->WriteHTML(View::make('pdf'), 0); | |
$path = storage_path() . '/mpdf.pdf'; | |
$mpdf->Output($path); | |
return Response::download($path, 'mpdf.pdf', ['Content-Type: application/pdf']); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment