NIKHIL NEDIYODATH
NIKHIL NEDIYODATH

Reputation: 2932

How to generate PDF in laravel using laravel-dompdf library

I'm trying to generate pdf using laravel-dompdf library. I had downloaded the package through composer.

composer require barryvdh/laravel-dompdf

I had updated my config >> app.php

'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

I included PDF class in my controller

use PDF;

My controller HomeController.php is

<?php

namespace App\Http\Controllers;
use PDF;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(){
        $pdf = PDF::loadView('welcome');
        return $pdf->download('invoice.pdf');
    }
}

But when I call this method it shows the following error

"fopen(C:\Users\asus\Desktop\Laravel\PDFGenerator\storage\fonts/\b4947bfc07f9f11b363f4d7446cd3e99.ufm): failed to open stream: No such file or directory"

I had also added the following to my bootstrap/app.php

$app->register(\Barryvdh\DomPDF\ServiceProvider::class);
$app->configure('dompdf');

There is no dompdf.php is generated in my config directory. I had tried

php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"

But it shows the following error

PHP Fatal error: Uncaught ReflectionException: Class config does not exist in

C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Container\Container.php:767 Stack trace: C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Container\Container.php(767): ReflectionClass->__construct('config') C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Container\Container.php(646): Illuminate\Container\Container->build('config') C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Container\Container.php(601): Illuminate\Container\Container->resolve('config', Array) C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(740): Illuminate\Container\Container->make('config', Array) C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Container\Container.php(1210): Illuminate\Foundation\Application->make('config') C:\U in C:\Users\asus\Desktop\Laravel\PDFGenerator\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 767

Upvotes: 1

Views: 12367

Answers (2)

katsarov
katsarov

Reputation: 1822

remove

$app->register(\Barryvdh\DomPDF\ServiceProvider::class);
$app->configure('dompdf');

It is for Lumen not Laravel. You still need to specify font. Here is my way:

$pdf = PDF::setOptions(['defaultFont' => 'dejavu serif'])->loadView('some_view', $data);
return $pdf->stream('filename.pdf');

Upvotes: 1

Mirza Obaid
Mirza Obaid

Reputation: 1717

The defaults configuration settings are set in config/dompdf.php. Copy this file to your own config directory to modify the values. You can publish the config using this command:

php artisan vendor:publish

You can still alter the dompdf options in your code before generating the pdf using this command:

PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);

Available options and their defaults:

rootDir: "{app_directory}/vendor/dompdf/dompdf"
tempDir: "/tmp" (available in config/dompdf.php)
fontDir: "{app_directory}/storage/fonts/" (available in config/dompdf.php)
fontCache: "{app_directory}/storage/fonts/" (available in config/dompdf.php)
chroot: "{app_directory}" (available in config/dompdf.php)
logOutputFile: "/tmp/log.htm"
defaultMediaType: "screen" (available in config/dompdf.php)
defaultPaperSize: "a4" (available in config/dompdf.php)
defaultFont: "serif" (available in config/dompdf.php)
dpi: 96 (available in config/dompdf.php)
fontHeightRatio: 1.1 (available in config/dompdf.php)
isPhpEnabled: false (available in config/dompdf.php)
isRemoteEnabled: true (available in config/dompdf.php)
isJavascriptEnabled: true (available in config/dompdf.php)
isHtml5ParserEnabled: false (available in config/dompdf.php)
isFontSubsettingEnabled: false (available in config/dompdf.php)
debugPng: false
debugKeepTemp: false
debugCss: false
debugLayout: false
debugLayoutLines: true
debugLayoutBlocks: true
debugLayoutInline: true
debugLayoutPaddingBox: true
pdfBackend: "CPDF" (available in config/dompdf.php)
pdflibLicense: ""
adminUsername: "user"
adminPassword: "password"

Upvotes: 0

Related Questions