我正在尝试使用laravel-dompdf库生成pdf。我通过作曲家下载了这个包。
composer require barryvdh/laravel-dompdf
我更新了我的配置>> app.php
'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
],
我在控制器中包含了PDF类
use PDF;
我的控制器 HomeController.php 是
<?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');
}
}
但是当我调用此方法时,它会显示以下错误
“的fopen(C:\用户\ ASUS \桌面\ Laravel \的pdfGenerator \存储\字体/ \ b4947bfc07f9f11b363f4d7446cd3e99.ufm): 无法打开流:没有这样的文件或目录“
我还将以下内容添加到我的bootstrap / app.php
中$app->register(\Barryvdh\DomPDF\ServiceProvider::class);
$app->configure('dompdf');
我的config目录中没有生成dompdf.php。我试过了
php artisan vendor:publish --provider="Barryvdh\DomPDF\ServiceProvider"
但它显示以下错误
PHP致命错误:未捕获ReflectionException:
中不存在类配置C:\用户\ ASUS \桌面\ Laravel \的pdfGenerator \厂商\ laravel \框架\ SRC \照亮\容器\ Container.php:767 堆栈跟踪: C:\ Users \用户华硕\桌面\ Laravel \的pdfGenerator \厂商\ laravel \框架\ SRC \照亮\集装箱\ Container.php(767): ReflectionClass-&GT; __构建体( '配置') C:\ Users \用户华硕\桌面\ Laravel \的pdfGenerator \厂商\ laravel \框架\ SRC \照亮\集装箱\ Container.php(646): 照亮\集装箱\容器 - &GT;建立( '配置') C:\ Users \用户华硕\桌面\ Laravel \的pdfGenerator \厂商\ laravel \框架\ SRC \照亮\集装箱\ Container.php(601): Illuminate \ Container \ Container-&gt; resolve('config',Array) C:\用户\ ASUS \桌面\ Laravel \的pdfGenerator \供应商\ laravel \框架的\ src \照亮\基金会\ Application.php(740): Illuminate \ Container \ Container-&gt; make('config',Array) C:\ Users \用户华硕\桌面\ Laravel \的pdfGenerator \厂商\ laravel \框架\ SRC \照亮\集装箱\ Container.php(1210): Illuminate \ Foundation \ Application-&gt; make('config')C:\ U in C:\用户\ ASUS \桌面\ Laravel \的pdfGenerator \供应商\ laravel \框架的\ src \照亮\集装箱\ Container.php 在第767行
答案 0 :(得分:1)
除去
$app->register(\Barryvdh\DomPDF\ServiceProvider::class);
$app->configure('dompdf');
这是Lumen不是Laravel。 您仍需要指定字体。 这是我的方式:
$pdf = PDF::setOptions(['defaultFont' => 'dejavu serif'])->loadView('some_view', $data);
return $pdf->stream('filename.pdf');
答案 1 :(得分:0)
默认配置设置在config / dompdf.php中设置。将此文件复制到您自己的config目录以修改值。您可以使用以下命令发布配置:
php artisan vendor:publish
在使用此命令生成pdf之前,您仍然可以更改代码中的dompdf选项:
PDF::setOptions(['dpi' => 150, 'defaultFont' => 'sans-serif']);
可用选项及其默认值:
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"
答案 2 :(得分:0)
首先,安装软件包:
composer require barryvdh/laravel-dompdf
在文件 config / app.php
中'providers' => [
....
Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
现在,我们将为演示示例添加路由,因此只需在您的路由文件中添加以下路由
routes / web.php
Route::get('generate-pdf','HomeController@generatePDF');
现在我们在这里创建HomeController
:
在这里,我们需要创建新的控制器HomeController,它将管理路由的generatePDF方法。因此,让我们输入下面的代码。
app / Http / Controllers / HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PDF;
class HomeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function generatePDF()
{
$data = ['title' => 'Welcome to HDTuto.com'];
$pdf = PDF::loadView('myPDF', $data);
return $pdf->download('hdtuto.pdf');
}
}
添加 myPDF.blade.php 文件:
最后,我们需要创建刀片文件调用 myPDF.blade.php 。
resources/views/myPDF.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Hi</title>
</head>
<body>
<h1>{{ $title }}</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</body>
</html>