使用可重用模板创建PDF

dev-dev 2023-02-25 12:56 UTC

This package is auto-updated.

Last update: 2024-08-25 16:16:15 UTC


README

通过创建可重用模板来创建PDF。可选地,您可以为PDF定义不同的基本样式以供共享。例如,考虑标志、颜色、间距等。

PDF可以通过清晰的语法构建,每个创建的项都有变量和可扩展的选项。此包是为Laravel构建的,但也可以与纯PHP或其他任何框架一起使用。和其他一切一样,这也是可互换的。

如何创建文档

class ExamplePdf 
{
    private function build(PdfBuilder $builder) : Document
    {
        // start a document and load the settings that are default for all
        // These settings overwrite the default settings. 
        // therefor settings are optional as there is a default
        $document = $builder->document();
        $document->settings(new SomeSettings);

        // We do not add pages. Pages are added automatically. Instead we add sections and create pages as we go and as fits
        $document->addSection(function($section) {
            // section passes back to document but has the document in it

            // add a titleor a paragraph
            $section->add($this->title($section, 'The title'));
            $section->add($this->paragraph($section, 'Some text'));

            // tables are more complex. Once in a table you can add rows and cells
            $section->addTable(function($table) {

                // add a row
                $table->addRow(function($row) {

                    // each cell in the row might have a different height
                    // but we take the highest and equalize the rest
                    // to fit it. This makes advanced tables impossible
                    // but it standardizes the table
                    $row->addCell(function($cell) {

                        // lets drop a paragraph in the cell
                        $cell->add($this->paragraph($cell, 'Some paragraph'));
                    })->width(60);
                    $row->addCell(function($cell) {

                        // lets drop a title in the cell
                        $cell->add($this->title($cell, 'Some title'));
                    })->width(40);

                    // so this row has two cells. By default we will be 
                    // assuming that you'd like to have two columns
                    // each with 50% width. If you want to have a different
                    // width you can set it here as we did in percentages with 
                    // the function width(). But the function grid() is also
                    // available. This will set the width in grid units.
                    // mix and match as you like
                }, new RowSettings);

                // Settings can come from a class with a settings interface
                // or you can just chain the same interfaced functions
                // We make this possible by rendering after the init
            }, new TableSettings);
        })->break(false)->lineHeight(1.5); // etc

        $document->title('Some title')->download(); // or inline or whatever
    }
    
    // these can all be moved to traits if needed
    private function title($instance, $text) : Title
    {
        return (new Title($section))
            ->text($text)
            ->font('Calibri')
            ->margin(2, 4) // like css; top and bottom have 2, left right have 4
            ->padding(2); // Like css: All sides 2 points padding
            // etc;
    }

    private function paragraph($instance, $text) : Paragraph
    {
        return (new Paragraph($section))
            ->text($text);
            // etc;
    }
}

定义此包的是什么

目前,行业标准倾向于HTML转换。由于这是可维护的并且易于构建,我们发现转换速度较慢,且不支持CSS3(特别是flex box)。我们还想对页面分页有更多的控制,并且希望保持较小的文件大小,因为我们把所有的PDF保存到磁盘上以用于管理目的(这在某种程度上是针对我们业务的)。但真正的问题是速度。我们每月在后台构建数千个PDF。我们发现使用像FPDF这样的库仍然可以提供最快和最精简的PDF。然而,问题是接口。使用这个库创建一个合适的、可维护的PDF需要大量的计算。因此,这是我们尝试使其更加标准化、可扩展、可重复,最重要的是简单的尝试。