メモだけ。
参考
composer initの分かりやすい記事ですね。
PHP Composer のインストールと使い方
Mac で Homebrew を使って Composer をインストールして使う方法についての覚書です。コマンドラインを使ったインストールや更新方法などについても解説しています。version 2 へのアップグレード方法を追加
こんな感じ
PowerShellを開く。
まず作業するディレクトリに移動。
cd C:\xampp\htdocs\mPDFtestinitをやっていく。
composer init最初の質問
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [****/yyyymmdd]:好きな名前でいいけどこんな感じに打ってEnter
mpdf/my_project202507以降こんな感じ。
コメントアウト部分で各行のやっていくことを書いています。
Description []: # Enter
Author [**** <****@example.com>, n to skip]: # 「n」と打ってEnter
Minimum Stability []: # Enter
Package Type (e.g. library, project, metapackage, composer-plugin) []: # Enter
License []: # Enterこれが表示されて
Define your dependencies.こう。
Would you like to define your dependencies (require) interactively [yes]? # 「yes」と打ってEnter
Search for a package: # 「mpdf」と打ってEnterこれを
Found 15 packages matching mpdf
[0] mpdf/mpdf
[1] mpdf/psr-log-aware-trait
[2] mpdf/psr-http-message-shim
[3] kartik-v/yii2-mpdf
[4] carlos-meneses/laravel-mpdf
[5] niklasravnsborg/laravel-pdf Abandoned. No replacement was suggested.
[6] mpdf/qrcode
[7] misterspelik/laravel-pdf
[8] contributte/pdf
[9] bithost-gmbh/pdfviewhelpers
[10] joseki/pdf-response Abandoned. Use contributte/pdf instead.
[11] omaralalwi/gpdf
[12] misterspelik/mpdf
[13] kartik-v/mpdf
[14] zanysoft/laravel-pdfこう。
Enter package # to add, or the complete package name if it is not listed: #「0」と打ってEnter
Enter the version constraint to require (or leave blank to use the latest version): # Enter
Using version ^8.2 for mpdf/mpdf
Search for a package: # Enter
Would you like to define your dev dependencies (require-dev) interactively [yes]? # Enter
Search for a package: # Enter
Add PSR-4 autoload mapping? Maps namespace "Mpdf\MyProject202507" to the entered relative path. [src/, n to skip]: #「n」と打ってEnterこれが出て
{
"name": "mpdf/my_project202507",
"require": {
"mpdf/mpdf": "^8.2"
}
}こう。
Do you confirm generation [yes]? # 「yes」と打ってEnter
Would you like to install dependencies now [yes]? # 「yes」と打ってEnterこうなって
Loading composer repositories with package information
Updating dependencies
Lock file operations: 8 installs, 0 updates, 0 removals
- Locking mpdf/mpdf (v8.2.5)
- Locking mpdf/psr-http-message-shim (1.0.0)
- Locking mpdf/psr-log-aware-trait (v2.0.0)
- Locking myclabs/deep-copy (1.13.3)
- Locking paragonie/random_compat (v9.99.100)
- Locking psr/http-message (1.0.1)
- Locking psr/log (1.1.4)
- Locking setasign/fpdi (v2.6.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 8 installs, 0 updates, 0 removals
0 [>---------------------------] 0 [>---------------------------]
- Installing setasign/fpdi (v2.6.3): Extracting archive
- Installing psr/log (1.1.4): Extracting archive
- Installing psr/http-message (1.0.1): Extracting archive
- Installing paragonie/random_compat (v9.99.100): Extracting archive
- Installing myclabs/deep-copy (1.13.3): Extracting archive
- Installing mpdf/psr-log-aware-trait (v2.0.0): Extracting archive
- Installing mpdf/psr-http-message-shim (1.0.0): Extracting archive
- Installing mpdf/mpdf (v8.2.5): Extracting archive
0/8 [>---------------------------] 0%
6/8 [=====================>------] 75%
7/8 [========================>---] 87%
8/8 [============================] 100%
2 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
3 packages you are using are looking for funding.
Use the `composer fund` command to find out more!おしまい。
移植できるよ
ディレクトリ内に生成されたものを全部コピペすれば、別環境でも動かすことができる。
ということで、非Composer環境でもやっていけます。
よかったですね。
日本語対応方法
ここを読めば解決する。
<?php
require_once __DIR__.'/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf([
'mode' => 'ja',
'format' => 'A4',
'fontdata' => [
'ipa' => ['R' => 'ipaexg.ttf'],
],
'default_font' => 'ipa',
]);
$html = <<<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>PDFサンプル</title>
</head>
<body>
<h1>こんにちは、mPDF!</h1>
<p>これは日本語のPDF出力サンプルです。</p>
</body>
</html>
HTML;
$mpdf->WriteHTML($html);
$mpdf->Output();
fontdataを指定しただけでも適用されたのはなぜでしょう。
フォントを個別指定することも可能。
<?php
require_once __DIR__.'/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf([
'mode' => 'ja',
'format' => 'A4',
'fontdata' => [
'ipaexg' => ['R' => 'ipaexg.ttf'],
'ipaexm' => ['R' => 'ipaexm.ttf'],
],
'default_font' => 'ipaexg',
]);
$html = <<<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>PDFサンプル</title>
</head>
<body>
<style>
</style>
<h1 style="font-family: ipaexg;">ゴシック体タイトル</h1>
<p style="font-family: ipaexm;">これは明朝体の段落です。</p>
<p style="font-family: Arial;">This is an English paragraph in Arial.</p>
</body>
</html>
HTML;
$mpdf->WriteHTML($html);
$mpdf->Output();fontdata内に設定してあるものから選ぶ感じですね。
ついでに、ヒアドキュメント(<<<のやつ)内で変数を使うこともできる。
<?php
require_once __DIR__.'/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf([
'mode' => 'ja',
'format' => 'A4',
'fontdata' => [
'ipaexg' => ['R' => 'ipaexg.ttf'],
],
'default_font' => 'ipaexg',
]);
$sample = "あああああああああ";
$html = <<<HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>PDFサンプル</title>
</head>
<body>
<style>
</style>
<p>お名前は{$sample}です。</p>
</body>
</html>
HTML;
$mpdf->WriteHTML($html);
$mpdf->Output();

コメント