ambersive/documentviewer

Laravel 包用于创建美观的打印文档。我们推荐与 ambersive/pdfprinter 包一起使用来创建文档。

v0.2.3 2020-10-04 18:36 UTC

README

本项目旨在提供一个创建美观、可打印和安全的文档的辅助功能。

Maintainability Test Coverage Build Status

安装

composer require ambersive/documentviewer

使用方法

步骤 1:创建可打印的文档

php artisan make:printable "Invoice"

步骤 2:注册文档查看器

在您的路由器中定义一个路由。请注意,默认的 Laravel Web 和 API 中间件可能需要在此处进行修改(例如,对于 POST 请求)。

$signed = false

Route::document(
    "invoices/{id}", 
    "invoices", 
    \App\Printables\Invoice::class, 
    [
        // Middleware for getting the document
    ], 
    $signed, 
    \App\Printables\Invoice::class, // for file upload handling
    [
        // Middleware for postback / upload the document
    ]
);

对于文档的 GET 请求,signed 属性始终有效。

如果您不想上传文件,请将上传类留空或设置为 null。

步骤 3:修改类

上述命令将在 app_path 下的 Printable 文件夹中创建 Printable 类。

首先,您应该定义在特定的 blade 文件中存在或需要的数据。

然后,您应该提供一个 uploadHandler。如果您使用我们的 Laravel 打印生成包Print-API,则需要该 uploadHandler。

use Illuminate\Http\Request;

use AMBERSIVE\DocumentViewer\Abstracts\DocumentAbstract;

class Invoice extends DocumentAbstract
{

    public array $data   = [];

    // Define the blade you want to return as printable document
    public String $blade = "ambersive.documentviewer::printable_default";
    public bool  $useValidationEndpoint = false; 

    public function setData(){
        // Request is available in $this->request
        // Save stuff in $this->data
        // This function must return $this (= chainable)
        // Params are provided in $this->params
        return $this;
    }

    public function uploadDocumentHandler(Request $request) {

        // Handle the file upload
        // Requires a response (preferable json)
        return ['status' => 200];

    }

    public function validateDocumentHandler(Request $request) {

        // Handle the validation
        // This information is a helper 
        // Requires a response (preferable json)
        return ['status' => 200];

    }

}

步骤 4:修改 blade 文件

make 命令还在资源文件夹中创建了一个 blade 文件。它将包含一些基本的脚手架设置,这样您就可以快速创建美观的文档。

如果您想创建一个自定义的 blade 文件,请确保您使用我们的基本 blade 进行扩展。否则,增强的视图优化将不起作用。

@extends('ambersive.documentviewer::printable')

一个完整的例子可能如下所示

@extends('ambersive.documentviewer::printable')

@section('styles')

    <style>
        body {
            // Custom Page style goes here
        }
    </style>

@endsection

@section('document')

    <div>Printable document</div>

@endsection

需要创建 PDF 文件的方法吗?

我们创建了一个开源的 微服务,用于创建 PDF 文件。如果您需要与该微服务进行顺畅交互,可以使用我们的 来实现。

安全漏洞

如果您在此包中发现安全漏洞,请通过 manuel.pirker-ihl@ambersive.com 向 Manuel Pirker-Ihl 发送电子邮件。所有安全漏洞都将得到及时解决。

许可证

本软件包是开源软件,根据 MIT 许可证 许可。