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

132qquick12's avatar

laravel ajax-pdf issue

I have a problem that I've been struggling with for days but still can't solve and I apologize in advance if I seem to be hypocritical.

I am implementing a post method with Ajax and I have to get a PDF output after this post process.

After post method i get certain data and print this data in PDF.

When I don't run these codes with Ajax, there is no problem, I can view the PDF in any format I want. But when I work it with Ajax it doesn't work just refreshing the page on success. What should I do exactly? Where am I missing?

And this is my codes Route:

Route::post('/label',[HomeController::class,'createLabel'])->name('create-label')->middleware('auth');

Controller:

$dompdf = new PDF();
            $dompdf = PDF::loadView('myPDF', $data);
            $customPaper = array(0, 0, 288.64, 419.53);
            $dompdf->setPaper($customPaper, 'portrait');
            $content = $dompdf->output();
            file_put_contents('label2.pdf', $content);

            $pdf = new \Clegginabox\PDFMerger\PDFMerger;
            $data = base64_decode($r->labelData);
            file_put_contents('label1.pdf',$data);
            $pdf->addPDF('label1.pdf', 'all');
            $pdf->addPDF('label2.pdf', 'all');
            $pdf->merge('browser', 'LABEL'.$order->orderNumber.'.pdf', 'P');

Ajax:

$.ajax({
            url:"{{route('create-label')}}",
            type:"post",
            data:{
              orderId:orderId,
              _token:'{{ csrf_token() }}'
            },
            success:function(response){
              console.log(response);
            },
            error: function(response) { 
              console.log(response);
              });
            }  
        });
0 likes
3 replies
jlrdw's avatar

You could load the pdf in a modal in an object, or use the javascript location to load it.

1 like
132qquick12's avatar

@jlrdw I'm not very experienced with javascript, can you show me a more detailed guide on what should I do?

Please or to participate in this conversation.