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

davy_yg's avatar
Level 27

Export Table to PDF

What is recommended packge for laravel to export table to PDF ?

Will this be good? https://packagist.org/packages/dompdf/dompdf

I also do not know the code to create the table:

This is my guess, is this true?

	// reference the Dompdf namespace
	use Dompdf\Dompdf;

	// instantiate and use the dompdf class
	$dompdf = new Dompdf();
	$dompdf->loadHtml('<table>');
	$dompdf->loadHtml('<tr>');				
	$dompdf->loadHtml('<th>Company</th>');
	$dompdf->loadHtml('<th>Contact</th>');								
	$dompdf->loadHtml('<th>Country</th>');
	$dompdf->loadHtml('</tr>');
0 likes
10 replies
Sinnbeck's avatar

@davy_yg It is just a wrapper for that package. To make it easier to work with laravel. And yes that is how views work

1 like
davy_yg's avatar
Level 27
			    $pdf = PDF::loadView('pdf.invoice', $data);
			    return $pdf->download('invoice.pdf');

Okay, how to catch the invoice.pdf in the frontend after the above code?

invoice.blade.php

			<a href="invoice.pdf">download invoice</a>

Will this recognize my code?

Sinnbeck's avatar

@davy_yg You need to tell the <a that it is a download, and use the correct route path

			<a href="/url/to/route" download>download invoice</a>
davy_yg's avatar
Level 27

I eventually use this (as someone else advice me): https://packagist.org/packages/niklasravnsborg/laravel-pdf

HargaController.php

	... 

	 $body.="</tr>";
    $body.="</tbody>";
    $body.="</table>";

    // bikin pdf
    $pdf = PDF::loadView('pdf.document', compact('body'));

    // Email penawaran to Marco
    Mail::to('[email protected]')->send(new SendPenawaran($pdf, $body));
   

    return $pdf->download('Penawaran.pdf');

Currently get this error message:

Error Object of class niklasravnsborg\LaravelPdf\Pdf could not be converted to string

ref: https://stackoverflow.com/questions/30260076/object-of-class-stdclass-could-not-be-converted-to-string-laravel

Another clue below the error message:

	E:\xampp76\htdocs\Fashionblinds\laravel\vendor\laravel\framework\src\Illuminate\Mail\Mailable.php:863

	
public function attachData($data, $name, array $options = [])
{

$this->rawAttachments = collect($this->rawAttachments)
 ->push(compact('data', 'name', 'options'))
 ->unique(function ($file) {
      return $file['name'].$file['data'];
 })->all();

	 return $this;
}
Sinnbeck's avatar

@davy_yg You cannot attach an instance of the PDF class... You need send in the data

output(): Outputs the PDF as a string.

    Mail::to('[email protected]')->send(new SendPenawaran($pdf->output(), $body));
davy_yg's avatar
Level 27

array_merge(): Expected parameter 2 to be an array, string given

SendPenawaran.php

	class SendPenawaran extends Mailable
	{
	use Queueable, SerializesModels;

	/**
 	* Create a new message instance.
 	*
 	* @return void
 	*/

	public function __construct($pdf, $body)
	{
    	//
    	$this->pdf = $pdf;
    	$this->body = $body;
	}

	/**
 	* Build the message.
 	*
 	* @return $this
 	*/

	public function build()
	{
    return $this->view('mail.penawaran')
                ->attachData($this->pdf, 'penawaran.pdf', [
                    'mime' => 'application/pdf',
                ]);

	}
}
davy_yg's avatar
Level 27

E:\xampp76\htdocs\Fashionblinds\laravel\vendor\laravel\framework\src\Illuminate\View\Factory.php:144

$data = array_merge($mergeData, $this->parseData($data))

Please or to participate in this conversation.