1

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

5
  • what about Configuration ? Commented Mar 12, 2018 at 12:52
  • There is no dompdf.php file generated in config directory Commented Mar 12, 2018 at 13:15
  • I had updated my question with more detail Commented Mar 12, 2018 at 13:35
  • I solved this problem. here my answer with code: Commented Mar 6, 2019 at 3:52
  • I think this tutorial is helpfull: How to create dompdf in laravel Commented Jan 24, 2022 at 6:33

2 Answers 2

1

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');
Sign up to request clarification or add additional context in comments.

Comments

0

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"

3 Comments

I'm not able to publish using >> php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
what is your laravel version?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.