daoandco/cakephp-dompdf

CakePHP 的 Dompdf 插件

安装数: 13,131

依赖关系: 0

建议者: 0

安全性: 0

星标: 4

关注者: 2

分支: 6

公开问题: 4

类型:cakephp-plugin

v1.2.1 2019-02-19 14:27 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:26:13 UTC


README

要求

  • PHP 版本 5.4.16 或更高
  • CakePhp 3.0 或更高
  • Dompdf 0.7

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方式是

composer require daoandco/cakephp-dompdf

安装后,为 CSS 生成符号链接(https://book.cakephp.com.cn/3.0/en/deployment.html#symlink-assets

// In a shell
bin/cake plugin assets symlink

快速入门

加载插件

  // In config/bootstrap.php
  Plugin::load('Dompdf');

激活 pdf 扩展(https://book.cakephp.com.cn/3.0/en/development/routing.html#routing-file-extensions

  // In config/routes.php
  Router::scope('/', function ($routes) {

    $routes->extensions(['pdf']);
    ...
  }

加载组件 RequestHandler

  // In src/controller/AppController.php
  public function initialize() {
    parent::initialize();

    $this->loadComponent('RequestHandler');
  }

在控制器中

class YopController extends AppController {

    public function view($filename) {

        $this->viewBuilder()
            ->className('Dompdf.Pdf')
            ->layout('Dompdf.default')
            ->options(['config' => [
                'filename' => $filename,
                'render' => 'browser',
            ]]);
    }
}

创建一个视图(pdf 内容)

<!-- src/Template/Yop/pdf/view.ctp -->
<?php $this->start('header'); ?>
    <p>Header.</p>
<?php $this->end(); ?>

<?php $this->start('footer'); ?>
    <p>Footer.</p>
<?php $this->end(); ?>


<h1>My title</h1>

<p>Banana</p>

<p>Boom !!!</p>

在浏览器中显示 pdf: http://dev.local/myproject/yop/view/test.pdf

配置

使用 $this->viewBuilder()

视图

页眉

使用默认布局和 dompdf.css

$this->start('header');
    echo '<p>I'm a header</p>';
$this->end();

页脚

使用默认布局和 dompdf.css

$this->start('footer');
    echo '<p>I'm a footer</p>';
$this->end();

图片

使用 Helper

/**
  * Générate an image
  * @param  string $path : Path to the image file, relative to the app/webroot/img/ directory
  * @param  array  $options : Array of HTML attributes
  * @return string <img>
  */
public function image($path, $options = false) {
  ...
}

示例

echo $this->Dompdf->image('test.png', ['class' => 'imgclass']);

CSS 样式表

使用 Helper

/**
  * Creates a link element for CSS stylesheets
  * @param  string $path : The name of a CSS style sheet
  * @param  bool $plugin : (true) add a plugin css file || (false) add a file in webroot/css /// default : false
  * @return string <link>
  */
public function css($path, $plugin) {
  ...
}

示例

echo $this->Dompdf->css('mycss');

页面分割

使用 dompdf.css

<p>Page 1</p>

<?= $this->Dompdf->page_break(); ?>

<p>Page 2</p>

渲染

在浏览器中显示

$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'render' => 'browser',
    ]]);

强制在浏览器中下载

$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'filename' => 'mydocument',
        'render' => 'download',
    ]]);

在服务器上上传

$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'upload_filename' => WWW_ROOT.'pdf/mydocument.pdf',
        'render' => 'upload',
    ]]);

use Cake\View\ViewBuilder;

$builder = new ViewBuilder();
$builder->className('Dompdf.Pdf')
        ->layout('Dompdf.pdf/default')
        ->template('Pdf/pdf/view')
        ->options(['config' => [
            'render' => 'stream',
        ]]);
$view = $builder->build();

$stream = $view->render();

分页

使用 Helper

您可以显示页码,但不能显示页数

<!-- In a view -->
<?php $this->start('footer'); ?>
    <p><?= $this->Dompdf->page_number(); ?></p>
<?php $this->end(); ?>

使用 PdfView

您可以显示页码和页数。在视图配置中使用分页键

$this->viewBuilder()
    ->className('Dompdf.Pdf')
    ->layout('Dompdf.default')
    ->options(['config' => [
        'filename' => $filename,
        'render' => 'browser',
        'paginate' => [
            'x' => 550,
            'y' => 5,
        ],
    ]]);

分页选项

  • x : 左位置:默认 0
  • y : 顶部位置:默认 0
  • 字体:字体族:默认 null
  • 大小:字体大小:默认 12
  • 文本:默认 "{PAGE_NUM} / {PAGE_COUNT}"
  • 颜色:RGB(数组):默认 [0,0,0] = 黑色