<?php


namespace Magetop\Advancepreview\Controller\Product;

use Magento\Framework\App\Action;
use Magento\Framework\App\Filesystem\DirectoryList;
class ExportTest  extends Action\Action
{
    protected $httpContext;
    protected $filesystem;
    protected $file;
    protected $fileFactory;
    protected $bookTicket;
    protected $x;
    protected $y;
    protected $company_name;
    protected $item_name;
    protected $ticket_price;
    protected $ticket_time;
    /**
     * @var \Magento\Framework\Stdlib\StringUtils
     */
    protected $stringUtils;
    protected $urlInterface;
    protected $priceHelper;
    protected $session;

    protected $collectionFactory;

    function __construct(
        Action\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
        \Magento\Framework\Stdlib\StringUtils $stringUtils,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magento\Customer\Model\Session $session
    )
    {
        parent::__construct($context);
        $this->httpContext = $httpContext;
        $this->filesystem = $filesystem;
        $this->file = $file;
        $this->fileFactory = $fileFactory;
        $this->stringUtils = $stringUtils;
        $this->urlInterface = $urlInterface;
        $this->priceHelper = $priceHelper;
        $this->session = $session;
    }

    function execute()
    {
        try {
            // x 595
            // Y 842
            $pdf = new \Zend_Pdf();
            $this->setBookTicket($pdf);
            /** @var \Zend_Pdf_Page $page*/
            $page = $this->getBookTicket()->pages[0];
            $fileName = 'result-'.date('Y-m-d-H-i-s').'.pdf';
            $this->fileFactory->create(
                $fileName,
                $pdf->render(),
                DirectoryList::VAR_DIR,
                'application/pdf'
            );
//                $this->messageManager->addSuccessMessage('Download Certificate success');
        }
        catch (\Exception $exception) {
            $this->messageManager->addErrorMessage('Error, Can not print ticket'.$exception->getMessage());
        }
    }
    protected function setBookTicket(\Zend_Pdf $pdf,$isNew = true) {
        if($isNew)
            $pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
        $this->bookTicket = $pdf;
    }
    protected function getBookTicket() {
        return $this->bookTicket;
    }
}
