PHP code example of sglms / pdf

1. Go to this page and download the library: Download sglms/pdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sglms / pdf example snippets


use Sglms\Pdf\Facades\Pdf;

Pdf::view('pdf.invoice', ['order' => $order])->output('invoice.pdf');

use Sglms\Pdf\Facades\Pdf;

$pdf = Pdf::init(
	config: ['format' => 'Letter'],
	stylesheet: 'css/pdf.css',
	header: 'pdf.header',
	footer: 'pdf.footer'
);

$pdf->view('pdf.page-one', ['name' => 'Jane'])
	->view('pdf.page-two')
	->output('document.pdf');

$pdf->logo('images/logo.svg');

$pdf->sign(
	signature: 'pdf.signature',
	data: ['name' => 'John Doe'],
	x: 50,
	y: 100,
	width: 100
);

$pdf->signFile('pdf.signature', storage_path('app/base.pdf'));

// Raw string — pass to Laravel's response() helper
$raw = $pdf->output('document.pdf');
$raw = $pdf->string();                   // alias, no filename context

// Write to an absolute file path
$pdf->save(storage_path('app/a.pdf'));

// Laravel Storage disk
$pdf->storeAs('a.pdf', 'public');

// Stream directly to the browser (bypasses Laravel response)
$pdf->inline('document.pdf');            // Content-Disposition: inline
$pdf->download('document.pdf');          // Content-Disposition: attachment

$mpdf = $pdf->get();

use App\Http\Controllers\Controller;
use Sglms\Pdf\Facades\Pdf;

class CustomController extends Controller
{
	public function render()
	{
		$bytes = Pdf::view('pdf.filename')
			->sign('pdf.signature')
			->output('document.pdf');

		return response($bytes, 200)
			->header('Content-Type', 'application/pdf')
			->header('Content-Disposition', 'inline; filename="document.pdf"');
	}
}