phpdocxproを使用してテンプレートから.docxファイルを生成するプロジェクトがあります。テンプレートにデータを簡単に取り込むことができますが、ファイルをダウンロードしてMS Word 2010で開くと、内容に問題があるためファイルを開くことができないとプログラムが報告します。詳細は「ファイルが破損しています。開くことはできません」。Wordはドキュメントを修復できますが、そもそも破損してはならないという問題があります。
これが私がドキュメントを生成する方法です:
function generateUnitDesign(){
if($this->populateRecords()){
require_once dirname(__FILE__).'/phpdocx/classes/CreateDocx.inc';
$filename = 'UnitDesignTemplate-'.str_replace(' ', '', $this->rec->title);
//Create Document
$document = new CreateDocx();
$document->addTemplate(dirname(__FILE__).'/templates/unitdesigntemplate.docx');
// Fill in text fields
$document->addTemplateVariable('TITLE', $this->rec->title);
$document->addTemplateVariable('CHALLENGE', $this->rec->challenge, 'html');
$document->addTemplateVariable('HOOK', $this->rec->hook, 'html');
$document->addTemplateVariable('RESEARCH', $this->rec->research, 'html');
$document->addTemplateVariable('AUDIENCE', $this->rec->audience, 'html');
$document->addTemplateVariable('SUMMARY', $this->rec->project_brief, 'html');
$document->addTemplateVariable('RESOURCES', $this->rec->resources, 'html');
$document->addTemplateVariable('REQUIREMENTS', $this->rec->requirements, 'html');
$document->addTemplateVariable('SCAFFOLDING', $this->rec->scaffolding, 'html');
$document->createDocx($filename);
unset($document);
header("Content-Type: application/vnd.ms-word");
header("Content-Length: ".filesize($filename.'.docx'));
header('Content-Disposition: attachment; filename='.$filename.'.docx');
header('Content-Transfer-Encoding: binary');
ob_clean();
flush();
readfile($filename.'.docx');
unlink($filename.'.docx');
}
}
もともと、私はそれらのcreateDocxAndDownload()
関数を使用してファイルを取得しようとしましたが、サーバーに.docxファイルのコピーが残るため、理想的ではありませんでした。私は何かが足りないのですか?phpdocxの経験が豊富な人がいますか?
編集:
まあ、私はばかのように感じます。ファイルを出力するコードの部分に問題を絞り込んだ後、最終的にHEXエディターでファイルを開いたところ、ファイルが正常に出力された後、WebフロントエンドがHTMLの先頭を末尾に追加することが問題であることがわかりました。 「破損した」ファイルを作成するdocxファイル。unlink()
全体を修正した直後のこの1行:
exit;
ペッカ:新しい情報でこれに答えたいのなら、私はあなたの答えを受け入れます。