定制 slims/pdf-factory 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

slims/pdf-factory

Composer 安装命令:

composer require slims/pdf-factory

包简介

SLiMS Library to generate report as PDF file with varous PDF providers

README 文档

README

Example code:

<?php
use SLiMS\Pdf\Factory;
use SLiMS\Pdf\Contract;
use Dompdf\Dompdf;

require __DIR__ . '/../vendor/autoload.php';

class MyProvider extends Contract {

    public function setPdf():void
    {
        $this->pdf = new Dompdf();
    }

    public function setContent():void
    {
        $this->pdf->loadHtml('<h1>Hello World!</h1>');
    }
    
    public function download(string $filename):void
    {
        $this->stream($filename, ['Attachment' => true]);
    }
    
    public function stream(?string $filename = null, ?array $options = null):void
    {
        $this->pdf->render();
        $this->pdf->stream(($filename??md5('this') . 'pdf'), ($options??['Attachment' => false]));
        exit;
    }
    
    public function saveToFile(string $filepath, ?Closure $callback = null):void
    {
        $this->pdf->render();
        if ($callback !== null) {
            $callback($this->pdf, $filepath);
        } else {
            file_put_contents($filepath, $this->pdf->output());
            exit;
        }
    }
}

Factory::registerProvider('MyProvider', MyProvider::class);
Factory::useProvider('MyProvider');

if (isset($_GET['download'])) {
    Factory::download('harno.pdf');
}

if (isset($_GET['stream'])) {
    Factory::stream();
}

if (isset($_GET['save'])) {
    // Simple save
    Factory::saveToFile(__DIR__ . '/test.pdf');

    // Advance save with custom file handler
    Factory::saveToFile('test2.pdf', function($pdf, $filepath) {
        // you can use league\filesystem etc.
        file_put_contents(__DIR__ . '/' . $filepath, $pdf->output());
    });
}

统计信息

  • 总下载量: 42
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2023-09-30