2

Refresh page after download PDF in custom module magento 2. When the download is complete, the page is not f5. Please help me.

My function

public function createPDF(\Magento\Framework\Event\Observer $observer, $product, $printedAttribute) {
    $nameProduct = $product->getName();
    $attribute = $product->getData($printedAttribute);
    $qty = $observer->getQty();
    $orderNumber = $observer->getOrdernumber();
    $deliOrderNumber = $observer->getDeliordernumber();
    $pdf = new \Zend_Pdf();
    $page = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
    $pdf->pages[] = $page;
    $style = new \Zend_Pdf_Style();
    $style->setLineColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
    $font = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_TIMES);
    $style->setFont($font, 15);
    $page->setStyle($style);
    $x = 30;
    $this->y = 850 - 100; //print table row from page top – 100px
    //Draw table header row’s
    $style->setFont($font, 16);
    $page->setStyle($style);
    $page->drawRectangle(30, $this->y + 10, $page->getWidth() - 30, $this->y + 70, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);
    $style->setFont($font, 15);
    $page->setStyle($style);
    $page->drawText(__("Cutomer Details"), $x + 5, $this->y + 50, 'UTF-8');
    $style->setFont($font, 11);
    $page->setStyle($style);
    $page->drawText(__("Name : %1", "John Smith"), $x + 5, $this->y + 33, 'UTF-8');
    $page->drawText(__("Email : %1", "[email protected]"), $x + 5, $this->y + 16, 'UTF-8');

    $style->setFont($font, 12);
    $page->setStyle($style);
    $page->drawText(__("PRODUCT_NAME"), $x + 30, $this->y - 10, 'UTF-8');
    $page->drawText(__("COUNT"), $x + 150, $this->y - 10, 'UTF-8');
    $page->drawText(__("ORDER_NUMBER"), $x + 250, $this->y - 10, 'UTF-8');
    $page->drawText(__("DELIVERY_NOTE_ID"), $x + 380, $this->y - 10, 'UTF-8');
    $page->drawText(__($printedAttribute), $x + 30, $this->y - 80, 'UTF-8');

    $style->setFont($font, 10);
    $page->setStyle($style);
    $page->drawText($nameProduct, $x + 30, $this->y - 30, 'UTF-8');
    $page->drawText($qty, $x + 150, $this->y - 30, 'UTF-8');
    $page->drawText($orderNumber, $x + 250, $this->y - 30, 'UTF-8');
    $page->drawText($deliOrderNumber, $x + 380, $this->y - 30, 'UTF-8');
    $page->drawText($attribute, $x + 70, $this->y - 80, 'UTF-8');
    $page->drawRectangle(30, $this->y - 62, $page->getWidth() - 30, $this->y + 10, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);
    $page->drawRectangle(30, $this->y - 62, $page->getWidth() - 30, $this->y - 100, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);
    $style->setFont($font, 15);
    $page->setStyle($style);
    $page->drawText(__("Text"), ($page->getWidth() / 2) - 50, $this->y - 200);

    $fileName = 'name.pdf';

    $this->fileFactory->create(
            $fileName, $pdf->render(), \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR, // this pdf will be saved in var directory with the name example.pdf
            'application/pdf'
    );
    return $this->_redirect('*/*/index');
}
3
  • 1
    it is using header http request download. So, you cant make it redirect in that case
    – the light
    Commented May 2, 2019 at 4:06
  • Is there any way to solve it?
    – Brian NN
    Commented May 3, 2019 at 2:59
  • Hi, did you find a solution?
    – SebastianT
    Commented Sep 2, 2022 at 12:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.