Documents
Documents are implemented by \JohnIt\Bc\Core\Domain\Models\Document
class. They provide a way for generating pdf files from HTML
using DomPDF
library and Mustache
engine.
Documents can have default content that can also be overwritten by users. They also can make use of dynamic placeholders. Placeholders are provided by a related Model
that this document renders (such as Contact
, License
etc.).
Documents are grouped into collections for easy filtering when displaying in drop down lists etc.
If you need to generate a pdf file for downloading or attaching to an email here are the steps to take:
- Open a file at
src/Domain/Core/Seeders/DocumentSeeder.php
and add code for inserting your new document into database according to the examples provided in that file. Please, do not paste HTML into that file, use a function such asfile_get_contents
to take body of document from a local file in a directory. Please, note thatdemo_placeholders
property must containkey => value
pairs to be used by tenant when overwriting document template through UI. They must contain same list as the target model that you will be rendering. As an example of target model please, look atDomain\Contacts\Models\Contact
model and note that it implementsgetPlaceholders
method ofSupport\ProvidesPlaceholders
interface. Also take note ofname_for_app
attribute, as you can use it to fetch some specific document for rendering. - Now to render just inserted document use this code:
```php $doc = Document::where(‘name_for_app’, ‘EXAMPLE_DOC_FOR_CONTACT’)->firstOrFail(); $doc->setModel($contact); //setting model that will provide placeholder keys and values. $pdf = $doc->__toString();
//make use of $pdf variable, for example in your custom mailable class: public function build() { return $this->view(‘emails.orders.shipped’) ->attachData($pdf, ‘name.pdf’, [ ‘mime’ => ‘application/pdf’, ]); } ```
Or alternatively if your mailable class inherits from Domain\Core\Mails\Mailable
you can also override its defaultAttachments
class to return instances of a Document
class to always be attached to that mailable. For example:
/**
* Get the default attachments of this mailable.
*
* @return Collection<\JohnIt\Bc\Core\Domain\Models\Document>
*/
public function defaultAttachments(): Collection
{
$invoice = $this->invoice;
//collections() is a scope on Document class that accepts names of collections to return documents for.
//here we get all documents in a collection called 'INVOICE'
return Document::collections(['INVOICE'])->get()->map(function ($doc) use ($invoice) {
$doc->setModel($invoice);
return $doc;
});
}
If you want to download document to user just return its instance from a controller method:
public function myMethod()
{
return Document::where(...)->firstOrFail();
}
Tenant admin can override default document template from this url: /my-tenant/documents
.
Documents can also be attached to emails by user from this url: /my-tenant/mailables
. These documents are getting attached in addition to what you provide in defaultAttachments
method.