Crear archivos Word RTF con PHPRtfLite
PHPRtfLite es una API para generar archivos RTF, los archivos RTF pueden ser abiertos con Microsoft Word y podemos trabajar como si fuera de extensión .doc o .docx. Es ideal para nuestros proyectos donde tenemos que generar reportes en formato Word.
La librería pueden descargarlo desde GitHub https://github.com/phprtflite/PHPRtfLite.
Documentación http://sigma-scripts.de/phprtflite/docs/
Aquí les dejo un ejemplo:
<?php $dir = dirname(__FILE__); require_once $dir . '/lib/PHPRtfLite.php'; // register PHPRtfLite class loader PHPRtfLite::registerAutoloader(); //Rtf document $rtf = new PHPRtfLite(); $rtf->setDefaultFont(new PHPRtfLite_Font(11, 'Calibri', '#000000')); // margin left: 3cm $rtf->setMarginLeft(3); // margin right: 3cm $rtf->setMarginRight(3); // margin top: 2.5cm $rtf->setMarginTop(2.5); // margin bottom: 2.5cm $rtf->setMarginBottom(2.5); $parFormatLeft = new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_LEFT); $parFormatRight = new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_RIGHT); $parFormatCenter = new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_CENTER); $parFormatJustify = new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_JUSTIFY); //section $section = $rtf->addSection(); $font = new PHPRtfLite_Font(14, 'Calibri', '#000000', '#ffff00'); $header = $section->addHeader(); $header->writeText('Document Header'); $footer = $section->addFooter(); $footer->writeText('Document Footer'); $section->writeText('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' . "\n\n", null, $parFormatLeft); $section->writeText('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' . "\n\n", $font, $parFormatRight); $section->addImage($dir . '/images/imagen.jpg',null,15); $section->writeText('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' . "\n\n", null, $parFormatCenter); $section->writeText('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' . "\n\n", null, $parFormatJustify); $rowCount = 5; $rowHeight = 1; $columnCount = 4; $columnWidth = 3; // if width of page is 15 cm $columnWidth = 15/$columnCount; $table = $section->addTable('center'); $table->addRows($rowCount, $rowHeight); $table->addColumnsList(array_fill(0, $columnCount, $columnWidth)); $border = new PHPRtfLite_Border( $rtf, // PHPRtfLite instance new PHPRtfLite_Border_Format(2, '#00FF00'), // left border: 2pt, green color new PHPRtfLite_Border_Format(1, '#FFFF00'), // top border: 1pt, yellow color new PHPRtfLite_Border_Format(2, '#FF0000'), // right border: 2pt, red color new PHPRtfLite_Border_Format(1, '#0000FF') // bottom border: 1pt, blue color ); for ($rowIndex = 1; $rowIndex <= $rowCount; $rowIndex++) { for ($columnIndex = 1; $columnIndex <= $columnCount; $columnIndex++) { $cell = $table->getCell($rowIndex, $columnIndex); $cell->writeText("Cell $rowIndex:$columnIndex"); $cell->setTextAlignment(PHPRtfLite_Table_Cell::TEXT_ALIGN_CENTER); $cell->setVerticalAlignment(PHPRtfLite_Table_Cell::VERTICAL_ALIGN_CENTER); $cell->setBorder($border); } } $borderTop = new PHPRtfLite_Border($rtf); $borderTop->setBorderTop(new PHPRtfLite_Border_Format(2, '#f33')); $table->setBorderForCellRange($borderTop, 1, 1, 1, $columnCount); $borderBottom = new PHPRtfLite_Border($rtf); $borderBottom->setBorderBottom(new PHPRtfLite_Border_Format(2, '#33f')); $table->setBorderForCellRange($borderBottom, $rowCount, 1, $rowCount, $columnCount); $cell = $table->getCell(2, 2); $cell->writeText('Lorem ipsum dolor sit amet.'); $section->writeText('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' . "\n\n"); $section = $rtf->addSection(); $section->writeText('Chess tournamet information (write your data)' . "\n\n"); // save rtf document $rtf->save($dir . '/' . basename(__FILE__, '.php') . '.rtf'); |
Articulos Relacionados
(Visited 701 times, 1 visits today)