Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

EdsonOrdaz's avatar

download DomPDF file through two returns

Hello, I am trying to use the Barryvdh library to download PDF, if I do it in my controller it works perfectly, but if I try to bring it from a further level (a second file) it does not work.

My route points to this function (for example)

    public function estadoResultados(Request $request){
        return EstadoResultadosPdf::class;
    }

my second file where I want to create the PDF is this:

<?php

namespace App\Exports\Polizas;

use Barryvdh\DomPDF\Facade\Pdf;

class EstadoResultadosPdf {
    public function __invoke()
    {
        $pdf = Pdf::loadView('reportes/polizas/EstadoResultados');
        $pdf->set_paper('letter', 'landscape');
        $pdf->output();
        $pdf->getDomPDF();
        return $pdf->download('EstadoResultados.pdf');
    }
}

It downloads the PDF but it is not allowed to open it because it tells me "damaged file", if I transfer the information from the second file to the first it downloads it and shows it without problems

    public function estadoResultados(Request $request){
        $pdf = Pdf::loadView('reportes/polizas/EstadoResultados');
        $pdf->set_paper('letter', 'landscape');
        $pdf->output();
        $pdf->getDomPDF();
        return $pdf->download('EstadoResultados.pdf');
        // return EstadoResultadosPdf::class;
    }

but unfortunately I need to divide it, since in the first file I only have very limited information and in the second that creates the PDF is where I need to carry the logic specifically for that PDF. thank you thank you

0 likes
0 replies

Please or to participate in this conversation.