PHPWord如何返回word响应对象
我的php symfony文档下载代码内容,我正在下载PDF和docx,我可以得到PDF响应对象作为返回,但不能返回docx响应对象。这里是我的docx下载代码PHPWord如何返回word响应对象,php,symfony,phpword,Php,Symfony,Phpword,我的php symfony文档下载代码内容,我正在下载PDF和docx,我可以得到PDF响应对象作为返回,但不能返回docx响应对象。这里是我的docx下载代码 public function getDocxBuilder() { if (is_null($this->docxBuilder)) { $this->docxBuilder = new \PhpOffice\PhpWord\PhpWord(); } return $this-&g
public function getDocxBuilder()
{
if (is_null($this->docxBuilder)) {
$this->docxBuilder = new \PhpOffice\PhpWord\PhpWord();
}
return $this->docxBuilder;
}
public function setMarginArray($parameterArray, $fileType)
{
$this->name = isset($parameterArray['name']) ? $parameterArray['name'] : 'document';
$pageSize = isset($parameterArray['pageSize']) ? $parameterArray['pageSize'] : 'A4';
$marginLeft = isset($parameterArray['margin_left']) ? $parameterArray['margin_left'] : ($fileType == "PDF" ? '10mm' : 600);
$marginRight = isset($parameterArray['margin_right']) ? $parameterArray['margin_right'] : ($fileType == "PDF" ? '10mm' : 600);
$marginTop = isset($parameterArray['margin_top']) ? $parameterArray['margin_top'] : ($fileType == "PDF" ? '10mm' : 600);
$marginBottom = isset($parameterArray['margin_bottom']) ? $parameterArray['margin_bottom'] : ($fileType == "PDF" ? '10mm' : 600);
return [
'pageSize' => $pageSize,
'name' => $this->name,
'marginLeft' => $marginLeft,
'marginRight' => $marginRight,
'marginTop' => $marginTop,
'marginBottom' => $marginBottom
];
}
public function exportDocument($parameterArray, $content)
{
$pageOption = $this->setMarginArray($parameterArray, $fileType = 'docx');
$this->getDocx($content, $pageOption);
}
protected function getDocx($content, $sectionStyle)
{
$section = $this->getDocxBuilder()->addSection($sectionStyle);
$content = preg_replace("/<table\s(.+?)>(.+?)<\/table>/is", "<table style=\"border: 6px #000000 solid;\">$2</table>", $content);
$content = str_replace("<p><div style=' page-break-after:always !important;'></div></p>", "<pagebreak></pagebreak>", $content);
$content = str_replace(" ", " ", $content);
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename="test.docx"');
header('Cache-Control: max-age=0');
$objWriter = PHPWord_IOFactory::createWriter($this->docxBuilder, 'Word2007');
$objWriter->save('php://output');
}
公共函数getDocxBuilder()
{
如果(为空($this->docxBuilder)){
$this->docxBuilder=new\PhpOffice\PhpWord\PhpWord();
}
返回$this->docxBuilder;
}
公共函数setMarginArray($parameterArray,$fileType)
{
$this->name=isset($parameterArray['name'])?$parameterArray['name']:'document';
$pageSize=isset($parameterArray['pageSize'])?$parameterArray['pageSize']:'A4';
$marginLeft=isset($parameterArray['margin_left'])?$parameterArray['margin_left']:($fileType==“PDF”?'10mm':600);
$marginRight=isset($parameterArray['margin_right'])?$parameterArray['margin_right']:($fileType==“PDF”?'10mm':600);
$marginTop=isset($parameterary['margin\u top'])?$parameterary['margin\u top']:($fileType==“PDF”?“10mm”:600);
$marginBottom=isset($PARAMETARRAY['margin\U bottom'])?$PARAMETARRAY['margin\U bottom']:($fileType==“PDF”?“10mm”:600);
返回[
“pageSize”=>$pageSize,
“名称”=>$this->name,
'marginLeft'=>$marginLeft,
“marginRight”=>$marginRight,
“marginTop”=>$marginTop,
“marginBottom”=>$marginBottom
];
}
公共函数exportDocument($parameterArray,$content)
{
$pageOption=$this->setMarginArray($parameterArray,$fileType='docx');
$this->getDocx($content,$pageOption);
}
受保护的函数getDocx($content,$sectionStyle)
{
$section=$this->getDocxBuilder()->addSection($sectionStyle);
$content=preg_replace(“/(.+?)/is”、“$2”、$content);
$content=str_replace(“”,“”,$content);
$content=str_replace(“,”,$content);
标题('Content-Type:application/vnd.ms-word');
标题('Content-Disposition:attachment;filename=“test.docx”');
标头('Cache-Control:max age=0');
$objWriter=PHPWord_IOFactory::createWriter($this->docxBuilder,'Word2007');
$objWriter->save('php://output');
}
Word下载功能正在工作,但是,我想为我的RESTAPI返回一个Word响应对象,因为我上面的代码返回null。我对此进行了大量搜索,但找不到如何返回php/word响应对象。如果我正确理解了您的问题,您希望随文件返回Symfony响应。流式响应最适合这种情况
/**
* @Route("/", name="default")
*/
public function index(): Response
{
return $this->exportDocument([], 'test');
}
public function getDocxBuilder()
{
if (is_null($this->docxBuilder)) {
$this->docxBuilder = new \PhpOffice\PhpWord\PhpWord();
}
return $this->docxBuilder;
}
public function setMarginArray($parameterArray, $fileType)
{
$this->name = isset($parameterArray['name']) ? $parameterArray['name'] : 'document';
$pageSize = isset($parameterArray['pageSize']) ? $parameterArray['pageSize'] : 'A4';
$marginLeft = isset($parameterArray['margin_left']) ? $parameterArray['margin_left'] : ($fileType == "PDF" ? '10mm' : 600);
$marginRight = isset($parameterArray['margin_right']) ? $parameterArray['margin_right'] : ($fileType == "PDF" ? '10mm' : 600);
$marginTop = isset($parameterArray['margin_top']) ? $parameterArray['margin_top'] : ($fileType == "PDF" ? '10mm' : 600);
$marginBottom = isset($parameterArray['margin_bottom']) ? $parameterArray['margin_bottom'] : ($fileType == "PDF" ? '10mm' : 600);
return [
'pageSize' => $pageSize,
'name' => $this->name,
'marginLeft' => $marginLeft,
'marginRight' => $marginRight,
'marginTop' => $marginTop,
'marginBottom' => $marginBottom
];
}
public function exportDocument($parameterArray, $content)
{
$pageOption = $this->setMarginArray($parameterArray, $fileType = 'docx');
return $this->getDocx($content, $pageOption);
}
protected function getDocx($content, $sectionStyle)
{
$section = $this->getDocxBuilder()->addSection($sectionStyle);
$content = preg_replace("/<table\s(.+?)>(.+?)<\/table>/is",
"<table style=\"border: 6px #000000 solid;\">$2</table>", $content);
$content = str_replace("<p><div style=' page-break-after:always !important;'></div></p>",
"<pagebreak></pagebreak>", $content);
$content = str_replace(" ", " ", $content);
// The latest version of PHPWord is using IOFactory from the PhpOffice\PhpWord namespace
// instead of the PHPWord_IOFactory, you might want to change it back.
$objWriter = IOFactory::createWriter($this->docxBuilder, 'Word2007');
$response = new StreamedResponse();
// Here we are using the $objWriter in a Callback method.
// This way we can use php://output within a Symfony Repsonse (StreamedResponse)
$response->setCallback(function () use ($objWriter) {
$objWriter->save('php://output');
});
$response->headers->set('Content-Type', 'application/vnd.ms-word');
$response->headers->set('Cache-Control', 'max-age=0');
$disposition = HeaderUtils::makeDisposition(
HeaderUtils::DISPOSITION_ATTACHMENT,
'test.docx'
);
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
/**
*@Route(“/”,name=“默认”)
*/
公共函数索引():响应
{
返回$this->exportDocument([],'test');
}
公共函数getDocxBuilder()
{
如果(为空($this->docxBuilder)){
$this->docxBuilder=new\PhpOffice\PhpWord\PhpWord();
}
返回$this->docxBuilder;
}
公共函数setMarginArray($parameterArray,$fileType)
{
$this->name=isset($parameterArray['name'])?$parameterArray['name']:'document';
$pageSize=isset($parameterArray['pageSize'])?$parameterArray['pageSize']:'A4';
$marginLeft=isset($parameterArray['margin_left'])?$parameterArray['margin_left']:($fileType==“PDF”?'10mm':600);
$marginRight=isset($parameterArray['margin_right'])?$parameterArray['margin_right']:($fileType==“PDF”?'10mm':600);
$marginTop=isset($parameterary['margin\u top'])?$parameterary['margin\u top']:($fileType==“PDF”?“10mm”:600);
$marginBottom=isset($PARAMETARRAY['margin\U bottom'])?$PARAMETARRAY['margin\U bottom']:($fileType==“PDF”?“10mm”:600);
返回[
“pageSize”=>$pageSize,
“名称”=>$this->name,
'marginLeft'=>$marginLeft,
“marginRight”=>$marginRight,
“marginTop”=>$marginTop,
“marginBottom”=>$marginBottom
];
}
公共函数exportDocument($parameterArray,$content)
{
$pageOption=$this->setMarginArray($parameterArray,$fileType='docx');
返回$this->getDocx($content,$pageOption);
}
受保护的函数getDocx($content,$sectionStyle)
{
$section=$this->getDocxBuilder()->addSection($sectionStyle);
$content=preg_replace(“/(.+?)/is”,
“$2”,$content);
$content=str_replace(“”,
“,$content);
$content=str_replace(“,”,$content);
//PHPWord的最新版本正在使用PhpOffice\PHPWord命名空间中的IOFactory
//您可能希望将其更改回PHPWord_IOFactory,而不是PHPWord_IOFactory。
$objWriter=IOFactory::createWriter($this->docxBuilder,'Word2007');
$response=新的流响应();
//这里我们在回调方法中使用$objWriter。
//这样我们就可以使用php://output 在Symfony Repsonse内(流响应)
$response->setCallback(函数()使用($objWriter){
$objWriter->save('php://output');
});
$response->headers->set('Content-Type','application/vnd.ms-word');
$response->headers->set('Cache-Control','max age=0');
$disposition=HeaderUtils::makeDisposition(
HeaderUtils::处置(附件),
“test.docx”
);
$response->headers->set('Content-Disposition',$Disposition);
返回$response;
}
这是什么版本的PHPWord?如何在我的回复中检查php/word版本#content:null;我使用Html::addHtml($section,$content,false,false);