在我的 php symphony 项目中,我想将 html 模板下载为 pdf 和 docx。
docx 使用 - PhpOffice\PhpWord\PhpWord;
pdf 使用 - PDFBuilder;
步骤
了解流程的 1&2 个步骤,我的问题与 3&4 相关
我有
在子类中有3个函数
DocxExporter -> getDocxBuilder、getDocx、exportDocument
PDFExporter -> getPdfBuilder、getPDF、exportDocument
在DocxExporter中;
const DEFAULT_HEADER = 'application/octet-stream';
protected function getDocx($content, array $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);
Html::addHtml($section, $content, false, false);
header('Content-Type: ' . self::DEFAULT_HEADER);
header('Content-Disposition: attachment;filename="' . $this->getExportFileName() . '.docx"');
$objWriter = IOFactory::createWriter($this->docxBuilder, 'Word2007');
$objWriter->save('php://output');}
在 PDFExporter 中;
const DEFAULT_HEADER = 'application/pdf';
protected function getPDF($content, $pageParamArray): AttachmentResponse {
$this->pdfBuilder = $this->getPdfBuilder();
$this->pdfBuilder->setPageOptions($pageParamArray);
$pdf = $this->pdfBuilder->createPDFData(null, null, $content);
$response = new AttachmentResponse(['data' => $pdf]);
$response->setContentType(self::DEFAULT_HEADER);
$response->setAttachmentName(parent::getExportFileName() . '.pdf');
return $response; }
我试图让 getDocx 函数也像 PDFExporterclass 一样返回一个 AttachmentResponse。但无法确定获得相同响应的正确方法。现在在端点我得到一个附件响应和 null 分别为 pdf 和 docx。如何更改 docx 函数以返回附件响应。