1. Go to this page and download the library: Download contoweb/laravel-pdflib library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
contoweb / laravel-pdflib example snippets
Contoweb\Pdflib\PdflibServiceProvider::class,
'Pdf' => Contoweb\Pdflib\Facades\Pdf::class,
use App\Documents\MarketingDocument;
use Contoweb\Pdflib\Facades\Pdf;
use App\Http\Controllers\Controller;
class MarketingController extends Controller
{
public function storeDocument()
{
return Pdf::store(new MarketingDocument, 'marketing.pdf');
}
}
public function draw(Writer $writer)
{
$writer->newPage();
$writer->useFont('Arial', 12);
$writer->writeText('Start something great...');
}
$writer->newPage();
$writer->newPage(210, 297); // A4 portrait format
namespace App\Documents;
use Contoweb\Pdflib\Concerns\FromTemplate;
use Contoweb\Pdflib\Concerns\WithDraw;
use Contoweb\Pdflib\Writers\PdfWriter as Writer;
class MarketingDocument implements FromTemplate, WithDraw
{
public function template(): string {
return 'template.pdf';
}
// ...
public function draw(Writer $writer)
{
$writer->newPage()->fromTemplatePage(1);
}
}
namespace App\Documents;
use Contoweb\Pdflib\Concerns\FromTemplate;
use Contoweb\Pdflib\Concerns\WithDraw;
use Contoweb\Pdflib\Concerns\WithPreview;
use Contoweb\Pdflib\Writers\PdfWriter as Writer;
class MarketingDocument implements FromTemplate, WithDraw, WithPreview
{
public function template(): string {
return 'print.pdf';
}
public function previewTemplate(): string
{
return 'preview.pdf';
}
public function offset(): array
{
return [
'x' => 20,
'y' => 20,
];
}
//
}
$writer->setPosition(10, 100);
// only X axis
$writer->setXPosition(10);
//only Y axis
$writer->setYPosition(100);
$writer->writeTextLine('your text');
// or
$writer->writeText('your text');
$writer->nextLine();
$writer->setLineSpacing(2.0);
public function fonts(): array
{
return ['OpenSans-Regular'];
}
public function draw(Writer $writer)
{
$writer->newPage();
$writer->useFont('OpenSans-Regular', 12)
->writeText('This text is written with Open Sans font...');
}
public function fonts(): array
{
return [
'OpenSans-Regular' => [
'encoding' => 'ansi',
'optlist' => ''
],
];
}