abiturma/laravel-latex 问题修复 & 功能扩展

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

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

abiturma/laravel-latex

Composer 安装命令:

composer require abiturma/laravel-latex

包简介

A LaTeX compiler wrapper for Laravel

README 文档

README

This package compiles Blade views containing LaTeX source code to a pdf file

Requirements

You need an installation of a latex compiler (e.g. texlive) on your server. This package is designed for the use with Laravel 10 or newer. Php 8.1 ist required starting from version 2.0.0.

Older Laravel versions

For laravel 8 and 9 use version ^1.3 of this package.

Installation and Setup

Install using composer:

composer require abiturma/laravel-latex

The Service Provider and the Facade will be installed automatically via laravel package auto-discover.

To publish the config file to config/latex.php run:

php artisan vendor:publish --provider="Abiturma\LaravelLatex\LatexServiceProvider

The default configuration looks like so

return [
    'temp_directory' => storage_path().'/latex/temp',
    'output' => [
        'disk' => 'local',
        'path' => 'latex/output' 
    ],
    'debug' => false, 
    'pdflatex' => env('PATH_TO_PDFLATEX','pdflatex')
];

With the key pdflatex you have to set the absolute path to the pdflatex binary on your server.

Usage

You can compile a blade view either by calling the Facade Latex or by injecting the class Abiturma\LaravelLatex\LatexToPdf. Within the documentation we will

Basic usage

The class has a fluent interface, which is terminated by a call of ->get() to start compilation.

You have to pass a blade view together with the data of the view. A standard use case looks like so

use Latex

...

$pdf = Latex::view('your.favourite.latex.view')
    ->with(['key' => 'value'])
    ->get()

The return value will be a path to compiled pdf. The path is relative to the root directory of the storage disk given in your config file.

...

Storage::disk(config('latex.output.disk'))
->exists($pdf) //true 

Escaping input

Since LaTeX has its own syntax it is not advised to use the standard blade syntax {{ $variable }} or {!! $variable !!}. Instead you can use @latex($variable) in your blade templates instead, which handles the suitable escaping of reserved LaTeX characters.

Multiple runs

To enable LaTeX to handle cross references it is sometimes necessary to compile a document multiple times. You can specify the number of runs like so:

    Latex::view('myView')->runs($numberOfRuns)->get()

The number of runs will be automatically truncated to an integer between 1 and 10.

Asset Files

Once you use graphics or custom sty-files in your template your compiler needs more than one file. To handle this situation you can call

->assets([
    './relative/path/to/one/asset',
    './relative/path/to/another/asset
])

on the compiler. This will copy all specified files to the compilation directory. The path is relative to the directory of the view. If absolute paths are needed, set the absolutePath flag to true:

->assets([
    '/absolute/path/to/asset',
    '/absolute/path/to/another/asset
],
true)

Very often you have the case, that you want to include all files in the directory of the view as assets. In this case you can simply call ->includeViewFolder() on the compiler. This will copy all files of the ambient directory of the view in the compilation directory.

After copying the asset files to the compilation directory all blade views are compiled to the according tex files using the variables passed by ->with($data).

Example

Given the following tree structure

views
│
└── myDocument
     │   main.blade.php
     │   package.sty.blade.php 
     │   picture.jpg
    ...

the command

Latex::view('myDocument.main)
    ->with($data)
    ->includeViewFolder()
    ->get()

will produce a (temporary) compilation directory of the form

temp
│
└── #someHash
     │   main.tex
     │   package.sty 
     │   picture.jpg
    ...

where package.sty is the compiled version of package.sty.blade.php using $data.

Using Texables

Instead of calling the Compiler with all the necessary parameters you can also pass a Texable class which act similarly like Laravel Mailables.

For creating a mailable run the command latex:make MyTexable to create a stub in your view directory.

To compile a Texable run make::latex(new MyTexable()). Texables have basically the same methods as the compiler class. Before the latex compiler run, the ->build() method is run on the Texable. Moreover, all public variables on the Texable are available as data for the given view.

Example

Say the class MyTexable is given by

use Abiturma\LaravelLatex\Texable

class MyTexable extends Texable
{
    public $message;     

    __construct($message) {
        $this->message = $message;        
    }

    public function build() {
        return $this->view('someview')
    }
    
}

then running Latex::make(MyTexable("someString") is equivalent to

Latex::view('someview')->with(["message" => "someString"])->get()

Assets

Besides the ->asset() method, Texables have more options to handle assets. It is possible to add assets using ->addAsset(). These methods not only understand absolute and relative paths via the absolutePath flag as second argument, they also parse file patterns as glob. So it also possible to add assets like this ->addAsset('*.jpg'). To exclude file patterns use the method ->excludeAssets($assets)

Debugging

If the compilation fails a CompilationFailedException is raised. By default the exception message contains the truncated compilation log. If debug is set to true the exception message contains the full compilation log.

Commands

Testing

The command latex:test runs the compiler on two test files. If the compilation is successful the output paths of the files are printed.

Garbage Collection

The command latex:gc removes all temporary directories and files which were not garbage collected during the compilation process. This might happen, if the process failes before the automatic clean-up process of the compiler kicked in.

If you add the flag --output also the given output directory is cleaned. This makes sense if you are used to move the output files after generation to someplace else anyways.

Upgrade/Breaking changes

As of version 1.2.0, the ->assets() method of the LatexToPdf and Texable class now expect paths relative to the view folder instead of absolute paths. For downward compatibility use the absolutePath flag as second argument, e.g. ->assets($someLegacyPath, true).

Acknowledgements

This project is inspired by latexcompiler by Vernon Hockney.

abiturma/laravel-latex 适用场景与选型建议

abiturma/laravel-latex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.49k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2021 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 abiturma/laravel-latex 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-08