PHPWord如何return一个单词响应对象
How PHPWord return a word response object
我的 php symfony 文档下载代码内容,我正在下载 PDF 和 docx,我可以获得 PDF 响应对象作为 return 但不能 return docx 响应对象。这是我的 docx 下载代码,
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;\"></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');
}
Word 下载功能有效,但是,我想 return 一个用于我的 REST API 的单词响应对象,因为我的上述代码被 return 编辑为空。我对此进行了很多搜索,但找不到如何 return 一个 php/word 响应对象。
如果我对你的问题的理解正确,你想 return 一个带有文件的 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;\"></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;
}
我的 php symfony 文档下载代码内容,我正在下载 PDF 和 docx,我可以获得 PDF 响应对象作为 return 但不能 return docx 响应对象。这是我的 docx 下载代码,
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;\"></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');
}
Word 下载功能有效,但是,我想 return 一个用于我的 REST API 的单词响应对象,因为我的上述代码被 return 编辑为空。我对此进行了很多搜索,但找不到如何 return 一个 php/word 响应对象。
如果我对你的问题的理解正确,你想 return 一个带有文件的 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;\"></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;
}