Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Chrome Pdf Bundle Laravel Package

daif/chrome-pdf-bundle

View on GitHub
Deep Wiki
Context7

Working with assets

You can add assets in several ways, available for most builders.

HTML URL Markdown
PDF :white_check_mark: :white_check_mark: :white_check_mark:
Screenshot :white_check_mark: :white_check_mark: :white_check_mark:

[!WARNING] By default, the assets are fetched from the assets folder of your application. If your asset files are in another folder, you can override the default value of assets_directory in your configuration file config/daif_chrome_pdf.yaml.

The asset path resolution depends on certain criteria:

  • If an absolute path is provided in {{ chrome_pdf_asset() }}, assets() or addAsset, this path will be applied and not the one in the configuration file.

  • If a path is provided in {{ chrome_pdf_asset() }}, assets() or addAsset, it will be treated as a relative path from the assets_directory configuration.

  • If an absolute path is provided in the configuration file (assets_directory), the path applied in {{ chrome_pdf_asset() }}, assets() or addAsset will have assets_directory as base path.

  • If a relative path is provided in the configuration file (assets_directory), the path applied will have the root of the project as base path followed by the path from the configuration file.

// Without daif_chrome_pdf.assets_directory:
$builder->addAsset('/some/absolute/path/img.png'); // (string) '/some/absolute/path/img.png'

// Without daif_chrome_pdf.assets_directory:
$builder->addAsset('some/relative/img.png'); // (string) '%kernel.project_dir%/assets/some/relative/img.png'

// With daif_chrome_pdf.assets_directory: '/some/absolute/path'
$builder->addAsset('./some/relative/img.png'); // (string) '/some/absolute/path/some/relative/img.png'

// With daif_chrome_pdf.assets_directory: 'some/relative/path'
$builder->addAsset('./also/relative/img.png'); // (string) '%kernel.project_dir%/assets/some/relative/path/also/relative/img.png'

Twig file

{{ chrome_pdf_asset() }} Twig function will help you generate an asset path. This function works like asset() Twig function.

You can provide an absolute path.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>PDF body</title>
    </head>
    <body>
        <img src="{{ chrome_pdf_asset('img/ceo.jpeg') }}" alt="CEO"/>
        <img src="{{ chrome_pdf_asset('img/admin.jpeg') }}" alt="Admin"/>
    </body>
</html>

The path provided can be relative as well.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>PDF body</title>
    </head>
    <body>
        <img src="{{ chrome_pdf_asset('../public/img/ceo.jpeg') }}" alt="CEO"/>
        <img src="{{ chrome_pdf_asset('../public/img/admin.jpeg') }}" alt="Admin"/>
    </body>
</html>

And in your controller nothing needs to be changed.

namespace App\Controller;

use Daif\ChromePdfBundle\ChromePdfInterface;

class YourController
{
    public function yourControllerMethod(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->html()
            ->content('twig_simple_pdf.html.twig')
            ->generate()
            ->stream()
         ;
    }
}

HTML file

If your file is an HTML file and not a Twig template, you can also add some assets as below.

The only requirement is that their paths in the HTML file are on the root level.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>PDF body</title>
    </head>
    <body>
        <img src="ceo.jpeg" />
        <img src="admin.jpeg" />
    </body>
</html>

All you need to do is add the path of the asset file to either assets(...string) or addAsset(string).

namespace App\Controller;

use Daif\ChromePdfBundle\ChromePdfInterface;

class YourController
{
    public function yourControllerMethod(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->html()
            ->contentFile('content.html')
            ->assets(
                'img/ceo.jpeg',
                'img/admin.jpeg'
            )
            ->generate()
            ->stream()
        ;
    }
}

Relative paths work as well.

namespace App\Controller;

use Daif\ChromePdfBundle\ChromePdfInterface;

class YourController
{
    public function yourControllerMethod(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->html()
            ->contentFile('../public/content.html')
            ->assets(
                '../img/ceo.jpeg',
                '../img/admin.jpeg'
            )
            ->generate()
            ->stream()
        ;
    }
}

You can also add assets conditionally with addAsset():

namespace App\Controller;

use Daif\ChromePdfBundle\ChromePdfInterface;

class YourController
{
    public function yourControllerMethod(ChromePdfInterface $chromePdf): Response
    {
        return $chromePdf->html()
            ->contentFile('../public/content.html')
            ->assets(
                '../img/ceo.jpeg',
                '../img/admin.jpeg'
            )
            ->addAsset('../img/developer.jpeg')
            ->generate()
            ->stream()
        ;
    }
}

In the example above ceo.jpeg, admin.jpeg and developer.jpeg will all be loaded.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon