PHP code example of fruitcake / laravel-weasyprint

1. Go to this page and download the library: Download fruitcake/laravel-weasyprint 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/ */

    

fruitcake / laravel-weasyprint example snippets




namespace App\Http\Controllers;

use Pontedilana\PhpWeasyPrint\Pdf;

class PdfController extends Controller
{
    public function __invoke(Pdf $weasyPrint)
    {

        //To file
        $html = '<h1>Bill</h1><p>You owe me money, dude.</p>';
        $weasyPrint->generateFromHtml($html, '/tmp/bill-123.pdf');
        $weasyPrint->generate('https://laravel.com/docs/10.x', '/tmp/laravel-docs.pdf');
        
        //Or output:
        return response(
            $weasyPrint->getOutputFromHtml($html),
            200,
            array(
                'Content-Type'          => 'application/pdf',
                'Content-Disposition'   => 'attachment; filename="file.pdf"'
            )
        );
    }
}


$pdf = \WeasyPrint::loadHTML('<h1>Test</h1>');
return $pdf->inline();

$pdf = \WeasyPrint::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

return \WeasyPrint::loadFile('https://laravel.com/docs')->inline('laravel.pdf');

\WeasyPrint::loadHTML($html)->setPaper('a4')->setOrientation('landscape')->setOption('margin-bottom', 0)->save('myfile.pdf')



namespace Tests\Feature;

use Tests\TestCase;
use PDF;

class ExampleTest extends TestCase
{
    public function testPrintOrderShipping()
    {
        PDF::fake();
        
        // Perform order shipping...
        
        PDF::assertViewIs('view-pdf-order-shipping');
        PDF::assertSee('Name');
    }
}

WeasyPrint::assertViewIs($value);
WeasyPrint::assertViewHas($key, $value = null);
WeasyPrint::assertViewHasAll(array $bindings);
WeasyPrint::assertViewMissing($key);
WeasyPrint::assertSee($value);
WeasyPrint::assertSeeText($value);
WeasyPrint::assertDontSee($value);
WeasyPrint::assertDontSeeText($value);
PDWeasyPrintF::assertFileNameIs($value);
bash
php artisan vendor:publish --provider="Fruitcake\WeasyPrint\WeasyPrintProvider"