定制 heimrichhannot/pdf-creator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

heimrichhannot/pdf-creator

最新稳定版本:0.4.3

Composer 安装命令:

composer require heimrichhannot/pdf-creator

包简介

PdfCreator is a high level API for PDF file creating with PHP.

README 文档

README

PdfCreator is a high level API for PDF file creating with PHP. Its goal is to make usage of existing libraries easier and object-orientated.

Features

Example

use HeimrichHannot\PdfCreator\Concrete\MpdfCreator;
use HeimrichHannot\PdfCreator\PdfCreatorFactory;

$pdf = PdfCreatorFactory::createInstance(MpdfCreator::getType());
$pdf->setHtmlContent($this->compile())
    ->setFilename($this->getFileName())
    ->setFormat('A4')
    ->setOrientation($pdf::ORIENTATION_PORTRAIT)
    ->addFont(
        "/path_to_project/assets/fonts/my_great_font.tff", 
        "myGreatFont", 
        $pdf::FONT_STYLE_REGUALAR,
        "normal"
    )
    ->setMargins(15, 10, 15,10)
    ->setTemplateFilePath("/path_to_project/assets/pdf/mastertemplate.pdf")
    ->setOutputMode($pdf::OUTPUT_MODE_DOWNLOAD)
    ->render()
;

Usage

Install

We recommend installing this library with composer:

composer require heimrichhannot/pdf-creator

You also need to install the pdf library, you want to use this bundle with:

  • Dompdf (version 1 to 3 are supported):
    • "dompdf/dompdf": "^3.0"
    • if you want to use master templates in Dompdf, you also need FPDI and TCPDF:
      • "tecnickcom/tcpdf": "^6.3"
      • "setasign/fpdi": "^2.3"
  • mPDF (version 7 and 8 are supported):
    • "mpdf/mpdf": "^8.0"
  • TCPDF
    • "tecnickcom/tcpdf": "^6.3"
    • if you want to use master templates in TCPDF, you also need FPDI:
      • "setasign/fpdi": "^2.3"

If you're using Contao, you could try the PDF Creator Bundle, which is based on this library.

Use callback for custom adjustments

Due the high level approach not all specific library functionality could be supported. To add specific configuration, you can use the callback mechanism comes with this api.

Callback Description
BeforeCreateLibraryInstanceCallback Is evaluated before the library instance is created and allows to modifiy the constructor parameters.
BeforeOutputPdfCallback Is evaluated before the library method to output the pdf is called and provide the library instance and the output method parameters.
use HeimrichHannot\PdfCreator\BeforeCreateLibraryInstanceCallback;
use HeimrichHannot\PdfCreator\BeforeOutputPdfCallback;
use HeimrichHannot\PdfCreator\Concrete\MpdfCreator;
use HeimrichHannot\PdfCreator\PdfCreatorFactory;

$pdf = PdfCreatorFactory::createInstance(MpdfCreator::getType());

$pdf->setBeforeCreateInstanceCallback(function (BeforeCreateLibraryInstanceCallback $callbackData) {
    $parameter = $callbackData->getConstructorParameters();
    $parameter['config']['fonttrans'] = [
        'rotis-sans-serif-w01-bold' => 'rotis-sans-serif',
        'rotissansserifw01-bold' => 'rotis-sans-serif',
    ];
    $callbackData->setConstructorParameters($parameter);
    return $callbackData;
});

$pdf->setBeforeOutputPdfCallback(function (BeforeOutputPdfCallback $callbackData) use ($pdf) {
    $mpdf = $callbackData->getLibraryInstance();
    $mpdf->AddPage();
    $parameters = $callbackData->getOutputParameters();
    $parameters['name'] = 'custom_'.$pdf->getFilename();
    $callbackData->setOutputParameters($parameters);
});

Use return value

The render method return an PdfCreatorResult instance. It contains the output mode and filepath or filecontent for corresponding output modes.

use HeimrichHannot\PdfCreator\Concrete\DompdfCreator;
use HeimrichHannot\PdfCreator\PdfCreatorFactory;

$pdf = PdfCreatorFactory::createInstance(DompdfCreator::getType());
$result = $pdf->setOutputMode($pdf::OUTPUT_MODE_FILE)
    // ...
    ->render()
;
$filepath = $result->getFilePath();

$pdf = PdfCreatorFactory::createInstance(DompdfCreator::getType());
$result = $pdf->setOutputMode($pdf::OUTPUT_MODE_STRING)
    // ...
    ->render()
;
$filepath = $result->getFileContent();

Documentation

统计信息

  • 总下载量: 769
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2021-02-26