xorgxx/neox-pdf-bundle

为 Symfony 定制的 Neox pdf bundle

0.1.2 2024-02-02 23:02 UTC

This package is auto-updated.

Last update: 2024-10-01 00:20:47 UTC


README

此扩展包提供服务多 API 来在您的应用程序中进行 Pdf-转换。其主要目标是使您能够轻松管理集成额外的工具!

BETA 版本安装 !!

使用 Composer 安装此扩展包 !! 因为它仍然是 beta 版本 !!

  composer require xorgxx/neox-pdf-bundle
  or 
  composer require xorgxx/neox-pdf-bundle:0.*

注意: 您可能需要使用 [symfony composer dump-autoload] 重新加载自动加载

..... 完成 🎈

要求 !!!

您需要注册到一个或多个 API 服务,它们将提供 API 密钥

  • 目前,我们只实现了一个提供者,如果您需要更多 PR,请访问 GitHub。我们将在未来实现更多

如何?

依次,您需要在 .env 中添加 Dsn,例如:pdflayer

  ....
  ###> NeoxToPdf ###
    PDFLAYER_DSN=pdflayer://opps:[api-key]@api.pdflayer.com/api/convert
  ###> NeoxToPdf ###
  ....

然后在 config/packages/neox_to_pdf.yaml 中添加

  neox_to_pdf:
      directory_save: "/public/neoxPdf/"
      services:
          pdfLayer: "%env(PDFLAYER_DSN)%"

** 重要 : s c:c & c dump-autoload **

控制器

  <?php
  
  namespace App\Controller\Admin;
  
  use NeoxToPdf\NeoxToPdfBundle\NeoxPdf\NeoxToPdfFactory;
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\Routing\Attribute\Route;
  use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  
  #[Route('/neox-to-pd')]
  class NeoxToPdfController extends AbstractController
  {
      /**
       * @throws TransportExceptionInterface
       */
      #[Route('/', name: 'app_neox_to_pdf')]
      public function index( NeoxToPdfFactory $neoxToPdf): Response
      {
          return $neoxToPdf->pdfLayerService()
              ->setParams('document_html',"Neox Wooooonnnn convert to pdf")
              ->setParams('test',true)
              ->convert()
              ->display_pdf();
              
          // Advance** (read Bellow) section ADVANCE
          $pdf = $neoxToPdf->customService("pdfLayerA")
              ->setPostData('document_html',"Neox Wooooonnnn convert to pdf") // in body
              ->setQuery('test',true) // in url
              ->convert()
              ->display_pdf();
      }
  }

内置命令

  • display_pdf | 在浏览器中返回 Pdf
  • download_pdf | 下载 Pdf 文件
  • getStream | 获取字符串
  • getRawResponse | 获取来自提供者的响应
  • file_pdf | 获取二进制文件响应

高级使用 "custom" 提供者 !!

在 config/packages/neox_to_pdf.yaml 中

  neox_to_pdf:
      ...
      # path to class customs
      directory_class: "App/Services"
      # Important | [pdfLayerAService] name have to be same "format" as the class name without "Service" ex: pdfLayerA not PdfLayera
      customs:
          pdfLayerA: "%env(PDFLAYERA_DSN)%"
    ...

.env 文件

    ###> NeoxToPdf ###
      ...
      PDFLAYERA_DSN=pdflayera://opps:[api-key]@api.pdflayer.com/api/convert
      ...
    ###> NeoxToPdf ###

自定义提供者结构类

<?php
    
    namespace App\Services;
    
    use NeoxToPdf\NeoxToPdfBundle\NeoxPdf\neoxPdfInterface;
    use NeoxToPdf\NeoxToPdfBundle\NeoxPdf\NeoxToPdfAbstract;
    use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
    use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
    use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
    use Symfony\Contracts\HttpClient\HttpClientInterface;
    
    class pdfLayerAService extends NeoxToPdfAbstract implements neoxPdfInterface
    {
        public readonly HttpClientInterface $httpClient;
        
        /**
         * method:  class construction
         * You can do your logic here!!.
         *
         * @param bool $redirect
         *
         * @return mixed
         * @throws TransportExceptionInterface
         */
        public function htmlConverter(bool $redirect = false): mixed
        {
            ...
            $request        = $this->buildRequest();
            ...
            $t = $this->sendRequest($request, $postData);
            
            return $this;
        }
        
        // Process to convert any (support) to pdf
        public function anyConverter(): mixed
        {
            // TODO: Implement anyConverter() method. Woooooooonnnn !
        }
        
        public function buildRequest(): string
        {
            return $this->build_request();
        }
        
        /**
         * @throws TransportExceptionInterface
         */
        public function sendRequest(string $request, array $postData): string
        {
            return $this->doApi($request, $postData);
        }
    }
    
    // This class have to extends NeoxToPdfAbstract and implements neoxPdfInterface
    // $this->buildRequest() will construct base on Dsn request API
    // $this->sendRequest($request, $postData) will send Api request : Note that this have to return string !!

贡献

如果您想为此扩展包做出贡献(感谢您!)以下是一些指导原则

  • 请尊重 Symfony 指南
  • 测试一切!请在修复未涵盖的 bug、添加新功能或看到未涵盖任何测试的代码时,将测试用例添加到 tests/ 目录中
    • 您修复了一个之前未涵盖的 bug
    • 您添加了一个新功能
    • 您看到代码在运行但未涵盖任何测试(天堂有一个特殊的地方为您保留)

待办事项

  • Packagist

谢谢