Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
Search
hirobe
March 08, 2024
Programming
0
980
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
PHPerKaigi2024 LT登壇資料です
hirobe
March 08, 2024
Tweet
Share
More Decks by hirobe
See All by hirobe
PHP8.1で、リソースがオブジェクトに!? マイナーリリースの変更が レガシープロダクトに与えた影響
hirobe1999
0
770
フレームワークが存在しない時代からのレガシープロダクトを、 Laravelに”載せる”実装戦略
hirobe1999
0
960
フレームワークが存在しない時代からのレガシープロダクトを、 Laravelに”載せる”実装戦略
hirobe1999
0
1.2k
新卒PHPer奮闘記 ~配属されたのは3歳違いのプロダクト!?~ / phperkaigi-2022-lt
hirobe1999
0
1.2k
Other Decks in Programming
See All in Programming
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
210
Node.js v22 で変わること
yosuke_furukawa
PRO
9
3.5k
Git Rebase
bkuhlmann
11
1.6k
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
27
8.3k
MetricKitで予期せぬ終了を検知する話 / Detect unexpected termination with MetricKit
nekowen
1
190
Apache Hive 4 on Treasure Data
ryukobayashi
0
340
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
7
940
Micro Frontends for Java Microservices - Devnexus 2024
mraible
PRO
0
500
PostmanでAPIの動作確認が楽になった話
h455h1
0
170
Site Reliability Engineering for GMO
pyama86
8
1k
Featured
See All Featured
Creatively Recalculating Your Daily Design Routine
revolveconf
210
11k
We Have a Design System, Now What?
morganepeng
43
6.8k
Mobile First: as difficult as doing things right
swwweet
216
8.6k
How STYLIGHT went responsive
nonsquared
92
4.8k
The Invisible Side of Design
smashingmag
294
49k
Statistics for Hackers
jakevdp
789
220k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
The Pragmatic Product Professional
lauravandoore
25
5.8k
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.6k
Become a Pro
speakerdeck
PRO
11
4.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
19
1.7k
Transcript
© RAKUS Co., Ltd. 1 PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話 #phperkaigi
2024/03/09 廣部知⽣
2 ⾃⼰紹介 21卒で株式会社ラクスに⼊社 PHPでMail Dealerの開発を⾏っている 『良いコード悪いコードで学ぶ設計⼊⾨』や 『ちょうぜつソフトウェア設計⼊⾨』で 設計を学ぶ⽇々…… スト6で戦いの螺旋を登り続けています (Act2はMR1700でフィニッシュ)
© RAKUS Co., Ltd. 3 PHPでOfficeファイル取り扱いたいな〜
© RAKUS Co., Ltd. 4 皆さん、思ったこと、ありますよね?
© RAKUS Co., Ltd. 5 あるある〜!
© RAKUS Co., Ltd. 6 できます!
7 PHPでOfficeを取り扱うためのライブラリ群 主に以下の3つがある • PhpSpreadsheet • PHPWord • PHPPresentation https://github.com/PHPOffice
PHPOffice
8 スプレッドシートの新規作成はもちろん PhpSpreadsheet $spreadsheet = new Spreadsheet(); $activeWorksheet = $spreadsheet->getActiveSheet();
$activeWorksheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet); $writer->save('hello world.xlsx'); 参考:https://phpspreadsheet.readthedocs.io/en/latest/
9 スプレッドシートの読み込みもできる PhpSpreadsheet $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx"); $spreadsheet = $reader->load("05featuredemo.xlsx"); 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/
10 Wordファイルの新規作成 PHPWord $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection();
$section->addText('Hello World!'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld.docx'); 参考:https://phpoffice.github.io/PHPWord/usage/introduction.html
11 Wordファイルの読み込み PHPWord $reader = IOFactory::createReader('Word2007'); $reader->load(__DIR__ . '/sample.docx'); $reader
= IOFactory::createReader('MsDoc'); $reader->load(__DIR__ . '/sample.doc'); 参考:https://phpoffice.github.io/PHPWord/usage/readers.html
12 rtfやodtもサポートしている PHPWord $reader = IOFactory::createReader('RTF'); $reader->load(__DIR__ . '/sample.rtf'); $reader
= IOFactory::createReader('ODText'); $reader->load(__DIR__ . '/sample.odt'); 参考:https://phpoffice.github.io/PHPWord/usage/readers.html
13 書式の指定もできます PHPWord $fontStyleName = 'oneUserDefinedStyle'; $phpWord->addFontStyle( $fontStyleName, array('name' =>
'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true) ); 参考:https://phpoffice.github.io/PHPWord/usage/introduction.html
14 pptxファイルの新規作成 PHPPresentation $presentation = new PhpPresentation(); $writerPPTX = IOFactory::createWriter($presentation,
'PowerPoint2007'); $writerPPTX->save(__DIR__ . '/sample.pptx'); 参考:https://phpoffice.github.io/PHPPresentation/usage/presentation.html
15 pptファイルの読み込み PHPPresentation $reader = IOFactory::createReader('PowerPoint2007'); $reader->load(__DIR__ . '/sample.pptx'); $reader
= IOFactory::createReader('PowerPoint97'); $reader->load(__DIR__ . '/sample.ppt'); 参考:https://phpoffice.github.io/PHPPresentation/usage/readers.html
16 ⽂字や画像の位置指定が、ちょっと⾯倒? PHPPresentation $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo')
->setPath('./resources/logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); 参考:https://phpoffice.github.io/PHPPresentation/usage/presentation.html
© RAKUS Co., Ltd. 17 地球上の誰かがふと思った PHPでOfficeファイルを 扱えるようになったら どんな機能が実装できるだろうか……
18 について メール共有管理システム 15年連続シェアNo.1(2009〜2023)※ 歴史は更に⻑く、2001年4⽉に販売開始 Laravelは2011年リリースなので、10歳年上 ※出典:ITR「ITR Market View:メール∕Web∕SNSマーケティング市場2024」
メール処理市場:ベンダー別売上⾦額推移およびシェア(2009〜2023年度予測)
19 PHPOfficeで読み込んだファイルを⾃然⾔語処理で解析して 宛先を抽出する メール本⽂、添付ファイル名、添付ファイル内の宛先を 突き合わせて、違うものがあったら警告を出す機能 (write系の機能は使わず、readのみの利⽤) 誤添付防⽌機能を実装しました
20 実際の動作はこんな感じ
21 メモリ枯渇との戦いが多かった! 外部ライブラリなので、Officeファイルをどう解析して どういう形でメモリ内に保持するのかブラックボックス →メモリ枯渇のエラーハンドリングを⾏い 発⽣時に随時対応する以外思いつきませんでした 苦労したところ
22 PhpSpreadsheet、超メモリ容量⾷います > PhpSpreadsheet uses an average of about 1k
per cell (1.6k on 64-bit PHP) in your worksheets 1セルごとに1kbのメモリを使⽤する →10000(100x100)セルで10MB使⽤する計算 とりあえず、読み込み範囲の制限をかけて対策 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/ 苦労したところ
23 社内サーバリリース後、メモリ枯渇発⽣の報告が →添付ファイルを⾒ても、そんなにセル数多くない…… あれこれ検証した結果、vlookupが原因だと判明 ⾮表⽰シートに対し、⾮常に広い範囲で検索をかけていた →関数の解析‧展開で、メモリ枯渇が発⽣していた! 関数を展開しないオプションがあったので そちらを適⽤することで解決 苦労したところ
24 他にも、空⽩のセルは無視するオプションや 1シートずつ読み込んで、適宜メモリ解放を⾏う等の 対策を⾏った 公式ドキュメントには、PSR-16が実装されたライブラリを 利⽤し、代替オブジェクト(APCu、Redist、Memcache) を利⽤する⽅法も紹介されているが、⾮採⽤ 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/memory_saving/ 苦労したところ
25 PowerPointもメモリ枯渇発⽣するときがある 特にドキュメントに記載はないが、画像ファイルが多い プレゼンテーションはメモリ枯渇が発⽣しやすい こちらは特に対策が記載されていない…… →諦めて、メモリ枯渇時は解析を実⾏しないように 苦労したところ
26 当然メモリ枯渇以外にも苦労はあり…… 神Excelがこの世に存在するように、どんなフォーマットの 添付ファイルが添付されるか予測がし⾟い 宛先を抽出する際「様」や「御中」などの敬称を 参考にするのだが、別セルに書かれていたり、同じセル に書かれていたりと形式が曖昧 セル結合も使われていたりすると、さらに複雑に 😱 苦労したところ(時間があれば)
© RAKUS Co., Ltd. 27 (いろいろ課題はありますが……) PHPで楽しい Officeライフを!