LoginSignup
1
2

More than 3 years have passed since last update.

画像、PDFをPDF化

Posted at

画像やPDFを1つのPDFにする

tcpdfとfpdiを使用。

jpgやpngなどの画像をPDF化するならtcpdfのみで可能ですが
PDFをPDFにマージするなどもしたい場合はfpdiも必要。

fpdiはtcpdfまたはtfpdfの拡張機能としても使用できるので
fpdiをインストールして、tcpdfの機能を使いたかったらtcpdfもインストールする、
という認識です。

インストール

composer require tecnickcom/tcpdf
composer require setasign/fpdi

処理

<?php

namespace App\Services\Hoge;

use setasign\Fpdi\Tcpdf;
use File;
use Utils;

class HogeService
{
    public function createPdf(string $id)
    {
        // ファイル取得
        $files = Utils::getAllFiles('hoge', $id);

        $maxsize = 550;
        $tcpdf = new Tcpdf\Fpdi('p', 'px', 'A3');
        $tcpdf->SetPrintHeader(false);
        $tcpdf->SetPrintFooter(false);

        foreach ($files as $file) {
            $extension = File::extension($file);
            $file_path = storage_path('app') . '/' . $file;

            if ($extension == 'pdf') {
                $pageCnt = $tcpdf->setSourceFile($file_path);

                for ($i = 1; $i <= $pageCnt; $i++) {
                    $tcpdf->addPage();
                    $tcpdf->useTemplate($tcpdf->importPage($i));
                }
            } else {
                $tcpdf->AddPage();

                // @NOTE:フルパスでないとセットしてくれない
                // 第5引数大文字でないとセットしてくれない
                $img_size = getimagesize($file_path);
                $set_size = ($img_size[0] >= $maxsize) ? $maxsize : $img_size[0];

                $tcpdf->Image(
                    $file_path,                                // 画像ファイル名
                    5,                                         // 領域左上のX座標
                    5,                                         // 領域左上のY座標
                    $tcpdf->pixelsToUnits($set_size),          // 領域の幅 [指定しない場合、自動計算される]
                    0,                                         // 領域の高さ [指定しない場合、自動計算される]
                    strtoupper($extension),                    // 画像フォーマット
                    '',                                        // AddLink()で作成したリンク識別子
                    '',                                        // align
                    ($img_size[0] >= $maxsize) ? true : false, // resize
                    300,                                       // dpi
                    '',                                        // palign
                    false,                                     // ismask
                    false,                                     // imgmask
                    0,                                         // border
                    false,                                     // fitbox
                    false,                                     // hidden
                    false,                                     // fitonpage
                    false,                                     // alt
                );
            }
        }

        $pdf_path = storage_path('app') . "/data/hoge/$id/hoge_$id.pdf";
        // 保存
        $tcpdf->output($pdf_path, 'F');
    }
}
1
2
0

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
1
2