画像ファイルの挿入

Demo

sample.php

PHP

PHPindex.phpdownload

<?php

	require_once 'vendor/autoload.php';			//ライブラリ読み込み

	$phpWord = new \PhpOffice\PhpWord\PhpWord();//PHPWordインスタンスを作成

	$section = $phpWord->addSection();			//セクションを追加

	$fileName = "sample.docx";						//ファイル名を設定

	/* 画像の挿入 */
	$section->addImage('logo1.png');

	/* wordファイルのダウンロード */
	header("Content-Description: File Transfer");

	header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');	//Wordファイル( .docx OFFICE 2007以降)

	header('Content-Disposition: attachment; filename="'.$fileName.'"');	//ダウンロード時のファイル名をセット

	header('Cache-Control: no-store, max-age=0');	//新しいリソースのキャッシュを防止、キャッシュのクリア

	ob_end_clean();	//バッファ消去

	$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');

	$writer->save('php://output');
?>
© 2025 wayday