如何使用Laravel本地驱动程序在本地保存PhpWord生成的文件?

时间:2017-06-16 14:05:39

标签: php laravel-5 phpword

我正在处理一个报告生成器,但由于PHPWord不替换图像,我必须首先创建一个模板来添加客户端的徽标。所以我可以创建和下载这样的文件,但我无法保存到磁盘供以后使用(完整的一点)。 这是相关的代码。

    **
     * Creates the basic template for the reports
     * since PHPWord cannot replace placeholder images
     * this function must get the name and the logo of
     * the board and the clonable sections to insert
     * the actual report
     *
     * @return PHPWord object
     * @author msantana
     */
public function createTemplate()
    {
        //Getting the board logo
        $logo = $this->exam->board->logo->first()->source;
        //Preparing th generarl orintation of the main section
        //For this kind of reports it is always portrait
        $sectionStyle = [
            'orientation' =>  'portrait',
            'marginTop' => 600,
            'colsNum' => 1,
        ];
        //Preparing the "small" image to be
        //used in the header of all the document
        $imageStyle = [
           'height' => 80,
           'wrappingStyle' => 'square',
           'posHorizontalRel' => 'margin',
           'posVerticalRel' => 'line',
        ];
        //Create the section
        $section = $this->objPHPWord->addSection($sectionStyle);
        //Create an empty header
        $header = $section->addHeader();
        //Preparing a table to insert in the heading
        //to accomodate the image on the left and
        //the name of the board on the right
        $tableStyle = [
            'borderColor' => '006699',
            'borderSize' => 6,
            'cellMargin' => 50
        ];
        $firstRowStyle = array('bgColor' => '66BBFF');
        $this->objPHPWord->addTableStyle('headerTable', $tableStyle, $firstRowStyle);

        $headerTable = $header->addTable('headerTable');
        $headerTable->addRow();
        $cell = $headerTable->addCell();

        $source = "images/".$this->exam->board->logo()->first()->source ;
        $cell->addImage($source, $imageStyle);
        $cell = $headerTable->addCell();
        $cell->addText($this->exam->board->name, array('name' => 'Arial', 'size' => 14));
        $cell->addText("Examen de Certificación ".$this->exam->applicated_at->year, array('name' => 'Arial', 'size' => 14));
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($this->objPHPWord, 'Word2007');
        $fileName = 'Reporte_'.$this->exam->board_id.'_'.Carbon::now()->toAtomString().".docx";
        $objWriter->save(public_path($fileName));
        //File::put($fileName, $objWriter->save($fileName));
        //return public_path($fileName);
        //return $objWriter->save("php://output");
        //$document->save(public_path($fileName));
        //$writer = PHPWord_IOFactory::createWriter($objPHPWord, 'Word2007');;
        //$writer->save("testword.docx");
        return Response::download(public_path($fileName), 'testword.docx'); 
    }

1 个答案:

答案 0 :(得分:0)

愚蠢我你是对的我在错误的地方寻找它。 谢谢