petecoop/odt 问题修复 & 功能扩展

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

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

petecoop/odt

Composer 安装命令:

composer require petecoop/odt

包简介

Render .odt files with Blade, convert to pdf with Libre/OpenOffice

README 文档

README

Use Open Document Text (.odt) files as templates in PHP/Laravel projects, rendering them with Blade syntax and outputting as ODT or PDF.

This allows you to create complex document templates in LibreOffice or OpenOffice, including loops and conditionals using Blade syntax, render them with dynamic data and convert into PDF.

Why

I've found that there are some things that just can't be done when generating PDF's with HTML or PDF libraries that word processors have solved. This is usually where dynamic content can flow over multiple pages, and you want to do things like repeat table headers, or have some items of text stay together, or other more complex layouts like anchoring a section to the bottom of the final page. This also allows non-developers to create and edit document templates in a familiar word processor rather than having to write HTML or code.

Installation

composer require petecoop/odt

Usage

In Laravel

Access via the ODT facade:

use Petecoop\ODT\Facades\ODT;

ODT::open(resource_path("file.odt"))
    ->render([
        "some" => "arguments",
    ]);

Other PHP Projects

Call ODT::make() to create an instance:

use Petecoop\ODT\ODT;

ODT::make()
    ->open(__DIR__ . "/templates/file.odt")
    ->render([
        "some" => "arguments",
    ]);

Converting to PDF

Conversion can be done using either the soffice binary directly, or via a Gotenberg server. soffice is the default and is slower due to the overhead of starting the LibreOffice process for each conversion, but requires no additional setup (other than installing LibreOffice). Gotenberg is faster for multiple conversions as it keeps a LibreOffice process running in the background, but requires a Gotenberg server to be running.

Using soffice for conversion

The soffice binary is required to be installed for PDF conversion. This should be available if LibreOffice or OpenOffice is installed on the system.

To convert to PDF, use the pdf() method after rendering.

$pdf = ODT::open(resource_path("file.odt"))
    ->render([
        "some" => "arguments",
    ])
    ->pdf();

If soffice is not in the system PATH, you can specify the path to the binary using the officeBinary method:

ODT::officeBinary('/path/to/soffice')
    ->open(resource_path("file.odt"));

// if using ODT::make()
ODT::make('/path/to/soffice')
    ->open(__DIR__ . "/templates/file.odt");

Using Gotenberg for conversion

To use Gotenberg for PDF conversion, you need to have a Gotenberg server running. You can specify the Gotenberg server URL using the gotenberg method:

$pdf = ODT::gotenberg('http://localhost:3000')
    ->open(resource_path("file.odt"))
    ->render([
        "some" => "arguments",
    ])
    ->pdf();

A HTTP Client is required to be installed, if outside of Laravel you can install the Guzzle client:

composer require php-http/guzzle7-adapter

See a full list of clients if you'd like to use a different one.

Saving or Downloading

The resulting .odt or PDF file can be saved to disk or returned as a download response:

$odt = ODT::open(resource_path("file.odt"))
    ->render([
        "some" => "arguments",
    ]);

// Save ODT file
$odt->save(storage_path("file.odt"));

// Convert to PDF and save
$pdf = $odt->pdf()->save(storage_path("file.pdf"));

// Return as download response in a Laravel Controller
return $pdf;

Templating

Some Blade directives are provided to help with common templating tasks in ODT files.

Image Embedding

Use the @image directive to embed base64 encoded images into your ODT templates. This directive accepts a base64 image string and optional maximum width and height parameters (in cm).

@image($base64ImageString, '5cm', '5cm')

Table Rows

When in Libre/Open Office you can't wrap a @foreach around a table row - use @rowforeach, this can be done inside a table cell. The entire row will be repeated for each item.

@rowforeach($users as $user)
{{ $user->name }}

All other cells in the row will have access to the $user variable.

@rowif can be used to conditionally include a table row based on an expression. This is useful if you want to display a row only when certain conditions are met.

@rowif($product->description)
{{ $product->description }}

Note: due to a limitation of matching nested parentheses, only one level of nested parentheses is supported in the @rowforeach(...) and @rowif(...) expressions. e.g. @rowforeach($users as $user) or @rowforeach($items->where('active', true) as $item) will work, but more complex expressions with multiple levels of parentheses will not.

If you need more control over what is before / after the row use @beforerow / @endbeforerow and @afterrow / @endafterrow. This example is the equivalent of the above but allows you to put any other directives before or after the row.

@beforerow@foreach ($users as $user)@endbeforerow
{{ $user->name }}
@afterrow@endforeach@endafterrow

petecoop/odt 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-11