pmarien/html-to-pdf-bundle 问题修复 & 功能扩展

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

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

pmarien/html-to-pdf-bundle

最新稳定版本:1.0.0

Composer 安装命令:

composer require pmarien/html-to-pdf-bundle

包简介

Symfony integration for pmarien/html-to-pdf

README 文档

README

Symfony integration for pmarien/html-to-pdf

  1. Requirements
  2. How to install?
  3. How to configure?
  4. How to use internal assets?
  5. How to use?
    1. Controller (Inline PDF)
    2. Controller (Download PDF)
    3. Controller (Store PDF file)
    4. With Twig Templates
      1. Controller (Inline PDF)
      2. Controller (Download PDF)
      3. Controller (Store PDF file)

Requirements

The bundle requires PHP in version 8.1 or higher and Symfony in version 5, 6 or 7.

The html-to-pdf-library requires implementations of psr/http-message, psr/http-factory and psr/http-client.

How to install?

The bundle should be installed via composer: pmarien/html-to-pdf.

composer req pmarien/html-to-pdf

How to configure?

If you want to configure the bundle you should add the file config/packages/html_to_pdf.yaml.

Without api key configuration the bundle will act as anonymous client, which leads to a limit of 3 generated PDF files per day.

html_to_pdf:
  apiKey: 'YOUR_API_KEY'
  assetScheme: 'http' #optional to use a different asset location
  assetHost: 'example.com' #optional to use a different asset location

How to use internal assets?

If you want to use assets in your pdf, they have to be public accessible via http for the pdf generator. Sometimes you want to use the assets, but don't want to publish them. For those cases, this bundle comes with an AssetAccessController, which requires a hash for file protection.

The routing have to be enabled via config (config/routes/html_to_pdf.yaml):

HtmlToPdfBundle:
  resource: '@HtmlToPdfBundle/Resources/config/routes.xml'

Asset uris can be build manually (route name: html_to_pdf_get_file, required parameters: filename and hash) or via twig filter:

<img src="{{ '/assets/images/test.png'|pdfAsset }}" alt="Test">

If you build uris manually, the hash can be generated via PMA\HtmlToPdfBundle\Asset\AssetAccessorInterface::getHash (available as symfony service).

How to use?

If you want to generate pdf files directly from html, you should use PMA\HtmlToPdfBundle\Bridge\FoundationBridge as generator. It's available as service in the service container and may be autowired by symfony's dependency injection.

Controller (Inline PDF)

public function showAction(\PMA\HtmlToPdfBundle\Bridge\FoundationBridge $generator):\Symfony\Component\HttpFoundation\Response{
   return $generator->inlineResponse(
        'test.pdf',
        '<html><body><p>This is a test!</p></body></html>'
    );
}

Controller (Download PDF)

public function downloadAction(\PMA\HtmlToPdfBundle\Bridge\FoundationBridge $generator):\Symfony\Component\HttpFoundation\Response{
   return $generator->attachmentResponse(
        'test.pdf',
        '<html><body><p>This is a test!</p></body></html>'
    );
}

Service (Store PDF file)

public function storeFile(\PMA\HtmlToPdfBundle\Bridge\FoundationBridge $generator):void {
    $generator->createFile(
        '/your/project/file-storage/test.pdf',
        '<html><body><p>This is a test!</p></body></html>'
    );
}

Twig

If you want to generate pdf files from twig templates, you should use PMA\HtmlToPdfBundle\Bridge\TwigBridge as generator. It's available as service in the service container and may be autowired by symfony's dependency injection. It requires Twig to be installed in your project.

Controller (Inline PDF file)

public function showAction(\PMA\HtmlToPdfBundle\Bridge\TwigBridge $generator):\Symfony\Component\HttpFoundation\Response{
   return $generator->inlineResponse(
        'test.pdf',
         'pdf-templates/test.pdf.twig',
        [
            'test'=>'Test'
        ]
    );
}

Controller (Download PDF file)

public function downloadAction(\PMA\HtmlToPdfBundle\Bridge\TwigBridge $generator):\Symfony\Component\HttpFoundation\Response{
   return $generator->attachmentResponse(
        'test.pdf',
         'pdf-templates/test.pdf.twig',
        [
            'test'=>'Test'
        ]
    );
}

Service (Store PDF file)

public function storeFile(\PMA\HtmlToPdfBundle\Bridge\TwigBridge $generator):void {
    $generator->createFile(
        '/your/project/file-storage/test.pdf',
        'pdf-templates/test.pdf.twig',
        [
            'test'=>'Test'
        ]
    );
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固