iamproperty/print 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

iamproperty/print

Composer 安装命令:

composer require iamproperty/print

包简介

Document printing like Laravel's Mail package

README 文档

README

Introduction

Printer provides a way to render PDFs using an API similar to Laravel's Mailable.

Installation

$ composer require iamproperty/print

Writing Printables

All of a printable class' configuration is done in the build method. Within this method, you may call the methods view, and with to configure the view to be printed.

Configuring The View

Within a printable class' build method, you may use the view method to specify which template should be used when rendering the document's contents. Since each document typically uses a Blade template to render its contents, you have the full power and convenience of the Blade templating engine when building your document's HTML:

/**
 * Build the document.
 *
 * @return $this
 */
public function build()
{
    return $this->view('printed.orders.invoice');
}

View Data

Via Public Properties

Typically, you will want to pass some data to your view that you can utilize when rendering the document's HTML. There are two ways you may make data available to your view. First, any public property defined on your printable class will automatically be made available to the view. So, for example, you may pass data into your printable class' constructor and set that data to public properties defined on the class:

<?php

namespace App\Printed;

use App\Order;
use IAMProperty\Printer\Printable;

class OrderShipped extends Printable
{
    /**
     * The order instance.
     *
     * @var Order
     */
    public $order;

    /**
     * Create a new printable instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('printed.orders.shipped');
    }
}

Once the data has been set to a public property, it will automatically be available in your view, so you may access it like you would access any other data in your Blade templates:

<div>
    Price: {{ $order->price }}
</div>

Via The with Method:

If you would like to customize the format of your document's data before it is sent to the template, you may manually pass your data to the view via the with method. Typically, you will still pass data via the printable class' constructor; however, you should set this data to protected or private properties so the data is not automatically made available to the template. Then, when calling the with method, pass an array of data that you wish to make available to the template:

<?php

namespace App\Printed;

use App\Order;
use IAMProperty\Printer\Printable;

class OrderShipped extends Printable
{
    /**
     * The order instance.
     *
     * @var Order
     */
    protected $order;

    /**
     * Create a new printable instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('printed.orders.shipped')
                    ->with([
                        'orderName' => $this->order->name,
                        'orderPrice' => $this->order->price,
                    ]);
    }
}

Once the data has been passed to the with method, it will automatically be available in your view, so you may access it like you would access any other data in your Blade templates:

<div>
    Price: {{ $orderPrice }}
</div>

Rendering Printables

Sometimes you may wish to capture the HTML content of a printable without converting it to a PDF. To accomplish this, you may call the toHtml method of the printable. This method will return the evaluated contents of the printable as a string:

$invoice = App\Invoice::find(1);

return (new App\Printed\InvoicePaid($invoice))->toHtml();

Previewing Printables In The Browser

When designing a printable's template, it is convenient to quickly preview the rendered printable in your browser like a typical Blade template. For this reason, you may return any printable directly from a route Closure or controller. When a printable is returned, it will be rendered and displayed in the browser, allowing you to quickly preview its design without needing to open a separate PDF file:

Route::get('printable', function () {
    $invoice = App\Invoice::find(1);

    return new App\Printed\InvoicePaid($invoice);
});

It's also possible to use the Printer facade to render a printable's template, without rendering the document:

Route::get('printable', function () {
    $invoice = App\Invoice::find(1);

    return Printer::render(new App\Printed\InvoicePaid($invoice));
});

iamproperty/print 适用场景与选型建议

iamproperty/print 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 iamproperty/print 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 iamproperty/print 我们能提供哪些服务?
定制开发 / 二次开发

基于 iamproperty/print 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-25