chelbeh
9/16/2016 - 11:41 AM

Создать и скачать phpword

Создать и скачать phpword

<?php
/**
 * Copyright (c) 2016.
 */
// CHELBEH FILE
class warehousePurchaseSaveController extends waJsonController
{

    public function execute()
    {
        wa('shop');
        $date = waRequest::request('date', null, waRequest::TYPE_STRING);
        $date_format = waDateTime::date('d.m.Y', $date);
        $purchase_model = new shopWarehousePurchaseModel();
        $products = $purchase_model->select('*')->where("`date` = '$date' AND bought_count < count")->fetchAll();
        // Create a new PHPWord Object
        $PHPWord = new PHPWord();
        $PHPWord->setDefaultFontName('Times New Roman');
        $PHPWord->setDefaultFontSize(14);
        $meta = $PHPWord->getProperties();
        $meta->setCreator('Вадим');
        $meta->setTitle('Закупка' . $date_format);
        $meta->setDescription('Список закупки');
        $meta->setCreated(time()); // Дата и время создания документа
        $meta->setModified(time()); //Дата и время последнего изменения документа
        $meta->setSubject('Закупка');
        $section = $PHPWord->createSection();
        $index = 1;

        foreach ($products as $key => $product) {
            $product['full_name'] = htmlentities($product['full_name']);
            $section->addLink($product['url'], $index . '. ' . $product['full_name'] . ' ' . $product['count'] . ' шт.');
            if ($product['comment'] != '') {
                $section->addText('(комментарий: ' . $product['comment'] . ')', array('color' => '9b9b9b', 'size' => 12));
            }
            $index++;
        }


        $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

        $filename = $date_format . '.docx';

        $objWriter->save($filename);

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        flush();
        readfile($filename);
        unlink($filename); // deletes the temporary file
        exit;
    }
}