Skip to content

Issue in creating pdf using fileFactory->create() in magento 2

I am pulling files from 3rd party server using apis and generating pdf files.

My code is some thing like this. I Will update the sample code to explain my approach.

foreach ($fileData['dataDocuments']['items'] as $document) {
    //API Call
    $urlToGetData = $this->api->getDocumentById($identifier, $docId);
    $data = $this->api->getDataFromApi($urlToGetData);
    $decodeData = json_decode($data, true);
    $labelContent[] = base64_decode((string)$imageString, true); //you can add more image strings.

    //use MagentoShippingModelShippingLabelGenerator;
    $outputPdf = $this->labelGenarator->combineLabelsPdf($labelContent);

    try{
        $this->fileFactory->create(
            $docId,
            $outputPdf->render(),
            MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR,
            'application/pdf'
        );
     }catch(Exception $e){
        $this->logger->debug($e->getMessage());
     }
}

While running through the loop ( I have pulling like 6 files from server using api) only first two files are generating and getting an error on $this->fileFactory->create()

I am not getting any exception also, Any hint to generate all files together here. If i run this for each and every file individually i can generate 6 files without no any issue. But my requirement is download all files at once. Any guess or suggestion to fix this issue highly appreciated.