0

I'm trying to download the current html page as a file in symfony :

My twig :

<a href="{{ path('polytech_skills_evaluations_export', {'idssoccasion': ssoccasion.id, 'idevalue':user.id}) }}" >Exporter </a>

My Controller

 $html= $this->renderView('PolytechSkillsBundle:Evaluation/Evaluation:type2.html.twig', array(
        'form' => $form->createView(),
        'niveaux' => $niveaux,
        'date' => $dateCourante,
        'evalue' => $evalue,
        'ssoccasion' => $ssoccasion,
        'lasteval' => $lastEval,
        'lastevalforocc' => $lastEvalForOccasion,
        'objectifs' => $objectifs
    ));

    $response = new Response();
    // Set headers
    $response->headers->set('Cache-Control', 'private');
    $response->headers->set('Content-type', mime_content_type($html));
    $response->headers->set('Content-Disposition', 'attachment; filename="' . basename($html) . '";');
    $response->headers->set('Content-length', filesize($html));
    // Send headers before outputting anything
    $response->sendHeaders();
    $response->setContent(file_get_contents($html));

The error I get :

Warning: mime_content_type failed to open stream: Invalid argument

500 Internal Server Error - ContextErrorException

Amira Khalifa
  • 43
  • 1
  • 10

1 Answers1

0

The best solution in this case is to save (even temporarily the page) then force the download :

file_put_contents('page.html', $content);

You can then download the file with a code like this :

// Create response et hydrate headers
$response = new Response();
$response->setContent(file_get_contents('page.html'));
$response->headers->set('Content-Type', 'text/html');
$response->headers->set('Content-disposition', 'filename=page.html');

// Return response
return $response;

Polytech school :) ?

Thanks for your feedback