Forum


Replies: 4   Views: 444
Return a docx as a stream
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by khalid-s  · 02-02-2024 - 10:40

Hey again,

I appologize in advance but i've been looking at all the examples and the other related topics from the forum but i still dont understand how to stream a docx from an api and retrieve it from another app. Would it be possible to have an actual fully working example ideally with symfony.

here is what i got so far:

 

The API endpoint

use Phpdocx\Create\CreateDocx;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Annotation\Route;

class AppController extends AbstractController
{
    #[Route('/', name: 'app_app')]
    public function index(): StreamedResponse
    {
        $doc = new CreateDocx();
        CreateDocx::$streamMode = true;

        $doc->addText('This is a test');
        $doc->embedHTML('<p style="font-size: 30px;">New paragraph</p>');

        return new StreamedResponse(function () use ($doc) {
            $doc->createDocx('output1');
        });
    }
}

 

and the front.

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class AppController extends AbstractController
{
    #[Route('/', name: 'app_home')]
    public function index(HttpClientInterface $http): Response
    {
        $response = $http->request(Request::METHOD_GET, 'https://dev.doc.ey');
        $doc = $response->getContent();

        dd($doc);

        return $this->render('app/index.html.twig', [
            'controller_name' => 'AppController',
        ]);
    }
}