2016-11-25 2 views
0

webappでdocFilesを生成しようとしています。まず、Markswindollsのjquery.wordexport.jsツールのようなFrontendでいくつかのツールを使用することを考えていました。しかし、ヘッダーやフッターの設定や整列のような機能はあまりないので、私は 'phpword'で作業を始めました。phpword生成されたドキュメントをフロントエンドに送信

私の問題は、docFileがサーバーに保存されていることです。フロントエンドにajaxを介してファイルを送信する可能性はありますか?そのため、ユーザーは「ダウンロードした.doc」ボタンを押した後にファイルを取得できますか?

他の推奨事項もあります。

jqueryの:

$('#word-button').on('click', function() { 
$.ajax({ 
    type: "POST", 
    url: "phpWORD/gendocx.php", 

    success: function (msg, string, jpXHR) { 
     console.log('AJAX SUCCESS'); 
    }, 
    complete : function(data, textStatus, jqXHR){ 
     console.log('AJAX COMPLETE'); 
    }, 
    error: function(xhr, desc, err) { 
     console.log(xhr); 
     console.log("Details: " + desc + "\nError:" + err); 
    } 
}); 
}) 

gendocx.php:

<?php 

require_once 'PHPWord.php'; 

$PHPWord = new PHPWord(); 

$section = $PHPWord->createSection(); 

// Create a new PHPWord Object 
$PHPWord = new PHPWord(); 

// Every element you want to append to the word document is placed in a section. So you need a section: 
$section = $PHPWord->createSection(); 

// After creating a section, you can append elements: 
$section->addText('Hello world!'); 

// You can directly style your text by giving the addText function an array: 
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); 

// If you often need the same style again you can create a user defined style to the word document 
// and give the addText function the name of the style: 
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232')); 
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle'); 


// You can also putthe appended element to local object an call functions like this: 
$myTextElement = $section->addText('Hello me!'); 

// At least write the document to webspace: 
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); 

$objWriter->save('helloWorld.docx'); 


?> 

答えて

0

これは私が断片化について申し訳ありませんPHPエクセル、のために使用するものですが、これは大規模なサービスファイルから次のとおりです。

$this->generateXls(); 
$this->writeToVariable(); 
if ($response instanceof Response) { 
    $this->setHeaders($response->getHeaders()); 
} 
$response->setContent($this->getXlsContent()); 

これは、ファイルの内容がファイルではなく変数に格納される場所です。

protected function writeToVariable() 
{ 
    ob_start(); 
    $this->getWriter()->save('php://output'); 
    $xlsContent = ob_get_clean(); 
    $this->setXlsContent($xlsContent); 
} 

そして、ちょうどヘッダはちょうどあなたの出力を生成し、何にそれらを追加する応答オブジェクトにではなく、あなたのケースに注入されているので、これはZF2で行われ

protected function setHeaders(Headers $headers) 
{ 
    $headers->addHeaderLine('Content-Type', $this->getFileMimeType()); 
    $headers->addHeaderLine('Content-Disposition', "attachment; filename=\"{$this->getFullFileName()}\""); 
    $headers->addHeaderLine('Accept-Ranges', 'bytes'); 
    $headers->addHeaderLine('Content-Length', strlen($this->getXlsContent())); 
} 

あなたの応答を返す前にヘッダを設定します。

ああ、ところで、ExcelのMIMEタイプは、次のようになります。

$this->setFileMimeType('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 

だから、このフォーマットであり、あなたが出力したいMSワード形式のものを探してみてください。単純化された非OOのapprochについては

:高速応答のための

$writer = \PHPExcel_IOFactory::createWriter($this->getWorkbook(), $type); 
//<---GENERATE YOUR DOCUMENT HERE 
ob_start(); 
$writer->save('php://output'); 
$document = ob_get_clean(); 
//<---SET OUTPUT HEADERS LIKE FOR ANY OTHER FILE TYPE HERE 
echo $document; 
die; 
+0

感謝。私はこれに新しいので、あなたがしたようにどこから始めたり解決したらいいかわかりません。 – moody

+0

こんにちは、あなたの目標は、ファイルではなくPHP出力ストリームにドキュメントを保存し、そのストリームを変数に取り込むことです。私は単純化されたバージョンで答えを更新しました – Auris

関連する問題