2017 © Pedro Peláez
 

library pdf-bot

image

optimus/pdf-bot

  • Sunday, August 13, 2017
  • by esbenp
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,488 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 19 % Grown

The README.md

🤖 pdf-bot-laravel

Laravel integration for pdf-bot, a Node microservice for generating PDFs using headless Chrome., (*1)

Installation

composer require optimus/pdf-bot 0.1.*

Add the service provider to config/app.php, (*2)

// ... other service providers
Optimus\PdfBot\PdfBotServiceProvider::class,

Publish the config file., (*3)

php artisan vendor:publish provider="Optimus\PdfBot\PdfBotServiceProvider"

You also need to create a controller for receiving webhooks., (*4)

<?php

namespace Infrastructure\Webhooks\Controllers;

use Illuminate\Http\Request;
use Optimus\PdfBot\PdfBotController;

class PdfController extends PdfBotController
{
    protected function onPdfReceived(array $data, Request $request)
    {
        // Do stuff with PDF
    }
}

And add its route to your routes, (*5)

<?php
$router->post('/webhooks/pdf', '\Infrastructure\Webhooks\Controllers\PdfController@receive');

Now you can access pdf-bot using, (*6)

$pdfBot = app()->make(\Optimus\PdfBot\PdfBot::class);

or by adding the including facade to your facades., (*7)

Configuration

As a minimum you should change the server and secret parameters in config/optimus.pdfbot.php., (*8)

<?php

return [
    'secret' => 'secret-used-for-generating-signature',

    'server' => 'http://url-to-pdf-bot-server',

    'header-namespace' => 'X-PDF-'
];

Usage example

Now you are ready to generate PDF's. Start by pushing a URL to the PDF server for generation., (*9)

$pdfBot = app()->make(\Optimus\PdfBot\PdfBot::class);
$pdfBot->push('https://invoices.traede.com/1', [
    'type' => 'invoice',
    'id' => 1
]);

Now this will be added to your PDF queue. At some point your PDF controller method (onPdfReceived) will receive a request with the path to the file. In your controller you can decide what to do with it., (*10)

<?php

namespace Infrastructure\Webhooks\Controllers;

use Illuminate\Http\Request;
use Optimus\PdfBot\PdfBotController;

class PdfController extends PdfBotController
{
    protected function onPdfReceived(array $data, Request $request)
    {
        $meta = $data['meta'];
        $s3 = $data['s3'];

        switch ($meta['type']) {
            case 'invoice':
                $invoice = \Invoice::find($meta['id']);
                $invoice->pdf = $s3['path']['key'];
                $invoice->save();
                break;
        }
    }
}

Now the invoice's location is saved on the correct invoice., (*11)

Issues

Please report issues to the issue tracker, (*12)

License

The MIT License (MIT). Please see License File for more information., (*13)

The Versions