barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.71k stars 967 forks source link

how to export dynamic blade view to pdf using a button? #800

Closed keyvanlotfi closed 3 years ago

keyvanlotfi commented 3 years ago

imagine we have a table which we can add rows and columns to it using javascript, and after some customization, we want to export the page to pdf

is this possible? and how

barryvdh commented 3 years ago

Store the result and render it as html, then create and PDF.

keyvanlotfi commented 3 years ago

hi bro where should i store it? i cant understand

On Thu, Jul 15, 2021 at 12:33 AM Barry vd. Heuvel @.***> wrote:

Store the result and render it as html, then create and PDF.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/barryvdh/laravel-dompdf/issues/800#issuecomment-880171611, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHBP5OKYFONUZ34MJPZ33VDTXXUPRANCNFSM5ALVE7MQ .

nikonachev commented 1 year ago

I want to use JavaScript in my Blade template to add or change dynamically the HTML but I don't know how to enable it in dompdf. I have a simple Blade:

<!DOCTYPE html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <style>
    .page-break {
        page-break-after: always;
    }
    *{ font-family: DejaVu Sans !important;}
    </style>
    <title>Hi</title>
</head>
<body>
  <h1 id="demo">Test PDF</h1>
  <script type="text/javascript">
    function insertHtml() {
      document.getElementById("demo").innerHTML = "I have changed!";
    }
    insertHtml()
</script>
</body>

I use Laravel Filament actions to generate the PDF.

Tables\Actions\Action::make('pdf') 
                    ->label('PDF')
                    ->color('success')
                    ->icon('heroicon-s-download')
                    ->action(function (Model $record) {
                        return response()->streamDownload(function () use ($record) {
                            echo Pdf::loadHTML(
                    Blade::render('pdf', ['record' => $record]), 'UTF-8'
                            )->stream();
                        }, 'Test-' . $record->number . '.pdf');
                    }), 

When I click on the action button the PDF is generated but the JavaScript is not take into account and the outputted text in the PDF is still "Test PDF" instead "Test PDF". Can you help me to solve this problem?

Thank you in advance.