0

I have added a function where in when the user select multiple order then trigger the Download PDF button I added it will download PDF's with X number of PDF based from the number of selected orders. So basically when I select 3 PDF it should generate or download 3 PDF. But on my function it only download one pdf. How can I tell Magento 2 to download 3 pdf or more based from the number of selected items.

Below is my sample code

public function printPDF()
{
    $pdf = new \Zend_Pdf();
    $pdf->pages[] = $pdf->newPage(\Zend_Pdf_Page::SIZE_A4);
    $page = $pdf->pages[0]; // this will get reference to the first 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,13);
    $page->setStyle($style);
    $width = $page->getWidth();
    $hight = $page->getHeight();
    $x = 30;
    $pageTopalign = 850; 
    $this->y = 850 - 100; 

    $style->setFont($font,14);
    $page->setStyle($style);
    $page->drawRectangle(30, $this->y + 10, $page->getWidth()-30, $this->y +70, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);

    $style->setFont($font,13);
    $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", "Test Meetanshi"), $x + 5, $this->y+33, 'UTF-8');
    $page->drawText(__("Email : %1","[email protected]"), $x + 5, $this->y+16, 'UTF-8');

    $style->setFont($font,11);
    $page->setStyle($style);

    $page->drawText(__("PRODUCT NAME"), $x + 60, $this->y-10, 'UTF-8');
    $page->drawText(__("PRODUCT PRICE"), $x + 200, $this->y-10, 'UTF-8');
    $page->drawText(__("QTY"), $x + 310, $this->y-10, 'UTF-8');
    $page->drawText(__("SUB TOTAL"), $x + 440, $this->y-10, 'UTF-8');

    $style->setFont($font,10);
    $page->setStyle($style);
    $add = 9;

    $page->drawText("$12.00", $x + 210, $this->y-30, 'UTF-8');
    $page->drawText(10, $x + 330, $this->y-30, 'UTF-8');
    $page->drawText("$120.00", $x + 470, $this->y-30, 'UTF-8');
    $pro = "TEST product";
    $page->drawText($pro, $x + 65, $this->y-30, '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(__("Total : %1", "$50.00"), $x + 435, $this->y-85, 'UTF-8');

    $style->setFont($font,10);
    $page->setStyle($style);
    $page->drawText(__("Test Footer example"), ($page->getWidth()/2)-50, $this->y-200);

    $fileName = 'meetanshi.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 meetanshi.pdf
       'application/pdf'
    );
}

So in my execute() function I added this call

public function execute()
{
        $this->printPDF();
        $this->printPDF();
        $this->printPDF();
}

But it only download 1 PDF. How can I tell it to download multiple times?

1 Answer 1

0
Use below code to download multiple PDF at once.

public function execute()
{
    $labelsContent = [];
    $labelsContent[] = "pdf1";
    $labelsContent[] = "pdf2";
    $labelsContent[] = "pdf3";
    $outputPdf = new \Zend_Pdf();
    foreach ($labelsContent as $content) {
        if (stripos($content, '%PDF-') !== false) {
                $pdfLabel = \Zend_Pdf::parse($content);
                foreach ($pdfLabel->pages as $page) {
                        $outputPdf->pages[] = clone $page;
                }
        } else {
                $page = $this->createPdfPage($content);
                if ($page) {
                        $outputPdf->pages[] = $page;
                }
        }
    }
    $fileName = 'meetanshi.pdf';
    $this->fileFactory->create(
       $fileName,
       $outputPdf->render(),
       \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR,
       'application/pdf'
    );
}

public function createPdfPage() 
{
  $pdf = new \Zend_Pdf(); //Create new PDF file
  $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,13);
  $page->setStyle($style);
  $width = $page->getWidth();
  $hight = $page->getHeight();
  $x = 30;
  $pageTopalign = 850;
  $this->y = 850 - 100;

  $style->setFont($font,14);
  $page->setStyle($style);
  $page->drawRectangle(30, $this->y + 10, $page->getWidth()-30, $this->y +70, \Zend_Pdf_Page::SHAPE_DRAW_STROKE);

  $style->setFont($font,13);
  $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", "Test Meetanshi"), $x + 5, $this->y+33, 'UTF-8');
  $page->drawText(__("Email : %1","[email protected]"), $x + 5, $this->y+16, 'UTF-8');

  $style->setFont($font,11);
  $page->setStyle($style);

  $page->drawText(__("PRODUCT NAME"), $x + 60, $this->y-10, 'UTF-8');
  $page->drawText(__("PRODUCT PRICE"), $x + 200, $this->y-10, 'UTF-8');
  $page->drawText(__("QTY"), $x + 310, $this->y-10, 'UTF-8');
  $page->drawText(__("SUB TOTAL"), $x + 440, $this->y-10, 'UTF-8');

  $style->setFont($font,10);
  $page->setStyle($style);
  $add = 9;

  $page->drawText("$12.00", $x + 210, $this->y-30, 'UTF-8');
  $page->drawText(10, $x + 330, $this->y-30, 'UTF-8');
  $page->drawText("$120.00", $x + 470, $this->y-30, 'UTF-8');
  $pro = "TEST product";
  $page->drawText($pro, $x + 65, $this->y-30, '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(__("Total : %1", "$50.00"), $x + 435, $this->y-85, 'UTF-8');

  $style->setFont($font,10);
  $page->setStyle($style);
  $page->drawText(__("Test Footer example"), ($page->getWidth()/2)-50, $this->y-200);
    return $page;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.