1. Go to this page and download the library: Download asarmiento/laravel-fpdf 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/ */
asarmiento / laravel-fpdf example snippets
return [
'default_font' => 'Arial', // Fuente predeterminada
'default_size' => 12, // Tamaño de fuente predeterminado
'margin_left' => 10, // Margen izquierdo en mm
'margin_right' => 10, // Margen derecho en mm
'margin_top' => 10, // Margen superior en mm
'margin_bottom' => 10, // Margen inferior en mm
'orientation' => 'P', // Orientación: P (Portrait) o L (Landscape)
'unit' => 'mm', // Unidad de medida (mm, pt, cm, in)
'format' => 'A4' // Formato del papel
];
// En routes/web.php
use Asarmiento\FriendlyFpdf\Fpdf;
Route::get('pdf', function (Fpdf $fpdf) {
$fpdf->AddPage();
$fpdf->SetFont('Arial', 'B', 16);
$fpdf->Cell(0, 10, '¡Hola Mundo!', 0, 1, 'C');
$fpdf->Output('I', 'documento.pdf');
});
// En un Controller
class PdfController extends Controller
{
public function generate(Fpdf $fpdf)
{
$fpdf->AddPage();
$fpdf->SetFont('Arial', 'B', 16);
$fpdf->Cell(0, 10, '¡Hola Mundo!', 0, 1, 'C');
return response($fpdf->Output('S'), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="documento.pdf"'
]);
}
}
use Asarmiento\FriendlyFpdf\Facades\FriendlyFpdf;
public function generatePdf()
{
return FriendlyFpdf::addPage()
->addText('¡Hola Mundo!', 10, 10)
->Output('I', 'documento.pdf');
}
use Asarmiento\FriendlyFpdf\Facades\Fpdf;
public function generatePdf()
{
Fpdf::AddPage();
Fpdf::SetFont('Arial', 'B', 16);
Fpdf::Cell(0, 10, '¡Hola Mundo!', 0, 1, 'C');
return Fpdf::Output('I', 'documento.pdf');
}
// Añadir una nueva página
FriendlyFpdf::addPage($orientation = '', $size = '', $rotation = 0);
// Añadir texto en una posición específica
FriendlyFpdf::addText($text, $x = null, $y = null, $align = 'L');
// Los valores válidos para $align son:
// 'L' - Alineación izquierda
// 'C' - Centrado
// 'R' - Alineación derecha
// Generar el PDF
FriendlyFpdf::Output($destination = 'I', $filename = 'doc.pdf');
// Valores para $destination:
// 'I' - Enviar al navegador
// 'D' - Forzar descarga
// 'F' - Guardar en archivo local
// 'S' - Retornar como string
use Asarmiento\FriendlyFpdf\Fpdf;
// En routes/web.php
Route::get('reporte', function (Fpdf $fpdf) {
$fpdf->AddPage();
$fpdf->SetFont('Arial', 'B', 18);
$fpdf->Cell(0, 15, 'Reporte Mensual', 0, 1, 'C');
$fpdf->SetFont('Arial', '', 12);
$fpdf->Cell(0, 10, 'Fecha: ' . date('Y-m-d'), 0, 1);
$fpdf->Cell(0, 10, 'Este es un ejemplo de reporte', 0, 1);
$fpdf->Output('D', 'reporte.pdf');
});
// En un Controller con respuesta personalizada
class ReporteController extends Controller
{
public function generar(Fpdf $fpdf)
{
$fpdf->AddPage();
$fpdf->SetFont('Arial', 'B', 18);
$fpdf->Cell(0, 15, 'Reporte Avanzado', 0, 1, 'C');
// Agregar contenido dinámico
$data = collect(['Item 1', 'Item 2', 'Item 3']);
$fpdf->SetFont('Arial', '', 12);
foreach ($data as $item) {
$fpdf->Cell(0, 8, $item, 0, 1);
}
return response($fpdf->Output('S'), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="reporte.pdf"'
]);
}
}
use Asarmiento\FriendlyFpdf\Facades\FriendlyFpdf;
public function generateReport()
{
return FriendlyFpdf::addPage()
->addText('Reporte Mensual', 10, 10, 'C')
->addText('Fecha: ' . date('Y-m-d'), 10, 20)
->addText('Este es un ejemplo de reporte', 10, 30)
->Output('D', 'reporte.pdf');
}
// Con inyección de dependencias
Route::get('save-pdf', function (Fpdf $fpdf) {
$fpdf->AddPage();
$fpdf->SetFont('Arial', '', 12);
$fpdf->Cell(0, 10, 'Contenido guardado', 0, 1);
$fpdf->Output('F', storage_path('app/pdfs/documento.pdf'));
return 'PDF guardado exitosamente';
});
// Con interfaz fluida
FriendlyFpdf::addPage()
->addText('Contenido del PDF', 10, 10)
->Output('F', storage_path('app/pdfs/documento.pdf'));
// Esto seguirá funcionando exactamente igual
Route::get('/', function (xxxxxxx\Fpdf\Fpdf $fpdf) {
$fpdf->AddPage();
$fpdf->SetFont('Courier', 'B', 18);
$fpdf->Cell(50, 25, 'Hello World!');
$fpdf->Output();
});
// Solo cambia el namespace:
Route::get('/', function (Asarmiento\FriendlyFpdf\Fpdf $fpdf) {
$fpdf->AddPage();
$fpdf->SetFont('Courier', 'B', 18);
$fpdf->Cell(50, 25, 'Hello World!');
$fpdf->Output();
});
// config/friendly-fpdf.php
return [
'orientation' => 'P', // P = Portrait, L = Landscape
'unit' => 'mm', // mm, pt, cm, in
'size' => 'A4', // A4, A5, Letter, Legal, etc.
'default_font' => [
'family' => 'Helvetica', // Arial, Helvetica, Times, Courier
'style' => '', // '', 'B', 'I', 'U'
'size' => 12
],
'vapor_support' => env('FPDF_VAPOR_HEADERS', false),
];