LoginSignup
9

More than 3 years have passed since last update.

ExcelのテンプレートをもとにPHPでPdf出力する

Last updated at Posted at 2020-12-22

概要

Laravelを使用した環境にて、以下を行います。

  • Excelのテンプレートを読み込む。
  • 読み込んだテンプレートに任意の値を挿入。
  • 任意の値を挿入したExcelを出力。
  • そのExcelをPdfに変換。

Laravel Excel(PhpSpreadsheet)のインストール

※Laravel Excelを使おうとしたら、結局Laravel Excel内で使用してるPhpSpreadsheetで完結してしまったという私の背景があります。ここでは状況を合わせてLaravel Excelのインストールを行います。

Laravelのホームディレクトリで以下を実行

COMPOSER_MEMORY_LIMIT=-1 composer require maatwebsite/excel

LibreOfficeのインストール

以下を実行

sudo yum -y install libreoffice libreoffice-langpack-ja

インストールできたかバージョン表示で確認

libreoffice --version

LaravelのStorageにディレクトリ作成・Excelテンプレートの配置

Excelのテンプレートを配置。storage/app/excel/template/template.xlsx
Excelを出力するディレクトリを作成。storage/app/excel/export
Pdfを出力するディレクトリを作成。storage/app/pdf/export

↓こんな感じ。

storage/
 ┗app/
  ┣excel/
  ┃ ┣export/
  ┃ ┗template/
  ┃  ┗template.xlsx
  ┗pdf/
   ┗export/

実装

ルーティング

/routes/web.php
<?php

use Illuminate\Support\Facades\Route;

Route::get('pdf', 'ExportController@pdf')->name('pdf');

コントローラー

/app/Http/Controllers/ExportController.php
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Services\ExportService as ExportService;
use Illuminate\Support\Facades\Storage;

class ExportController extends Controller
{
    public function pdf()
    {
        $file_name = 'filename_' . date('YmdHis');
        $export_service = new ExportService();
        $export_service->makePdf($file_name);
        $file_path = Storage::path('pdf/export/' . $file_name . '.pdf');
        $headers = ['Content-Type' => 'application/pdf'];
        return response()->download($file_path, $file_name . '.pdf', $headers);
    }
}

サービス

/app/Services/ExportService.php
<?php

namespace App\Services;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;

class ExportService
{
    public function makePdf($file_name)
    {
        // もとになるExcelを読み込み
        $excel_file = storage_path('app/excel/template/template.xlsx');
        $reader = new XlsxReader();
        $spreadsheet = $reader->load($excel_file);

        // 編集するシート名を指定
        $worksheet = $spreadsheet->getSheetByName('hoge');

        // セルに指定した値挿入
        $worksheet->setCellValue('A1', 'fugafuga');

        // Excel出力
        $writer = new XlsxWriter($spreadsheet);
        $export_excel_path = storage_path('app/excel/export/' . $file_name . '.xlsx');
        $writer->save($export_excel_path);

        // Pdf出力
        if (file_exists($export_excel_path)) {
            $export_pdf_path = storage_path('app/pdf/export');
            $cmd = 'export HOME=/tmp; libreoffice --headless --convert-to pdf --outdir ' . $export_pdf_path . ' ' . $export_excel_path;
            exec($cmd);
        }
    }
}

あとがき

サンプルなのでExcel出力とPdfを一緒のメソッド内でやってしまっているところなどありますが、
見積書などpdfで出力する際のご参考になればと思います。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9