Office関連

PHPPresentationを使ってPHPからPowerPointファイルを出力してみる。

久しぶりにPHPOfficeを覗いてみたら「PHPPresentation」(LGPL v3ライセンス)が更新されているようだったので、さっそく使ってみました。

PHPPresentationのインストール

PHPPresentationも、PhpSpreadsheetPHPWordと同じく「composer install」するだけで導入できます。

composer.json
{
    "require": {
       "phpoffice/phppresentation": "dev-master"
    }
}

PHPPresentationの呼び出し

インストールが終わったら、さっそくPHPPresentationを使ってPowerPointファイルを作成してみます。

<?php
    require_once './vendor/autoload.php';
    
    use PhpOffice\PhpPresentation\PhpPresentation;
    use PhpOffice\PhpPresentation\IOFactory;
    use PhpOffice\PhpPresentation\Style\Color;
    use PhpOffice\PhpPresentation\Style\Alignment;
    
    //新規プレゼンテーション作成
    $phpPres = new PhpPresentation();
    
    //ドキュメントのプロパティ設定
    $phpPres->getDocumentProperties()
        ->setCreator('@kinuasa')
        ->setCompany('初心者備忘録')
        ->setTitle('PHPPresentation Sample')
        ->setDescription('PHPPresentationのサンプルです。');
    
    //図形(テキスト)追加
    $slide = $phpPres->getActiveSlide();
    $shape = $slide->createRichTextShape()
        ->setHeight(300)
        ->setWidth(600)
        ->setOffsetX(170)
        ->setOffsetY(180);
    $shape->getActiveParagraph()
        ->getAlignment()
        ->setHorizontal(Alignment::HORIZONTAL_CENTER);
    $textRun = $shape->createTextRun('こんにちは、世界!');
    $textRun->getFont()
        ->setName('MS Pゴシック')
        ->setBold(true)
        ->setSize(48)
        ->setColor(new Color('8e44ad'));
    
    //ファイルダウンロード
    //MIMEタイプ:https://technet.microsoft.com/ja-jp/ee309278.aspx
    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename*=utf-8\'\'' . rawurlencode('サンプル.pptx'));
    header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    $xmlWriter = IOFactory::createWriter($phpPres, 'PowerPoint2007');
    ob_end_clean(); //バッファ消去
    $xmlWriter->save('php://output');

上記コードを実行すると、下図のようなファイルが出力されます。

日本語表示も問題ありません。
ただ、リファレンスによれば下記コードで既存ファイルが読み込めるはずなのですが、私が試した限りでは、既存ファイルを元にファイル作成すると、ファイルが破損して上手くいきませんでした。

$oReader = IOFactory::createReader('PowerPoint2007');
$oReader->load(__DIR__ . '/sample.pptx');

このあたりはまだ発展途上といった感じがしますが、使い勝手は良く、直感的に扱えるので、PHPでPowerPointファイルを出力する際には役立つライブラリだと思います。

Nintendo Switch「スナックワールド トレジャラーズ ゴールド」に3DS版からデータ引継ぎはできるの?前のページ

【ワールドネバーランド】Switchでまったり箱庭ゲーム!次のページ

関連記事

  1. Office関連

    “元に戻す”履歴に文字列をセットするPowerPointマクロ

    PowerPointマクロでは、Presentationオブジェクトの…

  2. Office アドイン

    [Office用アプリ]カレンダーから日付を入力するコンテンツアプリ。

    ※ この情報はOffice 2013 カスタマー プレビュー版を元にし…

  3. Office アドイン

    Office用アプリではalertやconfirmが使えない?

    JavaScriptでメッセージや確認ダイアログを表示する際には「al…

  4. Office関連

    メモ帳だけでOutlook用アドインを作ってみる。

    「SharpDevelopでExcel用COMアドインを作成する方法」…

  5. Excel

    プログラムのソースコードを別の言語に変換するVBAマクロ

    SharpDevelopが公開している、ソースコードを変換するAPI「…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP